Skip to content

Commit

Permalink
Bug 1573097 - Fix Invalid casts in instance.c, r=mt
Browse files Browse the repository at this point in the history
  • Loading branch information
Ho Cheung committed Oct 10, 2023
1 parent cb043df commit 335d294
Showing 1 changed file with 33 additions and 12 deletions.
45 changes: 33 additions & 12 deletions lib/ckfw/instance.c
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,27 @@ nssCKFWInstance_GetInitArgs(
return fwInstance->pInitArgs;
}

#if defined(_MSC_VER)
#pragma warning(push)
#pragma warning(disable: 4312)
#elif defined(__clang__) || defined(__GNUC__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpointer-to-int-cast"
#endif

PR_STATIC_ASSERT(sizeof(CK_SESSION_HANDLE) <= sizeof(void*));

static inline void* CKSessionHandleToVoidPtr(CK_SESSION_HANDLE handle)
{
return (void*)(uintptr_t)handle;
}

#if defined(_MSC_VER)
#pragma warning(pop)
#elif defined(__clang__) || defined(__GNUC__)
#pragma GCC diagnostic pop
#endif

/*
* nssCKFWInstance_CreateSessionHandle
*
Expand Down Expand Up @@ -549,7 +570,7 @@ nssCKFWInstance_CreateSessionHandle(
}

*pError = nssCKFWHash_Add(fwInstance->sessionHandleHash,
(const void *)(uintptr_t)hSession, (const void *)fwSession);
CKSessionHandleToVoidPtr(hSession), (const void *)fwSession);
if (CKR_OK != *pError) {
hSession = (CK_SESSION_HANDLE)0;
goto done;
Expand Down Expand Up @@ -582,7 +603,7 @@ nssCKFWInstance_ResolveSessionHandle(
}

fwSession = (NSSCKFWSession *)nssCKFWHash_Lookup(
fwInstance->sessionHandleHash, (const void *)(uintptr_t)hSession);
fwInstance->sessionHandleHash, CKSessionHandleToVoidPtr(hSession));

/* Assert(hSession == nssCKFWSession_GetHandle(fwSession)) */

Expand Down Expand Up @@ -613,9 +634,9 @@ nssCKFWInstance_DestroySessionHandle(
}

fwSession = (NSSCKFWSession *)nssCKFWHash_Lookup(
fwInstance->sessionHandleHash, (const void *)(uintptr_t)hSession);
fwInstance->sessionHandleHash, CKSessionHandleToVoidPtr(hSession));
if (fwSession) {
nssCKFWHash_Remove(fwInstance->sessionHandleHash, (const void *)(uintptr_t)hSession);
nssCKFWHash_Remove(fwInstance->sessionHandleHash, CKSessionHandleToVoidPtr(hSession));
nssCKFWSession_SetHandle(fwSession, (CK_SESSION_HANDLE)0);
}

Expand Down Expand Up @@ -684,7 +705,7 @@ nssCKFWInstance_CreateObjectHandle(
}

*pError = nssCKFWHash_Add(fwInstance->objectHandleHash,
(const void *)(uintptr_t)hObject, (const void *)fwObject);
CKSessionHandleToVoidPtr(hObject), (const void *)fwObject);
if (CKR_OK != *pError) {
hObject = (CK_OBJECT_HANDLE)0;
goto done;
Expand Down Expand Up @@ -717,7 +738,7 @@ nssCKFWInstance_ResolveObjectHandle(
}

fwObject = (NSSCKFWObject *)nssCKFWHash_Lookup(
fwInstance->objectHandleHash, (const void *)(uintptr_t)hObject);
fwInstance->objectHandleHash, CKSessionHandleToVoidPtr(hObject));

/* Assert(hObject == nssCKFWObject_GetHandle(fwObject)) */

Expand Down Expand Up @@ -751,19 +772,19 @@ nssCKFWInstance_ReassignObjectHandle(
}

oldObject = (NSSCKFWObject *)nssCKFWHash_Lookup(
fwInstance->objectHandleHash, (const void *)(uintptr_t)hObject);
fwInstance->objectHandleHash, CKSessionHandleToVoidPtr(hObject));
if (oldObject) {
/* Assert(hObject == nssCKFWObject_GetHandle(oldObject) */
(void)nssCKFWObject_SetHandle(oldObject, (CK_SESSION_HANDLE)0);
nssCKFWHash_Remove(fwInstance->objectHandleHash, (const void *)(uintptr_t)hObject);
nssCKFWHash_Remove(fwInstance->objectHandleHash, CKSessionHandleToVoidPtr(hObject));
}

error = nssCKFWObject_SetHandle(fwObject, hObject);
if (CKR_OK != error) {
goto done;
}
error = nssCKFWHash_Add(fwInstance->objectHandleHash,
(const void *)(uintptr_t)hObject, (const void *)fwObject);
CKSessionHandleToVoidPtr(hObject), (const void *)fwObject);

done:
(void)nssCKFWMutex_Unlock(fwInstance->mutex);
Expand Down Expand Up @@ -792,10 +813,10 @@ nssCKFWInstance_DestroyObjectHandle(
}

fwObject = (NSSCKFWObject *)nssCKFWHash_Lookup(
fwInstance->objectHandleHash, (const void *)(uintptr_t)hObject);
fwInstance->objectHandleHash, CKSessionHandleToVoidPtr(hObject));
if (fwObject) {
/* Assert(hObject = nssCKFWObject_GetHandle(fwObject)) */
nssCKFWHash_Remove(fwInstance->objectHandleHash, (const void *)(uintptr_t)hObject);
/* Assert(hObject == nssCKFWObject_GetHandle(fwObject)) */
nssCKFWHash_Remove(fwInstance->objectHandleHash, CKSessionHandleToVoidPtr(hObject));
(void)nssCKFWObject_SetHandle(fwObject, (CK_SESSION_HANDLE)0);
}

Expand Down

0 comments on commit 335d294

Please sign in to comment.