Re: [PATCH v1] xstrtol: 1 is not a valid base

2024-07-19 Thread Alejandro Colomar
Hi Paul,

On Thu, Jul 18, 2024 at 05:37:26PM GMT, Paul Eggert wrote:
> On 2024-07-18 12:53, Alejandro Colomar wrote:
> > I think it'd be common to assume that unless specifically
> > documented, you behave like POSIX's strtol(3), which produces defined
> > behavior for a base of 1.
> 
> Fair enough. I installed the attached Gnulib patch which addresses that
> point, along with the other points you raised about xstrtol that I
> understood.

Thanks!  I think the patch is good.  I think I would have split it in
several patches, since it does a bit too much, but that's your
prerogative.  ;)

I still think there's one issue in the function, which I reported in a
different thread, so I'll discuss it there.


> Since the system strtol can support base 1 as an extension,
> xstrtol now does whatever the system does so there's no need for the
> 'assume'.

Agree.

> (Not that any Gnulib-using programs care) (Oh, and by the way,
> I don't think I put that 'assume'/'assert'/whatever code in there decades
> ago as I'm not a fan of that sort of thing)

My git-blame(1) session pointed at

commit 034a18049cbcc4b370052cb1fa28a81000a10849
Author: Paul Eggert 
Date:   Sat Dec 20 13:00:21 2014 -0800

assure: new module

> PS. Sorry about missing a space in the ChangeLog entry - I fixed that in a
> followup patch.

No problem; typos happen.  Thanks!

Have a lovely day!
Alex


-- 



signature.asc
Description: PGP signature


[PATCH v1 0/2] xstrtol() fixes

2024-07-19 Thread Alejandro Colomar
Hi Paul,

This patch set --hopefully; writing calls to strtol(3) is a PITA-- fixes
the remaining issues I've observed in xstrtol().

Have a lovely day!
Alex

Alejandro Colomar (2):
  xstrtol: Correctly handle an invalid base
  xstrtol: Add defensive check against undocumented errors

 lib/xstrtol.c | 35 +--
 1 file changed, 21 insertions(+), 14 deletions(-)

Range-diff against v0:
-:  -- > 1:  9345449d79 xstrtol: Correctly handle an invalid base
-:  -- > 2:  d1836c207a xstrtol: Add defensive check against 
undocumented errors
-- 
2.45.2



signature.asc
Description: PGP signature


[PATCH v1 1/2] xstrtol: Correctly handle an invalid base

2024-07-19 Thread Alejandro Colomar
strtol(3) doesn't set the end pointer if the base is invalid.  This
allows a caller to differentiate between "invalid base" (what
strtoi(3bsd) calls EINVAL) and an "no digits seen" (what strtoi(3bsd)
calls ECANCELED) in systems that report EINVAL on no digits seen (POSIX
allows this).

strtol("foo", &e, 0);
strtol("0", &e, -1);

The former call will set e = nptr.
The latter will leave e untouched.

The caller has no other way to portably differentiate the calls.

The way to differentiate those, thus, is to initialize e = NULL, to
allow reading it after the call.

While doing this, change the behavior of this function to only set
*endptr if strtol(3) has set it, leaving it untouched otherwise.

Fixes: 034a18049cbc (2014-12-20, "assure: new module")
Fixes: 64ddc975e72c (2024-07-18, "xstrtol: document and stray less from strtol")
Cc: Paul Eggert 
Cc: Bruno Haible 
Cc: Đoàn Trần Công Danh 
Cc: Eli Schwartz 
Cc: Sam James 
Cc: Serge Hallyn 
Cc: Iker Pedrosa 
Cc: Michael Vetter 
Cc: 
Signed-off-by: Alejandro Colomar 
---
 lib/xstrtol.c | 35 +--
 1 file changed, 21 insertions(+), 14 deletions(-)

diff --git a/lib/xstrtol.c b/lib/xstrtol.c
index c3145171f3..592673557f 100644
--- a/lib/xstrtol.c
+++ b/lib/xstrtol.c
@@ -71,9 +71,7 @@ strtol_error
 __xstrtol (char const *nptr, char **endptr, int base,
__strtol_t *val, char const *valid_suffixes)
 {
-  char *t_ptr;
-  char **p = endptr ? endptr : &t_ptr;
-  *p = (char *) nptr;
+  char *e = NULL;
 
   if (! TYPE_SIGNED (__strtol_t))
 {
@@ -82,14 +80,21 @@ __xstrtol (char const *nptr, char **endptr, int base,
   while (isspace (ch))
 ch = *++q;
   if (ch == '-')
-return LONGINT_INVALID;
+{
+  if (endptr)
+*endptr = (char *) nptr;
+  return LONGINT_INVALID;
+}
 }
 
   errno = 0;
-  __strtol_t tmp = __strtol (nptr, p, base);
+  __strtol_t tmp = __strtol (nptr, &e, base);
   strtol_error err = LONGINT_OK;
 
-  if (*p == nptr)
+  if (endptr && e)
+*endptr = e;
+
+  if (e == nptr)
 {
   /* If there is no number but there is a valid suffix, assume the
  number is 1.  The string is invalid otherwise.  */
@@ -113,19 +118,19 @@ __xstrtol (char const *nptr, char **endptr, int base,
   return err;
 }
 
-  if (**p != '\0')
+  if (*e != '\0')
 {
   int xbase = 1024;
   int suffixes = 1;
   strtol_error overflow;
 
-  if (!strchr (valid_suffixes, **p))
+  if (!strchr (valid_suffixes, *e))
 {
   *val = tmp;
   return err | LONGINT_INVALID_SUFFIX_CHAR;
 }
 
-  switch (**p)
+  switch (*e)
 {
 case 'E': case 'G': case 'g': case 'k': case 'K': case 'M': case 'm':
 case 'P': case 'Q': case 'R': case 'T': case 't': case 'Y': case 'Z':
@@ -138,10 +143,10 @@ __xstrtol (char const *nptr, char **endptr, int base,
  power-of-1024.  */
 
   if (strchr (valid_suffixes, '0'))
-switch (p[0][1])
+switch (e[1])
   {
   case 'i':
-if (p[0][2] == 'B')
+if (e[2] == 'B')
   suffixes += 2;
 break;
 
@@ -153,7 +158,7 @@ __xstrtol (char const *nptr, char **endptr, int base,
   }
 }
 
-  switch (**p)
+  switch (*e)
 {
 case 'b':
   overflow = bkm_scale (&tmp, 512);
@@ -224,8 +229,10 @@ __xstrtol (char const *nptr, char **endptr, int base,
 }
 
   err |= overflow;
-  *p += suffixes;
-  if (**p)
+  e += suffixes;
+  if (endptr)
+*endptr = e;
+  if (*e)
 err |= LONGINT_INVALID_SUFFIX_CHAR;
 }
 
-- 
2.45.2



signature.asc
Description: PGP signature


[PATCH v1 2/2] xstrtol: Add defensive check against undocumented errors

2024-07-19 Thread Alejandro Colomar
strtod(3) calls malloc(3) in some systems.  While strtol(3) doesn't,
let's be cautious and write code that would be safe under a theoretical
implementation of strtol(3) that could ENOMEM (and let's assume we don't
know what 'e' will look like after such an error).

Some attempt of defensive error handling had been added in 790855e18a1d,
but it was wrong, since it was assuming e!=nptr on such a case, which is
not a reasonable assumption.

Fixes: 790855e18a1d (2003-10-14, "Handle invalid suffixes and overflow 
independently, so that ...")
Cc: Paul Eggert 
Cc: Bruno Haible 
Cc: Đoàn Trần Công Danh 
Cc: Eli Schwartz 
Cc: Sam James 
Cc: Serge Hallyn 
Cc: Iker Pedrosa 
Cc: Michael Vetter 
Cc: 
Signed-off-by: Alejandro Colomar 
---
 lib/xstrtol.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/xstrtol.c b/lib/xstrtol.c
index 592673557f..faa7b9fd4c 100644
--- a/lib/xstrtol.c
+++ b/lib/xstrtol.c
@@ -94,7 +94,7 @@ __xstrtol (char const *nptr, char **endptr, int base,
   if (endptr && e)
 *endptr = e;
 
-  if (e == nptr)
+  if (e == 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.  */
-- 
2.45.2



signature.asc
Description: PGP signature


Re: [PATCH v1 1/2] xstrtol: Correctly handle an invalid base

2024-07-19 Thread Alejandro Colomar
Hi Paul,

On Fri, Jul 19, 2024 at 02:53:38PM GMT, Alejandro Colomar wrote:
> strtol(3) doesn't set the end pointer if the base is invalid.  This
> allows a caller to differentiate between "invalid base" (what
> strtoi(3bsd) calls EINVAL) and an "no digits seen" (what strtoi(3bsd)
> calls ECANCELED) in systems that report EINVAL on no digits seen (POSIX
> allows this).
> 
>   strtol("foo", &e, 0);
>   strtol("0", &e, -1);
> 
> The former call will set e = nptr.
> The latter will leave e untouched.
> 
> The caller has no other way to portably differentiate the calls.
> 
> The way to differentiate those, thus, is to initialize e = NULL, to
> allow reading it after the call.
> 
> While doing this, change the behavior of this function to only set
> *endptr if strtol(3) has set it, leaving it untouched otherwise.
> 
> Fixes: 034a18049cbc (2014-12-20, "assure: new module")

Self-correction: I had a typo while running git-blame(1), which led me
to not find that the assure() call already existed --as assert()--
before your commit, Paul.

It was actually introduced in 3af47ca3f67 (1994-12-20, ".").

Please amend the Fixes tags to:

Fixes: 3af47ca3f67 (1994-12-20, ".")
Fixes: 64ddc975e72c (2024-07-18, "xstrtol: document and stray less from 
strtol")

Sorry for the confusion!

Cheers,
Alex

> Fixes: 64ddc975e72c (2024-07-18, "xstrtol: document and stray less from 
> strtol")
> Cc: Paul Eggert 
> Cc: Bruno Haible 
> Cc: Đoàn Trần Công Danh 
> Cc: Eli Schwartz 
> Cc: Sam James 
> Cc: Serge Hallyn 
> Cc: Iker Pedrosa 
> Cc: Michael Vetter 
> Cc: 
> Signed-off-by: Alejandro Colomar 
> ---
>  lib/xstrtol.c | 35 +--
>  1 file changed, 21 insertions(+), 14 deletions(-)
> 
> diff --git a/lib/xstrtol.c b/lib/xstrtol.c
> index c3145171f3..592673557f 100644
> --- a/lib/xstrtol.c
> +++ b/lib/xstrtol.c
> @@ -71,9 +71,7 @@ strtol_error
>  __xstrtol (char const *nptr, char **endptr, int base,
> __strtol_t *val, char const *valid_suffixes)
>  {
> -  char *t_ptr;
> -  char **p = endptr ? endptr : &t_ptr;
> -  *p = (char *) nptr;
> +  char *e = NULL;
>  
>if (! TYPE_SIGNED (__strtol_t))
>  {
> @@ -82,14 +80,21 @@ __xstrtol (char const *nptr, char **endptr, int base,
>while (isspace (ch))
>  ch = *++q;
>if (ch == '-')
> -return LONGINT_INVALID;
> +{
> +  if (endptr)
> +*endptr = (char *) nptr;
> +  return LONGINT_INVALID;
> +}
>  }
>  
>errno = 0;
> -  __strtol_t tmp = __strtol (nptr, p, base);
> +  __strtol_t tmp = __strtol (nptr, &e, base);
>strtol_error err = LONGINT_OK;
>  
> -  if (*p == nptr)
> +  if (endptr && e)
> +*endptr = e;
> +
> +  if (e == nptr)
>  {
>/* If there is no number but there is a valid suffix, assume the
>   number is 1.  The string is invalid otherwise.  */
> @@ -113,19 +118,19 @@ __xstrtol (char const *nptr, char **endptr, int base,
>return err;
>  }
>  
> -  if (**p != '\0')
> +  if (*e != '\0')
>  {
>int xbase = 1024;
>int suffixes = 1;
>strtol_error overflow;
>  
> -  if (!strchr (valid_suffixes, **p))
> +  if (!strchr (valid_suffixes, *e))
>  {
>*val = tmp;
>return err | LONGINT_INVALID_SUFFIX_CHAR;
>  }
>  
> -  switch (**p)
> +  switch (*e)
>  {
>  case 'E': case 'G': case 'g': case 'k': case 'K': case 'M': case 'm':
>  case 'P': case 'Q': case 'R': case 'T': case 't': case 'Y': case 'Z':
> @@ -138,10 +143,10 @@ __xstrtol (char const *nptr, char **endptr, int base,
>   power-of-1024.  */
>  
>if (strchr (valid_suffixes, '0'))
> -switch (p[0][1])
> +switch (e[1])
>{
>case 'i':
> -if (p[0][2] == 'B')
> +if (e[2] == 'B')
>suffixes += 2;
>  break;
>  
> @@ -153,7 +158,7 @@ __xstrtol (char const *nptr, char **endptr, int base,
>}
>  }
>  
> -  switch (**p)
> +  switch (*e)
>  {
>  case 'b':
>overflow = bkm_scale (&tmp, 512);
> @@ -224,8 +229,10 @@ __xstrtol (char const *nptr, char **endptr, int base,
>  }
>  
>err |= overflow;
> -  *p += suffixes;
> -  if (**p)
> +  e += suffixes;
> +  if (endptr)
> +*endptr = e;
> +  if (*e)
>  err |= LONGINT_INVALID_SUFFIX_CHAR;
>  }
>  
> -- 
> 2.45.2
> 



-- 



signature.asc
Description: PGP signature


Re: [PATCH v1 1/2] xstrtol: Correctly handle an invalid base

2024-07-19 Thread Alejandro Colomar
On Fri, Jul 19, 2024 at 03:09:52PM GMT, Alejandro Colomar wrote:
> Hi Paul,
> 
> On Fri, Jul 19, 2024 at 02:53:38PM GMT, Alejandro Colomar wrote:
> > strtol(3) doesn't set the end pointer if the base is invalid.  This
> > allows a caller to differentiate between "invalid base" (what
> > strtoi(3bsd) calls EINVAL) and an "no digits seen" (what strtoi(3bsd)
> > calls ECANCELED) in systems that report EINVAL on no digits seen (POSIX
> > allows this).
> > 
> > strtol("foo", &e, 0);
> > strtol("0", &e, -1);
> > 
> > The former call will set e = nptr.
> > The latter will leave e untouched.
> > 
> > The caller has no other way to portably differentiate the calls.
> > 
> > The way to differentiate those, thus, is to initialize e = NULL, to
> > allow reading it after the call.
> > 
> > While doing this, change the behavior of this function to only set
> > *endptr if strtol(3) has set it, leaving it untouched otherwise.
> > 
> > Fixes: 034a18049cbc (2014-12-20, "assure: new module")
> 
> Self-correction: I had a typo while running git-blame(1), which led me
> to not find that the assure() call already existed --as assert()--
> before your commit, Paul.
> 
> It was actually introduced in 3af47ca3f67 (1994-12-20, ".").
> 
> Please amend the Fixes tags to:
> 
>   Fixes: 3af47ca3f67 (1994-12-20, ".")

Oops; For consistency, use 12 chars:

3af47ca3f672

>   Fixes: 64ddc975e72c (2024-07-18, "xstrtol: document and stray less from 
> strtol")
> 
> Sorry for the confusion!
> 
> Cheers,
> Alex
> 
> > Fixes: 64ddc975e72c (2024-07-18, "xstrtol: document and stray less from 
> > strtol")
> > Cc: Paul Eggert 
> > Cc: Bruno Haible 
> > Cc: Đoàn Trần Công Danh 
> > Cc: Eli Schwartz 
> > Cc: Sam James 
> > Cc: Serge Hallyn 
> > Cc: Iker Pedrosa 
> > Cc: Michael Vetter 
> > Cc: 
> > Signed-off-by: Alejandro Colomar 
> > ---
> >  lib/xstrtol.c | 35 +--
> >  1 file changed, 21 insertions(+), 14 deletions(-)
> > 
> > diff --git a/lib/xstrtol.c b/lib/xstrtol.c
> > index c3145171f3..592673557f 100644
> > --- a/lib/xstrtol.c
> > +++ b/lib/xstrtol.c
> > @@ -71,9 +71,7 @@ strtol_error
> >  __xstrtol (char const *nptr, char **endptr, int base,
> > __strtol_t *val, char const *valid_suffixes)
> >  {
> > -  char *t_ptr;
> > -  char **p = endptr ? endptr : &t_ptr;
> > -  *p = (char *) nptr;
> > +  char *e = NULL;
> >  
> >if (! TYPE_SIGNED (__strtol_t))
> >  {
> > @@ -82,14 +80,21 @@ __xstrtol (char const *nptr, char **endptr, int base,
> >while (isspace (ch))
> >  ch = *++q;
> >if (ch == '-')
> > -return LONGINT_INVALID;
> > +{
> > +  if (endptr)
> > +*endptr = (char *) nptr;
> > +  return LONGINT_INVALID;
> > +}
> >  }
> >  
> >errno = 0;
> > -  __strtol_t tmp = __strtol (nptr, p, base);
> > +  __strtol_t tmp = __strtol (nptr, &e, base);
> >strtol_error err = LONGINT_OK;
> >  
> > -  if (*p == nptr)
> > +  if (endptr && e)
> > +*endptr = e;
> > +
> > +  if (e == nptr)
> >  {
> >/* If there is no number but there is a valid suffix, assume the
> >   number is 1.  The string is invalid otherwise.  */
> > @@ -113,19 +118,19 @@ __xstrtol (char const *nptr, char **endptr, int base,
> >return err;
> >  }
> >  
> > -  if (**p != '\0')
> > +  if (*e != '\0')
> >  {
> >int xbase = 1024;
> >int suffixes = 1;
> >strtol_error overflow;
> >  
> > -  if (!strchr (valid_suffixes, **p))
> > +  if (!strchr (valid_suffixes, *e))
> >  {
> >*val = tmp;
> >return err | LONGINT_INVALID_SUFFIX_CHAR;
> >  }
> >  
> > -  switch (**p)
> > +  switch (*e)
> >  {
> >  case 'E': case 'G': case 'g': case 'k': case 'K': case 'M': case 
> > 'm':
> >  case 'P': case 'Q': case 'R': case 'T': case 't': case 'Y': case 
> > 'Z':
> > @@ -138,10 +143,10 @@ __xstrtol (char const *nptr, char **endptr, int base,
> >   power-of-1024.  */
> >  
> >if (strchr (valid_suffixes, '0'))
> > -switch (p[0][1])
> > +switch (e[1])
> >{
> >case 'i':
> > -if (p[0][2] == 'B')
> > +if (e[2] == 'B')
> >suffixes += 2;
> >  break;
> >  
> > @@ -153,7 +158,7 @@ __xstrtol (char const *nptr, char **endptr, int base,
> >}
> >  }
> >  
> > -  switch (**p)
> > +  switch (*e)
> >  {
> >  case 'b':
> >overflow = bkm_scale (&tmp, 512);
> > @@ -224,8 +229,10 @@ __xstrtol (char const *nptr, char **endptr, int base,
> >  }
> >  
> >err |= overflow;
> > -  *p += suffixes;
> > -  if (**p)
> > +  e += suffixes;
> > +  if (endptr)
> > +*endptr = e;
> > +  if (*e)
> >  err |= LONGINT_INVALID_SUFFIX_CHAR;
> >  }
> >  
> > -- 
> > 2.45.2
> > 
> 
> 
> 
> -

Re: doc: Document the stack-trace and abort-debug modules

2024-07-19 Thread Bruno Haible
> > > The stack-trace and abort-debug modules are now in a state where they
> > > can be documented. Done as follows:
> > 
> > Thanks. Are these modules safe to use in signal handlers?
> 
> No, they aren't.

They aren't. But you can still get a stack traces, even from signal handlers,
with some additional work. Ian Lance Taylor has added this documentation to
libbacktrace, just yesterday:

  In general the functions provided by this library are async-signal-safe,
  meaning that they may be safely called from a signal handler.
  That said, on systems that use `dl_iterate_phdr`, such as GNU/Linux,
  the first call to a libbacktrace function will call `dl_iterate_phdr`,
  which is not in general async-signal-safe.  Therefore, programs
  that call libbacktrace from a signal handler should ensure that they
  make an initial call from outside of a signal handler.
  Similar considerations apply when arranging to call libbacktrace
  from within malloc; `dl_iterate_phdr` can also call malloc,
  so make an initial call to a libbacktrace function outside of
  malloc before trying to call libbacktrace functions within malloc.

Bruno






Re: [PATCH v1] xstrtol: 1 is not a valid base

2024-07-19 Thread Bruno Haible
Paul Eggert wrote:
> I installed the attached Gnulib patch which addresses that 
> point ...

The comments reference parameter names VAL, VALID_SUFFIXES, etc.
but the declaration does not contain parameter names. This patch
fixes that:


2024-07-19  Bruno Haible  

xstrtol: Improve documentation.
* lib/xstrtol.h: Don't test _STRTOL_ERROR, left-over from 2007-08-08.
(_DECLARE_XSTRTOL): Add the parameter names, referenced by the comments.

diff --git a/lib/xstrtol.h b/lib/xstrtol.h
index cedff6393e..10b48cac4e 100644
--- a/lib/xstrtol.h
+++ b/lib/xstrtol.h
@@ -27,7 +27,6 @@ extern "C" {
 #endif
 
 
-#ifndef _STRTOL_ERROR
 enum strtol_error
   {
 LONGINT_OK = 0,
@@ -42,7 +41,6 @@ enum strtol_error
 LONGINT_INVALID = 4
   };
 typedef enum strtol_error strtol_error;
-#endif
 
 /* Act like the system's strtol (NPTR, ENDPTR, BASE) except:
- The TYPE of the result might be something other than long int.
@@ -59,8 +57,11 @@ typedef enum strtol_error strtol_error;
  'c' for 1, and 'w' for 2.  */
 
 #define _DECLARE_XSTRTOL(name, type) \
-  strtol_error name (char const *restrict, char **restrict, int, \
- type *restrict, char const *restrict);
+  strtol_error name (char const *restrict /*nptr*/, \
+ char **restrict /*endptr*/,\
+ int /*base*/,  \
+ type *restrict /*val*/,\
+ char const *restrict /*valid_suffixes*/);
 _DECLARE_XSTRTOL (xstrtol, long int)
 _DECLARE_XSTRTOL (xstrtoul, unsigned long int)
 _DECLARE_XSTRTOL (xstrtoll, long long int)






Re: [PATCH v1] xstrtol: 1 is not a valid base

2024-07-19 Thread Bruno Haible
Alejandro Colomar wrote:
> BTW, does gnulib have documentation for xstrtol()?  I couldn't find it.
> And for MALLOC()?  I'm interested in reading both.

Paul added the documentation for xstrtol().

There is no macro or symbol named MALLOC in gnulib; I don't know what you
are referring to.

Bruno






Re: [PATCH v1] xstrtol: Remove dead code

2024-07-19 Thread Bruno Haible
Alejandro Colomar wrote:
> strtod(3) defers to strtol(3) for the example program.

Is this adequate? strtod() can produce underflow (e.g. for "1e-500").
In this case all implementations except MSVC set errno to ERANGE.
This is a case that cannot happen in strtol().

Bruno






Re: [PATCH v1] xstrtol: Remove dead code

2024-07-19 Thread Bruno Haible
Alejandro Colomar wrote:
> We'd need to know the precise specification of that system that can set
> errno = ENOMEM.
> 
> Is *endp guaranteed to be set?  Or may it be unset (as happens with
> EINVAL)?

One system that calls malloc() during strtod() is NetBSD. See

$ grep -ri malloc src/lib/libc/gdtoa/
src/lib/libc/gdtoa/g__fmt.c:if ((decimalpoint_cache = 
MALLOC(strlen(s0) + 1)) != NULL) {
src/lib/libc/gdtoa/README:for intermediate quantities, and MALLOC (see 
gdtoaimp.h) is called only
src/lib/libc/gdtoa/README:if the private pool does not suffice.   2000 is large 
enough that MALLOC
src/lib/libc/gdtoa/gdtoaimp.h: * #define MALLOC your_malloc, where 
your_malloc(n) acts like malloc(n)
src/lib/libc/gdtoa/gdtoaimp.h: *appropriate.  If MALLOC is undefined, 
malloc will be invoked
src/lib/libc/gdtoa/gdtoaimp.h: *recycle memory acquired from MALLOC, 
#define FREE to be the
src/lib/libc/gdtoa/gdtoaimp.h: *suffices to get rid of MALLOC calls 
except for unusual cases,
src/lib/libc/gdtoa/gdtoaimp.h:#ifdef MALLOC
src/lib/libc/gdtoa/gdtoaimp.h:extern Char *MALLOC ANSI((size_t));
src/lib/libc/gdtoa/gdtoaimp.h:#define MALLOC malloc
src/lib/libc/gdtoa/misc.c:  rv = (Bigint *)MALLOC(sizeof(Bigint) + 
(x-1)*sizeof(ULong));
src/lib/libc/gdtoa/misc.c:  rv = 
(Bigint*)MALLOC(len*sizeof(double));

Feel free to look up the source code in glibc, musl libc, FreeBSD,
OpenBSD, Darwin, OpenSolaris, Android. And that still does not
give information about Solaris and AIX.

Here in the Gnulib project, we typically write a unit test that verifies
the behaviour/properties we expect from a certain libc functions, and then
let it run on all platforms. If we get test failures, we need to adjust the
expectations. This works even for ENOMEM situations; see
  gnulib/tests/test-fprintf-posix2.{c,sh}
But it is, admittedly, quite some effort to write and maintain such a test.

Bruno






doc: Mention a bug in NetBSD's *gettext functions

2024-07-19 Thread Bruno Haible
POSIX:2024 (page 1193, line 40734) specifies that the LANGUAGE environment
variable should be ignored if its value is empty. NetBSD's *gettext()
functions don't do this.


2024-07-19  Bruno Haible  

doc: Mention a bug in NetBSD's *gettext functions.
* doc/glibc-functions/gettext.texi: Mention buggy handling of empty
LANGUAGE environment variable.
* doc/glibc-functions/dgettext.texi: Likewise.
* doc/glibc-functions/dcgettext.texi: Likewise.
* doc/glibc-functions/ngettext.texi: Likewise.
* doc/glibc-functions/dngettext.texi: Likewise.
* doc/glibc-functions/dcngettext.texi: Likewise.

diff --git a/doc/glibc-functions/dcgettext.texi 
b/doc/glibc-functions/dcgettext.texi
index f6e76e963c..9fa436b220 100644
--- a/doc/glibc-functions/dcgettext.texi
+++ b/doc/glibc-functions/dcgettext.texi
@@ -31,4 +31,9 @@
 @item
 This function is missing on some platforms:
 macOS 14, FreeBSD 14.0, OpenBSD 6.7, Minix 3.1.8, HP-UX 11, Cygwin 2.9, mingw, 
MSVC 14, Android 9.0.
+@item
+This function does not treat a @code{LANGUAGE} environment variable
+with an empty value like an unset @code{LANGUAGE} environment variable
+on some platforms:
+NetBSD 10.0.
 @end itemize
diff --git a/doc/glibc-functions/dcngettext.texi 
b/doc/glibc-functions/dcngettext.texi
index 32bb3f8666..24b420c867 100644
--- a/doc/glibc-functions/dcngettext.texi
+++ b/doc/glibc-functions/dcngettext.texi
@@ -31,4 +31,9 @@
 @item
 This function is missing on some platforms:
 macOS 14, FreeBSD 14.0, OpenBSD 6.7, Minix 3.1.8, HP-UX 11, Cygwin 2.9, mingw, 
MSVC 14, Android 9.0.
+@item
+This function does not treat a @code{LANGUAGE} environment variable
+with an empty value like an unset @code{LANGUAGE} environment variable
+on some platforms:
+NetBSD 10.0.
 @end itemize
diff --git a/doc/glibc-functions/dgettext.texi 
b/doc/glibc-functions/dgettext.texi
index b7cbbb0bd9..5d06c8fc80 100644
--- a/doc/glibc-functions/dgettext.texi
+++ b/doc/glibc-functions/dgettext.texi
@@ -31,4 +31,9 @@
 @item
 This function is missing on some platforms:
 macOS 14, FreeBSD 14.0, OpenBSD 6.7, Minix 3.1.8, HP-UX 11, Cygwin 2.9, mingw, 
MSVC 14, Android 9.0.
+@item
+This function does not treat a @code{LANGUAGE} environment variable
+with an empty value like an unset @code{LANGUAGE} environment variable
+on some platforms:
+NetBSD 10.0.
 @end itemize
diff --git a/doc/glibc-functions/dngettext.texi 
b/doc/glibc-functions/dngettext.texi
index db9a4389c9..a8acedf55d 100644
--- a/doc/glibc-functions/dngettext.texi
+++ b/doc/glibc-functions/dngettext.texi
@@ -31,4 +31,9 @@
 @item
 This function is missing on some platforms:
 macOS 14, FreeBSD 14.0, OpenBSD 6.7, Minix 3.1.8, HP-UX 11, Cygwin 2.9, mingw, 
MSVC 14, Android 9.0.
+@item
+This function does not treat a @code{LANGUAGE} environment variable
+with an empty value like an unset @code{LANGUAGE} environment variable
+on some platforms:
+NetBSD 10.0.
 @end itemize
diff --git a/doc/glibc-functions/gettext.texi b/doc/glibc-functions/gettext.texi
index ec9a81f341..8c11136b42 100644
--- a/doc/glibc-functions/gettext.texi
+++ b/doc/glibc-functions/gettext.texi
@@ -31,4 +31,9 @@
 @item
 This function is missing on some platforms:
 macOS 14, FreeBSD 14.0, OpenBSD 6.7, Minix 3.1.8, HP-UX 11, Cygwin 2.9, mingw, 
MSVC 14, Android 9.0.
+@item
+This function does not treat a @code{LANGUAGE} environment variable
+with an empty value like an unset @code{LANGUAGE} environment variable
+on some platforms:
+NetBSD 10.0.
 @end itemize
diff --git a/doc/glibc-functions/ngettext.texi 
b/doc/glibc-functions/ngettext.texi
index 13bb1f8dac..8aa5869597 100644
--- a/doc/glibc-functions/ngettext.texi
+++ b/doc/glibc-functions/ngettext.texi
@@ -31,4 +31,9 @@
 @item
 This function is missing on some platforms:
 macOS 14, FreeBSD 14.0, OpenBSD 6.7, Minix 3.1.8, HP-UX 11, Cygwin 2.9, mingw, 
MSVC 14, Android 9.0.
+@item
+This function does not treat a @code{LANGUAGE} environment variable
+with an empty value like an unset @code{LANGUAGE} environment variable
+on some platforms:
+NetBSD 10.0.
 @end itemize






doc: Add references to ISO C23

2024-07-19 Thread Bruno Haible
Some functions are specified in ISO C23, but not yet in POSIX.


2024-07-19  Bruno Haible  

doc: Add references to ISO C23.
* doc/posix-functions/totalorder*.texi: Reference ISO C23.
* doc/posix-functions/getpayload*.texi: Likewise.
* doc/posix-functions/setpayload*.texi: Likewise.

diff --git a/doc/posix-functions/getpayload.texi 
b/doc/posix-functions/getpayload.texi
index 1fb7ab2b13..689a381cec 100644
--- a/doc/posix-functions/getpayload.texi
+++ b/doc/posix-functions/getpayload.texi
@@ -2,6 +2,8 @@
 @section @code{getpayload}
 @findex getpayload
 
+ISO C23 specification:@* 
@url{http://www.open-std.org/jtc1/sc22/wg14/www/docs/n3096.pdf} section 
F.10.13.1
+
 Documentation:@*
 @ifinfo
 @ref{FP Bit Twiddling,,Setting and modifying single bits of FP values,libc}.
diff --git a/doc/posix-functions/getpayloadf.texi 
b/doc/posix-functions/getpayloadf.texi
index d955d1e536..82699be174 100644
--- a/doc/posix-functions/getpayloadf.texi
+++ b/doc/posix-functions/getpayloadf.texi
@@ -2,6 +2,8 @@
 @section @code{getpayloadf}
 @findex getpayloadf
 
+ISO C23 specification:@* 
@url{http://www.open-std.org/jtc1/sc22/wg14/www/docs/n3096.pdf} section 
F.10.13.1
+
 Documentation:@*
 @ifinfo
 @ref{FP Bit Twiddling,,Setting and modifying single bits of FP values,libc}.
diff --git a/doc/posix-functions/getpayloadl.texi 
b/doc/posix-functions/getpayloadl.texi
index 4d9f622a90..11a2e2bda5 100644
--- a/doc/posix-functions/getpayloadl.texi
+++ b/doc/posix-functions/getpayloadl.texi
@@ -2,6 +2,8 @@
 @section @code{getpayloadl}
 @findex getpayloadl
 
+ISO C23 specification:@* 
@url{http://www.open-std.org/jtc1/sc22/wg14/www/docs/n3096.pdf} section 
F.10.13.1
+
 Documentation:@*
 @ifinfo
 @ref{FP Bit Twiddling,,Setting and modifying single bits of FP values,libc}.
diff --git a/doc/posix-functions/setpayload.texi 
b/doc/posix-functions/setpayload.texi
index eff1c40ae2..41bf75c12f 100644
--- a/doc/posix-functions/setpayload.texi
+++ b/doc/posix-functions/setpayload.texi
@@ -2,6 +2,8 @@
 @section @code{setpayload}
 @findex setpayload
 
+ISO C23 specification:@* 
@url{http://www.open-std.org/jtc1/sc22/wg14/www/docs/n3096.pdf} section 
F.10.13.2
+
 Documentation:@*
 @ifinfo
 @ref{FP Bit Twiddling,,Setting and modifying single bits of FP values,libc}.
diff --git a/doc/posix-functions/setpayloadf.texi 
b/doc/posix-functions/setpayloadf.texi
index d34c83ae5b..7aef05f79b 100644
--- a/doc/posix-functions/setpayloadf.texi
+++ b/doc/posix-functions/setpayloadf.texi
@@ -2,6 +2,8 @@
 @section @code{setpayloadf}
 @findex setpayloadf
 
+ISO C23 specification:@* 
@url{http://www.open-std.org/jtc1/sc22/wg14/www/docs/n3096.pdf} section 
F.10.13.2
+
 Documentation:@*
 @ifinfo
 @ref{FP Bit Twiddling,,Setting and modifying single bits of FP values,libc}.
diff --git a/doc/posix-functions/setpayloadl.texi 
b/doc/posix-functions/setpayloadl.texi
index ef7627372b..11e05685ff 100644
--- a/doc/posix-functions/setpayloadl.texi
+++ b/doc/posix-functions/setpayloadl.texi
@@ -2,6 +2,8 @@
 @section @code{setpayloadl}
 @findex setpayloadl
 
+ISO C23 specification:@* 
@url{http://www.open-std.org/jtc1/sc22/wg14/www/docs/n3096.pdf} section 
F.10.13.2
+
 Documentation:@*
 @ifinfo
 @ref{FP Bit Twiddling,,Setting and modifying single bits of FP values,libc}.
diff --git a/doc/posix-functions/setpayloadsig.texi 
b/doc/posix-functions/setpayloadsig.texi
index 0581fae5ae..5427535a8d 100644
--- a/doc/posix-functions/setpayloadsig.texi
+++ b/doc/posix-functions/setpayloadsig.texi
@@ -2,6 +2,8 @@
 @section @code{setpayloadsig}
 @findex setpayloadsig
 
+ISO C23 specification:@* 
@url{http://www.open-std.org/jtc1/sc22/wg14/www/docs/n3096.pdf} section 
F.10.13.3
+
 Documentation:@*
 @ifinfo
 @ref{FP Bit Twiddling,,Setting and modifying single bits of FP values,libc}.
diff --git a/doc/posix-functions/setpayloadsigf.texi 
b/doc/posix-functions/setpayloadsigf.texi
index 1adc76bb57..64f562cb8e 100644
--- a/doc/posix-functions/setpayloadsigf.texi
+++ b/doc/posix-functions/setpayloadsigf.texi
@@ -2,6 +2,8 @@
 @section @code{setpayloadsigf}
 @findex setpayloadsigf
 
+ISO C23 specification:@* 
@url{http://www.open-std.org/jtc1/sc22/wg14/www/docs/n3096.pdf} section 
F.10.13.3
+
 Documentation:@*
 @ifinfo
 @ref{FP Bit Twiddling,,Setting and modifying single bits of FP values,libc}.
diff --git a/doc/posix-functions/setpayloadsigl.texi 
b/doc/posix-functions/setpayloadsigl.texi
index 514026edfa..31301c8808 100644
--- a/doc/posix-functions/setpayloadsigl.texi
+++ b/doc/posix-functions/setpayloadsigl.texi
@@ -2,6 +2,8 @@
 @section @code{setpayloadsigl}
 @findex setpayloadsigl
 
+ISO C23 specification:@* 
@url{http://www.open-std.org/jtc1/sc22/wg14/www/docs/n3096.pdf} section 
F.10.13.3
+
 Documentation:@*
 @ifinfo
 @ref{FP Bit Twiddling,,Setting and modifying single bits of FP values,libc}.
diff --git a/doc/posix-functions/totalorder.texi 
b/doc/posix-functions/totalorder.texi
index 7968ecbae3..125d808c09 100644
--- a/doc/posix-functions/totalorder.texi
+++ b/d

doc: Mention function that were added in ISO C23

2024-07-19 Thread Bruno Haible
ISO C23 adds a couple of new functions in . See
https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2118.pdf

This patch adds documentation for them to Gnulib.


2024-07-19  Bruno Haible  

doc: Mention  function that were added in ISO C23.
* doc/posix-functions/acospi.texi: New file.
* doc/posix-functions/acospif.texi: New file.
* doc/posix-functions/acospil.texi: New file.
* doc/posix-functions/asinpi.texi: New file.
* doc/posix-functions/asinpif.texi: New file.
* doc/posix-functions/asinpil.texi: New file.
* doc/posix-functions/atanpi.texi: New file.
* doc/posix-functions/atanpif.texi: New file.
* doc/posix-functions/atanpil.texi: New file.
* doc/posix-functions/atan2pi.texi: New file.
* doc/posix-functions/atan2pif.texi: New file.
* doc/posix-functions/atan2pil.texi: New file.
* doc/posix-functions/compoundn.texi: New file.
* doc/posix-functions/compoundnf.texi: New file.
* doc/posix-functions/compoundnl.texi: New file.
* doc/posix-functions/cospi.texi: New file.
* doc/posix-functions/cospif.texi: New file.
* doc/posix-functions/cospil.texi: New file.
* doc/posix-functions/exp10.texi: Moved here from doc/glibc-functions/.
* doc/posix-functions/exp10f.texi: Moved here from doc/glibc-functions/.
* doc/posix-functions/exp10l.texi: Moved here from doc/glibc-functions/.
* doc/posix-functions/exp10m1.texi: New file.
* doc/posix-functions/exp10m1f.texi: New file.
* doc/posix-functions/exp10m1l.texi: New file.
* doc/posix-functions/exp2m1.texi: New file.
* doc/posix-functions/exp2m1f.texi: New file.
* doc/posix-functions/exp2m1l.texi: New file.
* doc/posix-functions/log10p1.texi: New file.
* doc/posix-functions/log10p1f.texi: New file.
* doc/posix-functions/log10p1l.texi: New file.
* doc/posix-functions/log2p1.texi: New file.
* doc/posix-functions/log2p1f.texi: New file.
* doc/posix-functions/log2p1l.texi: New file.
* doc/posix-functions/logp1.texi: New file.
* doc/posix-functions/logp1f.texi: New file.
* doc/posix-functions/logp1l.texi: New file.
* doc/posix-functions/pown.texi: New file.
* doc/posix-functions/pownf.texi: New file.
* doc/posix-functions/pownl.texi: New file.
* doc/posix-functions/powr.texi: New file.
* doc/posix-functions/powrf.texi: New file.
* doc/posix-functions/powrl.texi: New file.
* doc/posix-functions/rootn.texi: New file.
* doc/posix-functions/rootnf.texi: New file.
* doc/posix-functions/rootnl.texi: New file.
* doc/posix-functions/rsqrt.texi: New file.
* doc/posix-functions/rsqrtf.texi: New file.
* doc/posix-functions/rsqrtl.texi: New file.
* doc/posix-functions/sinpi.texi: New file.
* doc/posix-functions/sinpif.texi: New file.
* doc/posix-functions/sinpil.texi: New file.
* doc/posix-functions/tanpi.texi: New file.
* doc/posix-functions/tanpif.texi: New file.
* doc/posix-functions/tanpil.texi: New file.
* doc/gnulib.texi (Function Substitutes): Include them.
(Glibc math.h): Don't include glibc-functions/exp10*.texi.

diff --git a/doc/gnulib.texi b/doc/gnulib.texi
index d67caa6660..f538925d8d 100644
--- a/doc/gnulib.texi
+++ b/doc/gnulib.texi
@@ -1274,6 +1274,9 @@
 * acoshf::
 * acoshl::
 * acosl::
+* acospi::
+* acospif::
+* acospil::
 * aio_cancel::
 * aio_error::
 * aio_fsync::
@@ -1292,16 +1295,25 @@
 * asinhf::
 * asinhl::
 * asinl::
+* asinpi::
+* asinpif::
+* asinpil::
 * assert::
 * atan::
 * atan2::
 * atan2f::
 * atan2l::
+* atan2pi::
+* atan2pif::
+* atan2pil::
 * atanf::
 * atanh::
 * atanhf::
 * atanhl::
 * atanl::
+* atanpi::
+* atanpif::
+* atanpil::
 * atexit::
 * atof::
 * atoi::
@@ -1390,6 +1402,9 @@
 * cnd_signal::
 * cnd_timedwait::
 * cnd_wait::
+* compoundn::
+* compoundnf::
+* compoundnl::
 * confstr::
 * conj::
 * conjf::
@@ -1404,6 +1419,9 @@
 * coshf::
 * coshl::
 * cosl::
+* cospi::
+* cospif::
+* cospil::
 * cpow::
 * cpowf::
 * cpowl::
@@ -1487,9 +1505,18 @@
 * execvp::
 * exit::
 * exp::
+* exp10::
+* exp10f::
+* exp10l::
+* exp10m1::
+* exp10m1f::
+* exp10m1l::
 * exp2::
 * exp2f::
 * exp2l::
+* exp2m1::
+* exp2m1f::
+* exp2m1l::
 * expf::
 * expl::
 * expm1::
@@ -1848,17 +1875,26 @@
 * log10::
 * log10f::
 * log10l::
+* log10p1::
+* log10p1f::
+* log10p1l::
 * log1p::
 * log1pf::
 * log1pl::
 * log2::
 * log2f::
 * log2l::
+* log2p1::
+* log2p1f::
+* log2p1l::
 * logb::
 * logbf::
 * logbl::
 * logf::
 * logl::
+* logp1::
+* logp1f::
+* logp1l::
 * longjmp::
 * lrand48::
 * lrint::
@@ -2056,6 +2092,12 @@
 * pow::
 * powf::
 * powl::
+* pown::
+* pownf::
+* pownl::
+* powr::
+* powrf::
+* powrl::
 * pread::
 * printf::
 * pselect::
@@ -2213,12 +2255,18 @@
 * rintf::
 * rintl::
 * rmdir::
+* rootn::
+* rootnf:

Re: [PATCH v1 0/2] xstrtol() fixes

2024-07-19 Thread Paul Eggert
Thanks, I installed the attached, which should fix the problems you 
mentioned in a less-invasive way.From 16b33e6649425fcdce095f262da98b539d2f7448 Mon Sep 17 00:00:00 2001
From: Paul Eggert 
Date: Fri, 19 Jul 2024 10:39:58 -0700
Subject: [PATCH] xstrtol: be more robust against odd failures
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

* 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   | 11 +++
 lib/xstrtol.c   | 15 ++-
 modules/xstrtol |  1 +
 3 files changed, 22 insertions(+), 5 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 072a28855d..4e88d446a1 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2024-07-19  Paul Eggert  
+
+	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  
 
 	doc: Mention  function that were added in ISO C23.
diff --git a/lib/xstrtol.c b/lib/xstrtol.c
index c3145171f3..797d3e4dee 100644
--- a/lib/xstrtol.c
+++ b/lib/xstrtol.c
@@ -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.  */
diff --git a/modules/xstrtol b/modules/xstrtol
index ef49dfc357..1f8a28fa6c 100644
--- a/modules/xstrtol
+++ b/modules/xstrtol
@@ -9,6 +9,7 @@ m4/xstrtol.m4
 
 Depends-on:
 intprops
+nullptr
 stdckdint
 stdint
 
-- 
2.43.0



Re: [PATCH v1] xstrtol: Remove dead code

2024-07-19 Thread Alejandro Colomar
Hi Bruno,

On Fri, Jul 19, 2024 at 06:54:40PM GMT, Bruno Haible wrote:
> Alejandro Colomar wrote:
> > strtod(3) defers to strtol(3) for the example program.
> 
> Is this adequate? strtod() can produce underflow (e.g. for "1e-500").
> In this case all implementations except MSVC set errno to ERANGE.
> This is a case that cannot happen in strtol().

I was thinking tonight that it would be better to actually add an
example program to strtod(3) (and also document in VERSIONS that other
systems do different).

Cheers,
Alex

> 
> Bruno
> 
> 
> 

-- 



signature.asc
Description: PGP signature


Re: [PATCH v1 0/2] xstrtol() fixes

2024-07-19 Thread Alejandro Colomar
Hi Paul,

On Fri, Jul 19, 2024 at 10:47:11AM GMT, Paul Eggert wrote:
> Thanks, I installed the attached, which should fix the problems you
> mentioned in a less-invasive way.

Thanks!  I'll re-check again later to have a fresh look at the code, but
LGTM, I think.

Have a lovely night!
Alex


-- 



signature.asc
Description: PGP signature


doc: Add documentation about math_errhandling

2024-07-19 Thread Bruno Haible
A test program (attached) reveals the value of math_errhandling
and whether various operations behave like math_errhandling says.
The result is pretty sobering: math_errhandling is useless.


2024-07-19  Bruno Haible  

doc: Add documentation about math_errhandling.
* doc/posix-functions/math_errhandling.texi: New file.
* doc/gnulib.texi (Function Substitutes): Include it.

diff --git a/doc/gnulib.texi b/doc/gnulib.texi
index f538925d8d..683d2aabca 100644
--- a/doc/gnulib.texi
+++ b/doc/gnulib.texi
@@ -1907,6 +1907,7 @@
 * lseek::
 * lstat::
 * malloc::
+* math_errhandling::
 * mblen::
 * mbrlen::
 * mbrtoc8::
@@ -3303,6 +3304,7 @@
 @include posix-functions/lseek.texi
 @include posix-functions/lstat.texi
 @include posix-functions/malloc.texi
+@include posix-functions/math_errhandling.texi
 @include posix-functions/mblen.texi
 @include posix-functions/mbrlen.texi
 @include posix-functions/mbrtoc8.texi
diff --git a/doc/posix-functions/math_errhandling.texi 
b/doc/posix-functions/math_errhandling.texi
new file mode 100644
index 00..f897a5ec4b
--- /dev/null
+++ b/doc/posix-functions/math_errhandling.texi
@@ -0,0 +1,34 @@
+@node math_errhandling
+@section @code{math_errhandling}
+@findex math_errhandling
+
+ISO C23 specification:@* 
@url{http://www.open-std.org/jtc1/sc22/wg14/www/docs/n3096.pdf} section 7.12.1
+
+Documentation:@* 
@uref{https://www.kernel.org/doc/man-pages/online/pages/man7/math_error.7.html,,man
 math_error}
+
+Gnulib module: ---
+
+Portability problems fixed by Gnulib:
+@itemize
+@end itemize
+
+Portability problems not fixed by Gnulib:
+@itemize
+@item
+This macro is missing on some platforms:
+NetBSD 10.0, mingw.
+@item
+This macro does not describe the error behaviour
+of elementary arithmetic operations (+, -, *, /)
+and of mathematical operations for which the compiler emits inline code
+(such as @code{sqrt} on some CPUs).
+@item
+This macro does not describe the error behaviour of functions
+such as @code{strtod}.
+@item
+For mathematical operations in general, it is a safer bet to look
+at the exceptions set in the floating-point environment
+(by calling @code{feclearexcept (FE_ALL_EXCEPT)} before the operation
+and @code{fetestexcept} after the operation),
+regardless of @code{math_errhandling}.
+@end itemize
/* Test error handling of math functions.
   https://man7.org/linux/man-pages/man7/math_error.7.html
   https://en.cppreference.com/w/cpp/numeric/math/math_errhandling
 */
#include 
#include 
#include 
#include 
#include 

#ifndef MATH_ERRNO
# define MATH_ERRNO 1
#endif
#ifndef MATH_ERREXCEPT
# define MATH_ERREXCEPT 2
#endif
#ifndef math_errhandling
# define math_errhandling 0
#endif

volatile double z;

int main ()
{
  printf ("math_errhandling= %d %d\n", (math_errhandling & MATH_ERREXCEPT) ? 1 : 0, (math_errhandling & MATH_ERRNO) ? 1 : 0);

  {
errno = 0;
feclearexcept (FE_ALL_EXCEPT);
char *end;
z = strtod ("1e500", &end);
int err = errno;
printf ("strtod overflow = %d %d\n", fetestexcept (FE_OVERFLOW) ? 1 : 0, err == ERANGE);
  }

  {
errno = 0;
feclearexcept (FE_ALL_EXCEPT);
char *end;
z = strtod ("1e-320", &end);
int err = errno;
printf ("strtod underflow= %d %d\n", fetestexcept (FE_UNDERFLOW) ? 1 : 0, err == ERANGE);
  }

  {
errno = 0;
feclearexcept (FE_ALL_EXCEPT);
volatile double x = 1.0;
volatile double y = 0.0;
z = x / y;
int err = errno;
printf ("Division pole error = %d %d\n", fetestexcept (FE_DIVBYZERO) ? 1 : 0, err == ERANGE);
  }

  {
errno = 0;
feclearexcept (FE_ALL_EXCEPT);
volatile double x = 1.0e300;
volatile double y = 1.0e-200;
z = x / y;
int err = errno;
printf ("Division overflow   = %d %d\n", fetestexcept (FE_OVERFLOW) ? 1 : 0, err == ERANGE);
  }

  {
errno = 0;
feclearexcept (FE_ALL_EXCEPT);
volatile double x = 1.0e-300;
volatile double y = 1.0e20;
z = x / y;
int err = errno;
printf ("Division underflow  = %d %d\n", fetestexcept (FE_UNDERFLOW) ? 1 : 0, err == ERANGE);
  }

  {
errno = 0;
feclearexcept (FE_ALL_EXCEPT);
volatile double x = 1.0;
volatile double y = 10.0;
z = x / y;
int err = errno;
printf ("Division inexact= %d %d\n", fetestexcept (FE_INEXACT) ? 1 : 0, err != 0);
  }

  {
errno = 0;
feclearexcept (FE_ALL_EXCEPT);
volatile double x = 2.0;
z = sqrt (x);
int err = errno;
printf ("sqrt inexact= %d %d\n", fetestexcept (FE_INEXACT) ? 1 : 0, err != 0);
  }

  {
errno = 0;
feclearexcept (FE_ALL_EXCEPT);
volatile double x = 1.0e300;
volatile double y = 2.0;
z = pow (x, y);
int err = errno;
printf ("pow overflow= %d %d\n", fetestexcept (FE_OVERFLOW) ? 1 : 0, err == ERANGE);
  }

  {
errno = 0;
feclearexcept (FE_ALL_EXCEPT);
volatile double x = 1.0e-300;
volatile double y = 2.0;
z = pow (x, y);
int err = errno;
printf ("pow underflow   = %d %d\n",

C23 n3220

2024-07-19 Thread Alejandro Colomar
Hi Bruno,

I've seen your commit adding references to C23.

There's a more recent draft (the final one) of C23: n3220


You may want to use that link instead.

Have a lovely night!
Alex

-- 



signature.asc
Description: PGP signature


Re: [PATCH v1 0/2] xstrtol() fixes

2024-07-19 Thread Alejandro Colomar
Hi Paul,

On Fri, Jul 19, 2024 at 08:19:07PM GMT, Alejandro Colomar wrote:
> Hi Paul,
> 
> On Fri, Jul 19, 2024 at 10:47:11AM GMT, Paul Eggert wrote:
> > Thanks, I installed the attached, which should fix the problems you
> > mentioned in a less-invasive way.
> 
> Thanks!  I'll re-check again later to have a fresh look at the code, but
> LGTM, I think.

Yup; your implementation is correct, and IMO better than my suggestion.
And I think xstrtol() is good (at least as far as calling strtol(3) is
concerned).  Thanks!  ;-)

Have a lovely night!
Alex

-- 



signature.asc
Description: PGP signature


XMALLOC() et al (was: [PATCH v1] xstrtol: 1 is not a valid base)

2024-07-19 Thread Alejandro Colomar
[Reduced CC; since I'm changing topic]

Hi Bruno,

On Fri, Jul 19, 2024 at 06:47:13PM GMT, Bruno Haible wrote:
> Alejandro Colomar wrote:
> > BTW, does gnulib have documentation for xstrtol()?  I couldn't find it.
> > And for MALLOC()?  I'm interested in reading both.
> 
> Paul added the documentation for xstrtol().

Thanks.

> There is no macro or symbol named MALLOC in gnulib; I don't know what you
> are referring to.

D'oh, I was referring to XMALLOC (et al.).  I've found the
documentation for ALLOC (et al.) and have now seen that XMALLOC() is
lightly described in the source code, but not in the manual.



Are XMALLOC et al. part of the public API, or are they only internal
helper macros?  Are there current users of it (apart from gnulib
itself)?  I'm interested in discussing some details about that set of
macros if they're not set in stone.

> 
> Bruno

Have a lovely night!
Alex

-- 



signature.asc
Description: PGP signature


Re: XMALLOC() et al

2024-07-19 Thread Paul Eggert

On 2024-07-19 17:13, Alejandro Colomar wrote:

Are XMALLOC et al. part of the public API, or are they only internal
helper macros?  Are there current users of it (apart from gnulib
itself)?  I'm interested in discussing some details about that set of
macros if they're not set in stone.


They're not documented anywhere other than the source code. They're a 
bit controversial in the sense that I would rather not see their use 
proliferate.


In a sense they're a poor substitute for C++ templates and my feeling is 
that if you want C++ you should use C++.




Re: XMALLOC() et al

2024-07-19 Thread Bruno Haible
Alejandro Colomar wrote:
> > Are XMALLOC et al. part of the public API, or are they only internal
> > helper macros?

They are public API. This can be seen from the fact that
  - the macro is defined in xalloc.h,
  - the module description modules/xalloc lists this file as the file to
include:

  Include:
  "xalloc.h"

> They're not documented anywhere other than the source code.

Yes, many Gnulib APIs are only documented in the .h files. In the manual we
tend to only give a high-level overview (if at all).

> >  Are there current users of it (apart from gnulib itself)?

GNU gettext uses the XMALLOC macro in more than 100 places. It's
just so convenient to do a memory allocation in 1 line of code:

   op->token = XMALLOC (struct token);

> > I'm interested in discussing some details about that set of
> > macros if they're not set in stone.

Go ahead. Maybe your suggestions can be useful for another set of macros?

Paul Eggert wrote:
> They're a 
> bit controversial in the sense that I would rather not see their use 
> proliferate.

I disagree with that. They are very useful for application-level code:
concise, and with error-checking included.

I know that in some places, such as readutmp.c, you like to optimize several
memory allocations into a single one. But in other places, namely in
applications where the maintainability is of higher priority than the
execution speed, it's perfectly OK to do one memory allocation for each
piece of a data structure.

> In a sense they're a poor substitute for C++ templates and my feeling is 
> that if you want C++ you should use C++.

Nah. C++ is a big waste of developers' time, for so many reasons. We
shouldn't push developers towards this ill-designed language.

Bruno






Re: C23 n3220

2024-07-19 Thread Bruno Haible
Hi Alejandro,

> There's a more recent draft (the final one) of C23: n3220
> 

Thanks; I had not noticed this one.

> You may want to use that link instead.

Done:


2024-07-19  Bruno Haible  

doc: Reference a newer ISO C23 draft.
Suggested by Alejandro Colomar  in
.
* doc/*/*.texi: Refer to n3220.pdf (February 2024) instead of
n3096.pdf (April 2023).

https://git.savannah.gnu.org/gitweb/?p=gnulib.git;a=commitdiff;h=9a5294871da0dd4f9ba803ff7bc9ac6747fcf468






gitlog-to-changelog: Improve output of pdf documentation.

2024-07-19 Thread Collin Funk
I formatted this section of code poorly when I added documentation. Even
with '-t @finalout' to remove the black bars on overfilled hboxes it
obviously extends past the margin. Committed the attached patch so it
isn't ugly anymore.

Collin

>From 17967b5128a3747a35fa3e7bbc33435f5c1dedc2 Mon Sep 17 00:00:00 2001
From: Collin Funk 
Date: Fri, 19 Jul 2024 19:24:17 -0700
Subject: [PATCH] gitlog-to-changelog: Improve output of pdf documentation.

* doc/gitlog-to-changelog.texi (gitlog-to-changelog): Reformat code to
use shorter lines and avoid overfull hboxes.
---
 ChangeLog| 6 ++
 doc/gitlog-to-changelog.texi | 8 
 2 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index c5abb91e45..50353eb63b 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2024-07-19  Collin Funk  
+
+	gitlog-to-changelog: Improve output of pdf documentation.
+	* doc/gitlog-to-changelog.texi (gitlog-to-changelog): Reformat code to
+	use shorter lines and avoid overfull hboxes.
+
 2024-07-19  Bruno Haible  
 
 	doc: Reference a newer ISO C23 draft.
diff --git a/doc/gitlog-to-changelog.texi b/doc/gitlog-to-changelog.texi
index 6f1cabdcaf..8d39073295 100644
--- a/doc/gitlog-to-changelog.texi
+++ b/doc/gitlog-to-changelog.texi
@@ -51,10 +51,10 @@ @node gitlog-to-changelog
 dist-hook: gen-ChangeLog
 .PHONY: gen-ChangeLog
 gen-ChangeLog:
-$(AM_V_GEN)if test -e .git; then \
-  $(top_srcdir)/build-aux/gitlog-to-changelog --commit-timezone  \
-> $(distdir)/ChangeLog.tmp &&\
-  mv -f $(distdir)/ChangeLog.tmp $(distdir)/ChangeLog;   \
+$(AM_V_GEN)if test -e .git; then   \
+  $(top_srcdir)/build-aux/gitlog-to-changelog  \
+--commit-timezone > $(distdir)/ChangeLog.tmp &&\
+  mv -f $(distdir)/ChangeLog.tmp $(distdir)/ChangeLog; \
 fi
 @end example
 
-- 
2.45.2



Re: XMALLOC() et al

2024-07-19 Thread Paul Eggert

On 2024-07-19 18:59, Bruno Haible wrote:

GNU gettext uses the XMALLOC macro in more than 100 places. It's
just so convenient to do a memory allocation in 1 line of code:


I find it more convenient to write this:

  context = xmalloc (sizeof *context);

than this (taken from GNU gettext):

  context = XMALLOC (markup_parse_context_ty);

not simply because it's easier for a human to read and know instantly 
that it's right, but because when one changes the type of 'context' one 
needn't change all corresponding xmalloc calls.


Although XMALLOC has some advantages, all in all in my experience 
they're outweighed by its disadvantages. I would rather not encourage 
its use in Gnulib proper.




I know that in some places, such as readutmp.c, you like to optimize several
memory allocations into a single one.


Yes, and that flexibility is another advantage of xmalloc etc. over 
XMALLOC etc.