Add macro functions for checking unsigned wraps
This commit is contained in:
@@ -20,6 +20,9 @@
|
|||||||
#define BH_PTR2INT(x) ((intptr_t)(x))
|
#define BH_PTR2INT(x) ((intptr_t)(x))
|
||||||
#define BH_INT2PTR(x) ((void*)((intptr_t)(x)))
|
#define BH_INT2PTR(x) ((void*)((intptr_t)(x)))
|
||||||
|
|
||||||
|
#define BH_CHECK_UADD_WRAP(x, y, type) ((type)((x) + (y)) < (type)(y))
|
||||||
|
#define BH_CHECK_USUB_WRAP(x, y, type) ((type)((x) - (y)) > (type)(x))
|
||||||
|
#define BH_CHECK_UMUL_WRAP(x, y, type) ((x) > ((type)-1 / (y)))
|
||||||
|
|
||||||
typedef int (*BH_EqualCallback)(const void *, const void *);
|
typedef int (*BH_EqualCallback)(const void *, const void *);
|
||||||
typedef size_t (*BH_HashCallback)(const void *);
|
typedef size_t (*BH_HashCallback)(const void *);
|
||||||
|
|||||||
@@ -62,16 +62,16 @@ static int BH_CalcCapacity(size_t size,
|
|||||||
*threshold = *capacity * factor;
|
*threshold = *capacity * factor;
|
||||||
while (size > *threshold)
|
while (size > *threshold)
|
||||||
{
|
{
|
||||||
|
/* Catch capacity overflow */
|
||||||
|
if (BH_CHECK_UMUL_WRAP(*capacity, 2, size_t))
|
||||||
|
return BH_OOM;
|
||||||
|
|
||||||
*capacity *= 2;
|
*capacity *= 2;
|
||||||
*threshold = *capacity * factor;
|
*threshold = *capacity * factor;
|
||||||
|
|
||||||
/* Catch capacity overflow */
|
|
||||||
if (*capacity < 16)
|
|
||||||
return BH_OOM;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Catch malloc overflow */
|
/* Catch malloc overflow */
|
||||||
if (*capacity >= ((size_t)-1) / sizeof(BH_HashmapNode))
|
if (BH_CHECK_UMUL_WRAP(*capacity, sizeof(BH_HashmapNode), size_t))
|
||||||
return BH_OOM;
|
return BH_OOM;
|
||||||
|
|
||||||
return BH_OK;
|
return BH_OK;
|
||||||
|
|||||||
@@ -78,7 +78,7 @@ int BH_QueueReserve(BH_Queue *queue,
|
|||||||
size = queue->size;
|
size = queue->size;
|
||||||
|
|
||||||
/* Catch malloc overflow */
|
/* Catch malloc overflow */
|
||||||
if (size >= ((size_t)-1) / sizeof(void *))
|
if (BH_CHECK_UMUL_WRAP(size, sizeof(void *), size_t))
|
||||||
return BH_OOM;
|
return BH_OOM;
|
||||||
|
|
||||||
/* Prevent same size memory reallocation */
|
/* Prevent same size memory reallocation */
|
||||||
|
|||||||
54
test/src/TestCommon.c
Normal file
54
test/src/TestCommon.c
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
#include <BH/Unit.h>
|
||||||
|
#include <BH/Common.h>
|
||||||
|
|
||||||
|
|
||||||
|
BH_UNIT_TEST(UADD)
|
||||||
|
{
|
||||||
|
BH_VERIFY(!BH_CHECK_UADD_WRAP(100, 100, uint8_t));
|
||||||
|
BH_VERIFY(!BH_CHECK_UADD_WRAP(0, 0, uint8_t));
|
||||||
|
BH_VERIFY(BH_CHECK_UADD_WRAP(200, 100, uint8_t));
|
||||||
|
BH_VERIFY(BH_CHECK_UADD_WRAP(100, 200, uint8_t));
|
||||||
|
BH_VERIFY(BH_CHECK_UADD_WRAP(255, 1, uint8_t));
|
||||||
|
BH_VERIFY(BH_CHECK_UADD_WRAP(1, 255, uint8_t));
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
BH_UNIT_TEST(USUB)
|
||||||
|
{
|
||||||
|
BH_VERIFY(!BH_CHECK_USUB_WRAP(100, 100, uint8_t));
|
||||||
|
BH_VERIFY(!BH_CHECK_USUB_WRAP(0, 0, uint8_t));
|
||||||
|
BH_VERIFY(!BH_CHECK_USUB_WRAP(200, 100, uint8_t));
|
||||||
|
BH_VERIFY(BH_CHECK_USUB_WRAP(100, 200, uint8_t));
|
||||||
|
BH_VERIFY(!BH_CHECK_USUB_WRAP(255, 1, uint8_t));
|
||||||
|
BH_VERIFY(BH_CHECK_USUB_WRAP(1, 255, uint8_t));
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
BH_UNIT_TEST(UMUL)
|
||||||
|
{
|
||||||
|
BH_VERIFY(!BH_CHECK_UMUL_WRAP(0, 2, uint8_t));
|
||||||
|
BH_VERIFY(!BH_CHECK_UMUL_WRAP(100, 2, uint8_t));
|
||||||
|
BH_VERIFY(BH_CHECK_UMUL_WRAP(200, 100, uint8_t));
|
||||||
|
BH_VERIFY(BH_CHECK_UMUL_WRAP(100, 200, uint8_t));
|
||||||
|
BH_VERIFY(!BH_CHECK_UMUL_WRAP(255, 1, uint8_t));
|
||||||
|
BH_VERIFY(!BH_CHECK_UMUL_WRAP(1, 255, uint8_t));
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
BH_UNUSED(argc);
|
||||||
|
BH_UNUSED(argv);
|
||||||
|
|
||||||
|
BH_UNIT_ADD(UADD);
|
||||||
|
BH_UNIT_ADD(USUB);
|
||||||
|
BH_UNIT_ADD(UMUL);
|
||||||
|
|
||||||
|
return BH_UnitRun();
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user