Re: [PATCH v1] xstrtol: Remove dead code

Bruno Haible <[email protected]> Fri, 19 Jul 2024 19:13:25 +0200
Newsgroups dev.linux.lists.liba2i
Message-ID <4253333.uADA5c2rLh@nimes>
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