From: Paul Eggert Date: Fri, 26 Jul 2024 00:24:20 +0000 (-0700) Subject: xstrtol: remove the base-checking changes X-Git-Url: https://gitweb.git.savannah.gnu.org/gitweb/?a=commitdiff_plain;h=bd1e981434c98751b1106a1744e77a27317b52b3;p=gnulib.git xstrtol: remove the base-checking changes * lib/xstrtol.c (__xstrtol): Stop worrying about hypothetical implementations that are causing more confusion than the code is worth. Instead, go back more to old way of doing things. None of this matters for practical applications. * lib/xstrtol.h: Document that behavior is undefined if the base is negative, 1, or greater than 36. * modules/xstrtol (Depends-on): Remove nullptr; no longer needed. --- diff --git a/ChangeLog b/ChangeLog index 21b1840dec..0c35378606 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,14 @@ +2024-07-25 Paul Eggert + + xstrtol: remove the base-checking changes + * lib/xstrtol.c (__xstrtol): Stop worrying about hypothetical + implementations that are causing more confusion than the code is + worth. Instead, go back more to old way of doing things. + None of this matters for practical applications. + * lib/xstrtol.h: Document that behavior is undefined + if the base is negative, 1, or greater than 36. + * modules/xstrtol (Depends-on): Remove nullptr; no longer needed. + 2024-07-25 Bruno Haible xstrtol, xstrtoll tests: Avoid test failure on FreeBSD. diff --git a/lib/xstrtol.c b/lib/xstrtol.c index 797d3e4dee..4e7c0fcfda 100644 --- a/lib/xstrtol.c +++ b/lib/xstrtol.c @@ -71,7 +71,7 @@ strtol_error __xstrtol (char const *nptr, char **endptr, int base, __strtol_t *val, char const *valid_suffixes) { - char *t_ptr = nullptr; + char *t_ptr; char **p = endptr ? endptr : &t_ptr; if (! TYPE_SIGNED (__strtol_t)) @@ -88,13 +88,10 @@ __xstrtol (char const *nptr, char **endptr, int base, } errno = 0; - __strtol_t tmp = __strtol (nptr, &t_ptr, base); - if (!t_ptr) - return LONGINT_INVALID; - *p = t_ptr; + __strtol_t tmp = __strtol (nptr, p, base); strtol_error err = LONGINT_OK; - if (*p == nptr && (errno == 0 || errno == EINVAL)) + if (*p == nptr) { /* If there is no number but there is a valid suffix, assume the number is 1. The string is invalid otherwise. */ diff --git a/lib/xstrtol.h b/lib/xstrtol.h index 1fab748810..45c8921578 100644 --- a/lib/xstrtol.h +++ b/lib/xstrtol.h @@ -47,6 +47,8 @@ typedef enum strtol_error strtol_error; - Return strtol_error, and store any result through an additional TYPE *VAL pointer instead of returning the result. - If TYPE is unsigned, reject leading '-'. + - Behavior is undefined if BASE is negative, 1, or greater than 36. + (In this respect xstrtol acts like the C standard, not like POSIX.) - Accept an additional char const *VALID_SUFFIXES pointer to a possibly-empty string containing allowed numeric suffixes, which multiply the value. These include SI suffixes like 'k' and 'M'; diff --git a/modules/xstrtol b/modules/xstrtol index 1f8a28fa6c..ef49dfc357 100644 --- a/modules/xstrtol +++ b/modules/xstrtol @@ -9,7 +9,6 @@ m4/xstrtol.m4 Depends-on: intprops -nullptr stdckdint stdint