Set errno when out of range in strtoimax (#111)

This commit is contained in:
Alison Winters 2021-03-07 14:38:56 -08:00 committed by GitHub
parent f5da4efcaf
commit 8a6ac6dd63
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 29 additions and 5 deletions

View file

@ -17,6 +17,7 @@
PERFORMANCE OF THIS SOFTWARE.
*/
#include "libc/fmt/conv.h"
#include "libc/errno.h"
#include "libc/limits.h"
#include "libc/nexgen32e/bsr.h"
#include "libc/str/str.h"
@ -85,7 +86,7 @@ intmax_t strtoimax(const char *s, char **endptr, int base) {
diglet = kBase36[*s & 0xff];
if (!diglet || diglet > base) break;
diglet -= 1;
if (!diglet || !x || (bits = bsr(diglet) + bsrmax(x)) < 127) {
if (!x || (bits = (diglet ? bsr(diglet) : 0) + bsrmax(x * base)) < 127) {
s++;
x *= base;
x += diglet;
@ -96,9 +97,11 @@ intmax_t strtoimax(const char *s, char **endptr, int base) {
if (x == INTMAX_MIN) s++;
}
x = INTMAX_MIN;
errno = ERANGE;
break;
} else {
x = INTMAX_MAX;
errno = ERANGE;
break;
}
}