On 2024-07-24 14:07, Alejandro Colomar wrote:

I forgot to reply to the last part: "Why isn't that [current gnulib]
good enough?

With an implementation of strtol(3) that does what I wrote above, the
test `if (!t_ptr)` isn't true, so we go to
`if (*p == nptr && (errno == 0 || errno == EINVAL))`, which is true.

Assuming that the string matches a valid suffix, it'd succeed, but the
call should have failed early.

No, because that code is dealing with the case where the number's text is missing. And if the number's text is missing it doesn't matter what the base is; there are no digits to multiply the base by.

I see that this part of the API isn't documented; it should be. I installed the attached to try to fix that.


> (And the worst part might be suggesting> readers that an invalid base can be successfully tested after a call to
strtol(3), so that they do something similar.)

That's OK; xstrtol.c need not warn about all the problems of strtol. Our readers' time is limited and this issue isn't important enough to consume their time. (In practice, as I wrote earlier, the base is always valid.)
From 2ebf179bf3660b80f2391e69003f9572bb3e52fb Mon Sep 17 00:00:00 2001
From: Paul Eggert <egg...@cs.ucla.edu>
Date: Wed, 24 Jul 2024 17:00:13 -0700
Subject: [PATCH] xstrtol: improve xstrtol.h comment

---
 lib/xstrtol.h | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/lib/xstrtol.h b/lib/xstrtol.h
index 10b48cac4e..1fab748810 100644
--- a/lib/xstrtol.h
+++ b/lib/xstrtol.h
@@ -54,7 +54,9 @@ typedef enum strtol_error strtol_error;
      includes '0' they can be followed by "B" to stand for the usual
      SI powers of 1000 (or by "iB" to stand for powers of 1024 as before).
      Other supported suffixes include 'K' for 1024 or 1000, 'b' for 512,
-     'c' for 1, and 'w' for 2.  */
+     'c' for 1, and 'w' for 2.
+   - Suppose that after the initial whitespace, the number is missing
+     but there is a valid suffix.  Then the number is treated as 1.  */
 
 #define _DECLARE_XSTRTOL(name, type) \
   strtol_error name (char const *restrict /*nptr*/,             \
-- 
2.43.0

Reply via email to