mirror of
https://git.planet-casio.com/Vhex-Kernel-Core/fxlibc.git
synced 2024-12-28 04:23:38 +01:00
stdlib: fix saturation in strtol, strtoul, strtoll, strtoull
This commit is contained in:
parent
9de2f5c391
commit
bc53e756a8
2 changed files with 11 additions and 1 deletions
|
@ -15,7 +15,7 @@
|
|||
(depending on use_unsigned). Signedness only affects the range of values
|
||||
that are considered to be ERANGE, and both results are stored in *outl.
|
||||
Similarly, if outll is non-NULL, strto_int produces a long long or unsigned
|
||||
long long result.
|
||||
long long result. Only one pointer should be non-NULL.
|
||||
|
||||
On platforms where long is 32-bit, 64-bit operations are performed only if
|
||||
outll is non-NULL. This is because multiplications with overflow can be
|
||||
|
|
|
@ -96,6 +96,16 @@ int strto_int(char const * restrict ptr, char ** restrict endptr, int base,
|
|||
errno_value = ERANGE;
|
||||
}
|
||||
|
||||
/* Handle overflows */
|
||||
if(outl && errno_value == ERANGE) {
|
||||
if(use_unsigned) xl = ULONG_MAX;
|
||||
else xl = negative ? LONG_MIN : LONG_MAX;
|
||||
}
|
||||
if(outll && errno_value == ERANGE) {
|
||||
if(use_unsigned) xll = ULLONG_MAX;
|
||||
else xll = negative ? LLONG_MIN : LLONG_MAX;
|
||||
}
|
||||
|
||||
if(outl) *outl = xl;
|
||||
if(outll) *outll = xll;
|
||||
if(endptr && valid) *endptr = (char *)ptr;
|
||||
|
|
Loading…
Reference in a new issue