mirror of
https://github.com/gbdev/rgbds.git
synced 2025-11-20 10:12:06 +00:00
Fix use of zero-allocated memory
It's possible that the unsigned integer may overflow to zero,
and then we might use zero-allocated memory.
This is incredibly unlikely, and I would even go so far as to say
that this is a false positive. Fix it anyway, to silence this warning:
src/link/patch.c:92:24: warning: Use of zero-allocated memory
stack.buf[stack.size] = value;
~~~~~~~~~~~~~~~~~~~~~ ^
Deal with overflow, and check for zero to get rid of the warning.
Signed-off-by: JL2210 <larrowe.semaj11@gmail.com>
This commit is contained in:
@@ -82,10 +82,18 @@ static inline void clearRPNStack(void)
|
||||
static void pushRPN(int32_t value)
|
||||
{
|
||||
if (stack.size >= stack.capacity) {
|
||||
stack.capacity *= 2;
|
||||
static const size_t increase_factor = 2;
|
||||
|
||||
if (stack.capacity > SIZE_MAX / increase_factor)
|
||||
err(1, "Overflow in RPN stack resize");
|
||||
|
||||
stack.capacity *= increase_factor;
|
||||
stack.buf =
|
||||
realloc(stack.buf, sizeof(*stack.buf) * stack.capacity);
|
||||
if (!stack.buf)
|
||||
// || !stack.capacity to fix bogus
|
||||
// zero-size allocation warning from
|
||||
// scan-build, already caught above
|
||||
if (!stack.buf || !stack.capacity)
|
||||
err(1, "Failed to resize RPN stack");
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user