Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/bit_manip.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ static M_INLINE size_t gen_bit_width(uint8_t msb, uint8_t lsb)

static M_INLINE uint64_t gen_safe_mask_u64(size_t width)
{
/* Check not needed here as this function is only called after validating width against GENERIC_WIDTH_64 by the functions which call it.
if (width >= GENERIC_WIDTH_64)
{
return UINT64_MAX;
Expand All @@ -161,12 +162,14 @@ static M_INLINE uint64_t gen_safe_mask_u64(size_t width)
{
return UINT64_C(0);
}
*/

return (UINT64_C(1) << width) - UINT64_C(1);
}

static M_INLINE uint32_t gen_safe_mask_u32(size_t width)
{
/* Check not needed here as this function is only called after validating width against GENERIC_WIDTH_32 by the functions which call it.
if (width >= GENERIC_WIDTH_32)
{
return UINT32_MAX;
Expand All @@ -176,12 +179,14 @@ static M_INLINE uint32_t gen_safe_mask_u32(size_t width)
{
return UINT32_C(0);
}
*/

return (UINT32_C(1) << width) - UINT32_C(1);
}

static M_INLINE uint16_t gen_safe_mask_u16(size_t width)
{
/* Check not needed here as this function is only called after validating width against GENERIC_WIDTH_16 by the functions which call it.
if (width >= GENERIC_WIDTH_16)
{
return UINT16_MAX;
Expand All @@ -191,12 +196,14 @@ static M_INLINE uint16_t gen_safe_mask_u16(size_t width)
{
return UINT16_C(0);
}
*/

return M_STATIC_CAST(uint16_t, (UINT32_C(1) << width) - UINT32_C(1));
}

static M_INLINE uint8_t gen_safe_mask_u8(size_t width)
{
/* Check not needed here as this function is only called after validating width against GENERIC_WIDTH_8 by the functions which call it.
if (width >= GENERIC_WIDTH_8)
{
return UINT8_MAX;
Expand All @@ -206,6 +213,7 @@ static M_INLINE uint8_t gen_safe_mask_u8(size_t width)
{
return UINT8_C(0);
}
*/

return M_STATIC_CAST(uint8_t, (UINT32_C(1) << width) - UINT32_C(1));
}
Expand All @@ -214,20 +222,24 @@ static M_INLINE uint64_t gen_extract_u64(uint64_t val, uint8_t msb, uint8_t lsb)
{
size_t width = gen_bit_width(msb, lsb);

/* Check not needed here as this function is only called after validating the condition by the functions which call it.
if (width == GENERIC_WIDTH_0)
{
return UINT64_C(0);
}
*/

if (width >= GENERIC_WIDTH_64 && lsb == 0)
{
return val;
}

/* Check not needed here as this function is only called after validating the condition by the functions which call it.
if (lsb >= GENERIC_WIDTH_64)
{
return UINT64_C(0);
}
*/

return M_STATIC_CAST(uint64_t, (val >> lsb) & gen_safe_mask_u64(width));
}
Expand All @@ -236,20 +248,24 @@ static M_INLINE uint32_t gen_extract_u32(uint32_t val, uint8_t msb, uint8_t lsb)
{
size_t width = gen_bit_width(msb, lsb);

/* Check not needed here as this function is only called after validating the condition by the functions which call it.
if (width == GENERIC_WIDTH_0)
{
return UINT32_C(0);
}
*/

if (width >= GENERIC_WIDTH_32 && lsb == 0)
{
return val;
}

/* Check not needed here as this function is only called after validating the condition by the functions which call it.
if (lsb >= GENERIC_WIDTH_32)
{
return UINT32_C(0);
}
*/

return M_STATIC_CAST(uint32_t, (val >> lsb) & gen_safe_mask_u32(width));
}
Expand All @@ -258,20 +274,24 @@ static M_INLINE uint16_t gen_extract_u16(uint16_t val, uint8_t msb, uint8_t lsb)
{
size_t width = gen_bit_width(msb, lsb);

/* Check not needed here as this function is only called after validating the condition by the functions which call it.
if (width == GENERIC_WIDTH_0)
{
return M_STATIC_CAST(uint16_t, UINT32_C(0));
}
*/

if (width >= GENERIC_WIDTH_16 && lsb == 0)
{
return val;
}

/* Check not needed here as this function is only called after validating the condition by the functions which call it.
if (lsb >= GENERIC_WIDTH_16)
{
return M_STATIC_CAST(uint16_t, UINT32_C(0));
}
*/

return M_STATIC_CAST(uint16_t, (val >> lsb) & gen_safe_mask_u16(width));
}
Expand All @@ -280,20 +300,24 @@ static M_INLINE uint8_t gen_extract_u8(uint8_t val, uint8_t msb, uint8_t lsb)
{
size_t width = gen_bit_width(msb, lsb);

/* Check not needed here as this function is only called after validating the condition by the functions which call it.
if (width == GENERIC_WIDTH_0)
{
return M_STATIC_CAST(uint8_t, UINT32_C(0));
}
*/

if (width >= GENERIC_WIDTH_8 && lsb == 0)
{
return val;
}

/* Check not needed here as this function is only called after validating the condition by the functions which call it.
if (lsb >= GENERIC_WIDTH_8)
{
return M_STATIC_CAST(uint8_t, UINT32_C(0));
}
*/

return M_STATIC_CAST(uint8_t, (val >> lsb) & gen_safe_mask_u8(width));
}
Expand Down
13 changes: 3 additions & 10 deletions src/memory_safety.c
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ M_NODISCARD M_PARAM_RW(1) M_MALLOC_SIZE(2) void* safe_reallocf(void** block, siz
else
{
void* newblock = realloc(*block, size);
if (newblock == M_NULLPTR && size != SIZE_T_C(0))
if (newblock == M_NULLPTR)
{
free(*block);
*block = M_NULLPTR;
Expand Down Expand Up @@ -729,14 +729,7 @@ void* safe_realloc_aligned(void* block, size_t originalSize, size_t size, size_t
// than a simple return realloc, the purpose of this is to help reduce
// false positives with SAST tools.
void* newblock = realloc_aligned(block, originalSize, size, alignment);
if (newblock == M_NULLPTR)
{
return M_NULLPTR;
}
else
{
return newblock;
}
return newblock;
}
}

Expand Down Expand Up @@ -770,7 +763,7 @@ void* safe_reallocf_aligned(void** block, size_t originalSize, size_t size, size
alignment = alignment_Round_Up(alignment);
size = aligned_Size_Round_Up(size, alignment);
void* newblock = realloc_aligned(*block, originalSize, size, alignment);
if (newblock == M_NULLPTR && *block && size != SIZE_T_C(0))
if (newblock == M_NULLPTR && *block)
{
free_aligned(*block);
*block = M_NULLPTR;
Expand Down