stdlib: fix saturation in strtol, strtoul, strtoll, strtoull

This commit is contained in:
Lephenixnoir 2021-05-20 15:38:37 +02:00
parent 9de2f5c391
commit bc53e756a8
No known key found for this signature in database
GPG key ID: 1BBA026E13FC0495
2 changed files with 11 additions and 1 deletions

View file

@ -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

View file

@ -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;