+2024-07-19 Paul Eggert <eggert@cs.ucla.edu>
+
+ xstrtol: be more robust against odd failures
+ * lib/xstrtol.c (__xstrtol): Don’t update *endptr if strtol doesn’t.
+ Also, if the underlying strtol gives an unusual error number and
+ sets *endpnr = nptr, assume that’s an error not a missing number.
+ Problems reported by Alejandro Colomar in:
+ https://lists.gnu.org/r/bug-gnulib/2024-07/msg00175.html
+ https://lists.gnu.org/r/bug-gnulib/2024-07/msg00176.html
+ * modules/xstrtol (Depends-on): Add nullptr.
+
2024-07-19 Bruno Haible <bruno@clisp.org>
doc: Mention <math.h> function that were added in ISO C23.
__xstrtol (char const *nptr, char **endptr, int base,
__strtol_t *val, char const *valid_suffixes)
{
- char *t_ptr;
+ char *t_ptr = nullptr;
char **p = endptr ? endptr : &t_ptr;
- *p = (char *) nptr;
if (! TYPE_SIGNED (__strtol_t))
{
while (isspace (ch))
ch = *++q;
if (ch == '-')
- return LONGINT_INVALID;
+ {
+ *p = (char *) nptr;
+ return LONGINT_INVALID;
+ }
}
errno = 0;
- __strtol_t tmp = __strtol (nptr, p, base);
+ __strtol_t tmp = __strtol (nptr, &t_ptr, base);
+ if (!t_ptr)
+ return LONGINT_INVALID;
+ *p = t_ptr;
strtol_error err = LONGINT_OK;
- if (*p == nptr)
+ if (*p == nptr && (errno == 0 || errno == EINVAL))
{
/* If there is no number but there is a valid suffix, assume the
number is 1. The string is invalid otherwise. */