Bruno Haible [Tue, 5 Nov 2024 14:53:47 +0000 (15:53 +0100)]
calloc, malloc: Fix a comment.
* m4/malloc.m4 (gl_FUNC_MALLOC_GNU): Mention that it sets
HAVE_MALLOC_PTRDIFF.
(gl_FUNC_MALLOC_PTRDIFF): Likewise.
(gl_CHECK_MALLOC_PTRDIFF): Do not mention HAVE_MALLOC_PTRDIFF here.
Paul Eggert [Tue, 5 Nov 2024 06:24:29 +0000 (22:24 -0800)]
safe-alloc: simplify, given new reallocarray behavior
* lib/safe-alloc.h [__CHERI_PURE_CAPABILITY__]:
Remove special cases for this, as reallocarray
now does it for us.
(safe_alloc_realloc_n): Remove. All uses replaced by reallocarray.
Paul Eggert [Tue, 5 Nov 2024 03:09:08 +0000 (19:09 -0800)]
realloc-posix: update doc
* doc/posix-functions/realloc.texi: Update to match recent Gnulib
changes and OS discoveries, and to clarify and simplify by
coalescing behaviors (4) and (5).
Paul Eggert [Tue, 5 Nov 2024 02:16:50 +0000 (18:16 -0800)]
realloc-posix: tune for glibc-like
For glibc, implementing realloc (p, s) as realloc (p, s?s:1) suffices,
and it’s probably better to do it inline to aid static checking.
* lib/realloc.c (rpl_realloc) [_GL_INLINE_RPL_REALLOC]:
Remove, replacing with inline function in stdlib.h.
* lib/stdlib.in.h (_GL_INLINE_RPL_REALLOC): New macro.
(rpl_realloc): Define as inline if _GL_INLINE_RPL_REALLOC.
* m4/realloc.m4 (gl_FUNC_REALLOC_0_NONNULL):
Set REPLACE_REALLOC_FOR_REALLOC_POSIX=2 instead of 1,
if it’s close enough to glibc that the inline optimization is valid.
Paul Eggert [Mon, 4 Nov 2024 19:12:21 +0000 (11:12 -0800)]
stdlib: make MB_CUR_MAX usable from extern inline
* lib/stdlib.c: New file.
* modules/stdlib (Files, lib_SOURCES): Add it.
(Depends-on): Add extern-inline.
* lib/stdlib.in.h: Use _GL_INLINE_HEADER_BEGIN, _GL_INLINE_HEADER_END.
(_GL_STDLIB_INLINE): New macro.
(gl_MB_CUR_MAX): Make it _GL_STDLIB_INLINE not static inline,
so that it can be called from extern inline functions.
Redo implementation to help GCC better optimize common expressions
like (MB_CUR_MAX == 1).
Paul Eggert [Mon, 4 Nov 2024 07:27:52 +0000 (23:27 -0800)]
realloc-posix: set CHERI bounds
* lib/ialloc.h, lib/xmalloc.c [__CHERI_PURE_CAPABILITY__]:
Do nothing special, as realloc and reallocarray
should do this for us now.
* lib/realloc.c [__CHERI_PURE_CAPABILITY__]:
Include cheri.h, and arrange for rpl_realloc to set bounds.
Paul Eggert [Mon, 4 Nov 2024 06:41:41 +0000 (22:41 -0800)]
realloc-posix: realloc (..., 0) now returns nonnull
* lib/realloc.c (rpl_realloc): Simplify and tune by using
HAVE_REALLOC_0_NONNULL and HAVE_MALLOC_PTRDIFF, and
by having just one call to realloc instead of two.
* lib/reallocarray.c (reallocarray): Simplify and tune
by delegating the zero case to the revised realloc.
* m4/eealloc.m4 (_AC_FUNC_REALLOC_IF): Since only eealloc uses
this macro now, move its definition here ...
* m4/realloc.m4: ... from here.
(gl_FUNC_REALLOC_0_NONNULL): Also check that realloc (p, 0)
returns nonnull. Require gl_FUNC_REALLOC_POSIX.
Define HAVE_REALLOC_0_NONNULL.
* m4/reallocarray.m4 (gl_FUNC_REALLOCARRAY):
Also replace reallocarray if it returns a null pointer for size zero.
* modules/eealloc (Files): Remove m4/realloc.m4.
* modules/realloc-posix (Depends-on): Add extensions-aix.
* modules/reallocarray (Files): Add m4/realloc.m4.
Paul Eggert [Mon, 4 Nov 2024 05:44:15 +0000 (21:44 -0800)]
realloc: don’t require success for nongrowth
* lib/realloc.c (rpl_realloc): Do not require realloc (p, 0) to
succeed, as apparently glibc realloc (p, n) can sometimes fail
even when the region would not grow.
Paul Eggert [Mon, 4 Nov 2024 02:18:58 +0000 (18:18 -0800)]
calloc, malloc: tune a bit
This applies mostly to non-glibc platforms, or to 32-bit
glibc before glibc 2.30 (2019).
* lib/calloc.c (rpl_calloc):
* lib/malloc.c (rpl_malloc):
Optimize away some of the code if !HAVE_MALLOC_0_NONNULL or if
!HAVE_MALLOC_PTRDIFF.
* m4/malloc.m4 (gl_FUNC_MALLOC_GNU): Define HAVE_MALLOC_0_NONNULL.
(gl_FUNC_MALLOC_PTRDIFF): Define HAVE_MALLOC_PTRDIFF.
Paul Eggert [Sun, 3 Nov 2024 20:24:03 +0000 (12:24 -0800)]
calloc: configure more like malloc
* m4/calloc.m4 (gl_FUNC_CALLOC_IF): Rename from _AC_FUNC_CALLOC_IF
since this is not derived from Autoconf. All uses changed.
Redo implementation so that it is more like _AC_FUNC_MALLOC_IF;
this fixes some problems with "volatile" and modernizes the known
platforms.
Paul Eggert [Sun, 3 Nov 2024 20:19:33 +0000 (12:19 -0800)]
malloc-gnu: depend on stdckdint not xalloc-oversized
* lib/malloc.c: Include stdckdint.h, xalloc-oversized.h.
(rpl_malloc): Use ckd_mul instead of xalloc_oversized.
* modules/malloc-gnu, modules/malloc-posix:
(Depends-on): Depend on stdckdint, not xalloc-oversized.
Paul Eggert [Sun, 3 Nov 2024 19:11:03 +0000 (11:11 -0800)]
calloc: depend on stdckdint not xalloc-oversized
* lib/calloc.c: Include stdckdint.h, xalloc-oversized.h.
(rpl_calloc): Use ckd_mul instead of xalloc_oversized.
* modules/calloc-gnu, modules/calloc-posix:
(Depends-on): Depend on stdckdint, not xalloc-oversized.
Paul Eggert [Mon, 4 Nov 2024 17:38:03 +0000 (09:38 -0800)]
malloc etc. tests: use volatile more consistently
Typically it’s ineffective to use ‘void *volatile p’
when testing malloc and similar functions, as the compiler
can optimize the call to malloc before assigning the result
to the volatile pointer variable. Instead, be more systematic
about calling malloc through a volatile function pointer
so that the compiler cannot infer that it is a malloc call.
This fix can’t be done for alloca-like functions which may not
have a function address, so continue to trust to luck there.
* tests/test-aligned-malloc.c (aligned4_malloc, aligned4_free)
(aligned8_malloc, aligned8_free, aligned16_malloc)
(aligned16_free, aligned32_malloc, aligned32_free):
* tests/test-aligned_alloc.c (aligned_alloc):
* tests/test-calloc-gnu.c (calloc):
* tests/test-free.c (free):
* tests/test-malloc-gnu.c (malloc):
* tests/test-reallocarray.c (reallocarray):
Test indirectly, by calling through a volatile pointer.
* tests/test-aligned-malloc.c, tests/test-aligned_alloc.c:
* tests/test-calloc-gnu.c, tests/test-calloc-posix.c:
* tests/test-free.c, tests/test-malloc-gnu.c:
* tests/test-malloc-posix.c, tests/test-realloc-posix.c:
* tests/test-reallocarray.c:
(main) Don’t bother making data pointers olatile, as it’s ineffective.
* tests/test-calloc-gnu.c (identity):
* tests/test-free.c (get_errno, get_errno_func):
Remove; all uses removed.
Bruno Haible [Mon, 4 Nov 2024 16:50:09 +0000 (17:50 +0100)]
aligned_alloc: Fix test failures on macOS, Solaris (regr. 2024-10-30).
* m4/aligned_alloc.m4 (gl_FUNC_ALIGNED_ALLOC): Check for the Solaris
bug. Let the test program return a bit mask. Update cross-compilation
guesses.
* lib/aligned_alloc.c: Include <errno.h>.
(aligned_alloc): Fail if the alignment is not a power of 2. Work around
the Solaris bug.
* modules/aligned_alloc (Depends-on): Add malloc-posix.
* doc/posix-functions/aligned_alloc.texi: Mention the Solaris bug.
Bruno Haible [Mon, 4 Nov 2024 10:47:23 +0000 (11:47 +0100)]
malloc, calloc, realloc tests: Work around clang optimization bug.
* tests/test-malloc-posix.c (my_malloc): New variable.
* tests/test-calloc-posix.c (my_calloc): New variable.
* tests/test-realloc-posix.c (my_realloc): New variable.
Bruno Haible [Sun, 3 Nov 2024 20:18:29 +0000 (21:18 +0100)]
crc: Fix build rules with Automake's option 'subdir-objects'.
Reported by Pádraig Brady in
<https://lists.gnu.org/archive/html/bug-gnulib/2024-11/msg00011.html>.
* build-aux/prefix-gnulib-mk: Add comments.
(usage): Mark this usage as deprecated.
* modules/crc (Makefile.am): Use %reldir%. Write $(srcdir)/. instead of
$(srcdir).
Paul Eggert [Fri, 1 Nov 2024 02:22:22 +0000 (19:22 -0700)]
malloc, realloc: fix $3 bug
Problem reported by Bruno Haible in:
https://lists.gnu.org/r/bug-gnulib/2024-10/msg00284.html
* m4/malloc.m4 (_AC_FUNC_MALLOC_IF):
* m4/realloc.m4 (_AC_FUNC_REALLOC_IF):
Sync from bleeding-edge Autoconf again.
Bruno Haible [Thu, 31 Oct 2024 23:17:20 +0000 (00:17 +0100)]
crc: Support generating the tables also when cross-compiling.
* m4/build-cc.m4: New file.
* modules/crc (Files): Add it.
(configure.ac): Invoke gl_BUILD_CC. Don't set GL_CROSS_COMPILING.
(Makefile.am): Use $(BUILD_CC) etc. instead of $(CC) etc.
Bruno Haible [Thu, 31 Oct 2024 13:23:09 +0000 (14:23 +0100)]
crc: Tweak generator.
* lib/crc-generate-table.c (print_header): Don't emit a blank line at
the end.
(print_copyright_notice): Prepend a "DO NOT EDIT" line.
(main): Fail if disk is full after we wrote part of the file.
Paul Eggert [Wed, 30 Oct 2024 19:47:02 +0000 (12:47 -0700)]
posix_memalign: check for GNU behavior with size 0
* lib/posix_memalign.c: Include stdckdint.h.
(posix_memalign): Test for overflow more straightforwardly,
and more portably to unlikely platforms where SIZE_MAX <= INT_MAX.
Treat a zero size as if it were alignment.
* m4/posix_memalign.m4 (gl_FUNC_POSIX_MEMALIGN):
* tests/test-posix_memalign.c (main):
Test zero size too. Use volatile to avoid compiler optimizations.
* modules/posix_memalign (Depends-on): Add stdckdint.
Paul Eggert [Wed, 30 Oct 2024 18:54:57 +0000 (11:54 -0700)]
aligned_alloc: check for GNU behavior with size 0
If someone ever needs to distinguish between GNU and merely POSIX
behavior we can split this into two modules, but for now just
make this module act like GNU.
* lib/aligned_alloc.c (aligned_alloc): Treat zero size like GNU.
* m4/aligned_alloc.m4 (gl_FUNC_ALIGNED_ALLOC):
* tests/test-aligned_alloc.c (main):
Test zero size too.
Paul Eggert [Wed, 30 Oct 2024 05:01:43 +0000 (22:01 -0700)]
malloc: fix recent doc bug for AIX 7.3
* doc/posix-functions/malloc.texi: Undo previous change,
as _LINUX_SOURCE_COMPAT no longer affects malloc on AIX 7.3.
It did affect it in AIX 7.1 but 7.1 is no longer supported.
Paul Eggert [Wed, 30 Oct 2024 05:01:42 +0000 (22:01 -0700)]
realloc-posix: realloc (p, 0) yields nonnull
* lib/realloc.c: Include <stdckdint.h>, not "xalloc-oversized.h".
Use of xalloc_oversized replaced by ckd_add.
(rpl_realloc) [!NEED_SANITIZED_REALLOC]:
Treat realloc (p, 0) as if it were a realloc (p, 1) that never fails.
That’s easier, reduces module dependencies,
and better supports the future alloc-0-nonnull module.
* modules/realloc-posix (Depends-on): Remove free-posix,
malloc-posix, xalloc-oversized. Add stdckdint.
Bruno Haible [Tue, 29 Oct 2024 12:48:36 +0000 (13:48 +0100)]
assert-h, stdbool: Allow mixed use of gcc/g++ and clang/clang++ again.
* m4/assert_h.m4 (gl_ASSERT_H): Improve indentation. With GCC and clang,
don't use the value of HAVE_C_STATIC_ASSERT.
* m4/c-bool.m4 (gl_C_BOOL): With GCC and clang, don't use the value of
HAVE_C_BOOL.
Bruno Haible [Sun, 27 Oct 2024 16:01:29 +0000 (17:01 +0100)]
doc: Add a module index.
* doc/Makefile (undocumented-modules.texi): New rule.
(%.info, %.html, %.dvi, %.pdf): Depend on undocumented-modules.texi.
(mostlyclean): Remove also *.m and *.tmp.
(force): New rule.
* doc/*.texi: Add module index entries.
* doc/*/*.texi: Likewise.
Paul Eggert [Sat, 26 Oct 2024 17:06:55 +0000 (10:06 -0700)]
aligned_alloc: document glibc bug 32301
* doc/posix-functions/aligned_alloc.texi:
* doc/posix-functions/posix_memalign.texi: Mention glibc bug
32301, which it is not worth our time to work around.
Paul Eggert [Sat, 26 Oct 2024 00:02:02 +0000 (17:02 -0700)]
eealloc: update commentary
Even if we remove eealloc we might as well add commentary
explaining some of its confusion, before we remove it.
* lib/eealloc.h: Update comment.
* modules/eealloc (Description):
Update, since eealloc is not glibc compatible.
Paul Eggert [Fri, 25 Oct 2024 04:52:19 +0000 (21:52 -0700)]
xalloc: port to Cheri, strict C23, realloc null
* lib/xmalloc.c [__CHERI_PURE_CAPABILITY__]: Include <cheri.h>.
(xrealloc, xreallocarray): Support Cheri. Avoid undefined
behavior in strict C23. Work better on platforms where
realloc (p, 0) returns a null pointer
Paul Eggert [Fri, 25 Oct 2024 04:34:12 +0000 (21:34 -0700)]
reallocarray: simplify
* lib/reallocarray.c (reallocarray): Use simpler workaround
for realloc glitch, which does not involve malloc.
* modules/reallocarray (Depends-on): Remove malloc-posix.
Paul Eggert [Wed, 23 Oct 2024 21:30:35 +0000 (14:30 -0700)]
backupfile: fix irealloc dependency
* modules/backupfile (Depends-on): Depend on irealloc,
not realloc-gnu, as the code now uses irealloc not realloc
and does not depend on GNU semantics anyway.
Paul Eggert [Wed, 23 Oct 2024 20:51:29 +0000 (13:51 -0700)]
realloc: still more improvements for realloc (p, 0)
* doc/posix-functions/malloc.texi: AIX is sans _LINUX_SOURCE_COMPAT.
* doc/posix-functions/realloc.texi:
Give more details about what recent POSIX versions say,
and fix some misperceptions about C89 through C11.
Document that the GNU behavior does not conform to POSIX,
and that AIX's behavior with _LINUX_SOURCE_COMPAT
matches glibc's circa 1999 behavior, not its current behavior.
Be a little less confident about realloc-gnu, since it doesn't
fully implement current glibc behavior.
Give more details about when memory leaks or false positives can occur.
* doc/posix-functions/reallocarray.texi:
Say that it shares realloc’s woes with zero sizes.
Bruno Haible [Mon, 21 Oct 2024 13:29:40 +0000 (15:29 +0200)]
realloc: Optionally check for undefined behaviour.
* m4/realloc.m4 (gl_FUNC_REALLOC_SANITIZED): New macro.
(gl_FUNC_REALLOC_POSIX): Require it. If a sanitized realloc is
requested, define NEED_SANITIZED_REALLOC and compile realloc.c.
(gl_FUNC_REALLOC_GNU): Require it. If a sanitized realloc is requested,
don't compile realloc.c a second time.
* lib/realloc.c (rpl_realloc): If NEED_SANITIZED_REALLOC is defined,
abort in the case of undefined behaviour.
Paul Eggert [Sat, 19 Oct 2024 21:18:49 +0000 (14:18 -0700)]
realloc: more improvements for realloc (p, 0)
* doc/posix-functions/realloc.texi (realloc):
Document more realloc (p, 0) gotchas: evolution of C standards,
errno values, which glibc versions do what, how programs might
misbehave, what happens when not growing.