Hi,

Alexander Sosedkin wrote:
> Hello, apologies for a terse report, hope it still comes useful.

Sure it is useful. Thanks!

> Since 528000 elements in struct gl_list_impl are __counted_by(count).
> gl_array_nx_create was probably overlooked
> and setting the count wasn't hoisted to before the memcpy.
> Clang 21 is either too smart or too dumb to consider it a problem,
> but gcc 16.2.0 + _FORTIFY_SOURCE=3, the current nixpkgs defaults,
> have four list tests failing because of that.

Thanks for the report. I don't see a test failure with
  CFLAGS="-D_FORTIFY_SOURCE=3"
but with
  CFLAGS="-O2 -D_FORTIFY_SOURCE=3"
I do see them:
  FAIL: test-array_list
  FAIL: test-avltree_list
  FAIL: test-avltreehash_list
  FAIL: test-linked_list
  FAIL: test-linkedhash_list
  FAIL: test-rbtree_list
  FAIL: test-rbtreehash_list

> Moving list->count = count; should be enough to fix it.

That would probably work, yes. I prefer to assign list->elements with
a fully initialized array.

Bruno


2026-10-30  Bruno Haible  <[email protected]>

        carray-list: Fix a counted_by constraint violation (regr. 2026-05-03).
        Reproducible with gcc-16.2.0 -O2 -D_FORTIFY_SOURCE=3.
        * lib/gl_carray_list.c (gl_carray_nx_create): Assign to list->elements
        only after filling the array.

2026-10-30  Bruno Haible  <[email protected]>

        array-list: Fix a counted_by constraint violation (regr. 2026-05-03).
        Reproducible with gcc-16.2.0 -O2 -D_FORTIFY_SOURCE=3.
        Reported by Alexander Sosedkin <[email protected]> in
        <https://lists.gnu.org/archive/html/bug-gnulib/2026-10/msg00140.html>.
        * lib/gl_array_list.c (gl_array_nx_create): Assign to list->elements
        only after filling the array.

>From e9ddddd0bd1241162942fe678f354a84dbbeb70a Mon Sep 17 00:00:00 2001
From: Bruno Haible <[email protected]>
Date: Fri, 30 Oct 2026 14:34:00 +0100
Subject: [PATCH 1/2] array-list: Fix a counted_by constraint violation (regr.
 2026-05-03).

Reproducible with gcc-16.2.0 -O2 -D_FORTIFY_SOURCE=3.

Reported by Alexander Sosedkin <[email protected]> in
<https://lists.gnu.org/archive/html/bug-gnulib/2026-10/msg00140.html>.

* lib/gl_array_list.c (gl_array_nx_create): Assign to list->elements
only after filling the array.
---
 ChangeLog           | 9 +++++++++
 lib/gl_array_list.c | 8 +++++---
 2 files changed, 14 insertions(+), 3 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 54a8b43c23..f1e9f41a08 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2026-10-30  Bruno Haible  <[email protected]>
+
+	array-list: Fix a counted_by constraint violation (regr. 2026-05-03).
+	Reproducible with gcc-16.2.0 -O2 -D_FORTIFY_SOURCE=3.
+	Reported by Alexander Sosedkin <[email protected]> in
+	<https://lists.gnu.org/archive/html/bug-gnulib/2026-10/msg00140.html>.
+	* lib/gl_array_list.c (gl_array_nx_create): Assign to list->elements
+	only after filling the array.
+
 2026-10-28  Bruno Haible  <[email protected]>
 
 	doc: Expand documentation about string descriptors.
diff --git a/lib/gl_array_list.c b/lib/gl_array_list.c
index aeb2ad71bc..c34ec7d331 100644
--- a/lib/gl_array_list.c
+++ b/lib/gl_array_list.c
@@ -95,10 +95,12 @@ gl_array_nx_create (gl_list_implementation_t implementation,
     {
       if (size_overflow_p (xtimes (count, sizeof (const void *))))
         goto fail;
-      list->elements = (const void **) malloc (count * sizeof (const void *));
-      if (list->elements == NULL)
+      const void **contents_copy =
+        (const void **) malloc (count * sizeof (const void *));
+      if (contents_copy == NULL)
         goto fail;
-      memcpy (list->elements, contents, count * sizeof (const void *));
+      memcpy (contents_copy, contents, count * sizeof (const void *));
+      list->elements = contents_copy;
     }
   else
     list->elements = NULL;
-- 
2.53.0

>From 1c56cadb553bba6b23a31fe26fde7db5b205ffc0 Mon Sep 17 00:00:00 2001
From: Bruno Haible <[email protected]>
Date: Fri, 30 Oct 2026 14:35:20 +0100
Subject: [PATCH 2/2] carray-list: Fix a counted_by constraint violation (regr.
 2026-05-03).

Reproducible with gcc-16.2.0 -O2 -D_FORTIFY_SOURCE=3.

* lib/gl_carray_list.c (gl_carray_nx_create): Assign to list->elements
only after filling the array.
---
 ChangeLog            | 7 +++++++
 lib/gl_carray_list.c | 8 +++++---
 2 files changed, 12 insertions(+), 3 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index f1e9f41a08..592bf41284 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2026-10-30  Bruno Haible  <[email protected]>
+
+	carray-list: Fix a counted_by constraint violation (regr. 2026-05-03).
+	Reproducible with gcc-16.2.0 -O2 -D_FORTIFY_SOURCE=3.
+	* lib/gl_carray_list.c (gl_carray_nx_create): Assign to list->elements
+	only after filling the array.
+
 2026-10-30  Bruno Haible  <[email protected]>
 
 	array-list: Fix a counted_by constraint violation (regr. 2026-05-03).
diff --git a/lib/gl_carray_list.c b/lib/gl_carray_list.c
index 0a7086d45e..b91b0dd0f2 100644
--- a/lib/gl_carray_list.c
+++ b/lib/gl_carray_list.c
@@ -99,10 +99,12 @@ gl_carray_nx_create (gl_list_implementation_t implementation,
     {
       if (size_overflow_p (xtimes (count, sizeof (const void *))))
         goto fail;
-      list->elements = (const void **) malloc (count * sizeof (const void *));
-      if (list->elements == NULL)
+      const void **contents_copy =
+        (const void **) malloc (count * sizeof (const void *));
+      if (contents_copy == NULL)
         goto fail;
-      memcpy (list->elements, contents, count * sizeof (const void *));
+      memcpy (contents_copy, contents, count * sizeof (const void *));
+      list->elements = contents_copy;
     }
   else
     list->elements = NULL;
-- 
2.53.0

Reply via email to