]> Savannah Git Hosting - gnulib.git/commitdiff
xstrtol: be more robust against odd failures
authorPaul Eggert <eggert@cs.ucla.edu>
Fri, 19 Jul 2024 17:39:58 +0000 (10:39 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Fri, 19 Jul 2024 17:41:43 +0000 (10:41 -0700)
* 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.

ChangeLog
lib/xstrtol.c
modules/xstrtol

index 072a28855dea0ecd8bb73f5657222994ac9ba120..4e88d446a1ae7b16b6e1713ff3e9f556b53b4cd8 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+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.
index c3145171f37587df53bf68dd9fd8f0f42f0cfefa..797d3e4deef955269778e91a7dd807b63c0b3f08 100644 (file)
@@ -71,9 +71,8 @@ strtol_error
 __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))
     {
@@ -82,14 +81,20 @@ __xstrtol (char const *nptr, char **endptr, int base,
       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.  */
index ef49dfc357b1f215d1985dfdd0ad2917ac51e126..1f8a28fa6c8ce910ede6136974d427ea14ec787d 100644 (file)
@@ -9,6 +9,7 @@ m4/xstrtol.m4
 
 Depends-on:
 intprops
+nullptr
 stdckdint
 stdint