Check for overflow/underflow in parse_signed_integer
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
59c9d8f119
commit
97cb57d521
@ -1,3 +1,4 @@
|
|||||||
|
#include <limits.h>
|
||||||
#include <luna/CType.h>
|
#include <luna/CType.h>
|
||||||
#include <luna/NumberParsing.h>
|
#include <luna/NumberParsing.h>
|
||||||
|
|
||||||
@ -47,6 +48,9 @@ usize parse_unsigned_integer(const char* str, const char** endptr, int base)
|
|||||||
return val;
|
return val;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#define SSIZE_MAX LONG_MAX
|
||||||
|
#define SSIZE_MIN (-SSIZE_MAX - (isize)1)
|
||||||
|
|
||||||
isize parse_signed_integer(const char* str, const char** endptr, int base)
|
isize parse_signed_integer(const char* str, const char** endptr, int base)
|
||||||
{
|
{
|
||||||
bool negative = false;
|
bool negative = false;
|
||||||
@ -59,9 +63,9 @@ isize parse_signed_integer(const char* str, const char** endptr, int base)
|
|||||||
str++;
|
str++;
|
||||||
}
|
}
|
||||||
|
|
||||||
usize rc = parse_unsigned_integer(
|
usize rc = parse_unsigned_integer(str, endptr, base);
|
||||||
str, endptr,
|
|
||||||
base); // FIXME: Check for overflow (the unsigned usize value might not fit into a signed isize)
|
if (rc > SSIZE_MAX) { return negative ? SSIZE_MIN : SSIZE_MAX; }
|
||||||
|
|
||||||
return negative ? -(isize)rc : (isize)rc;
|
return negative ? -(isize)rc : (isize)rc;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user