Hi,

Since GCC 11 (still with 15), APR_RING users (brigades included) get
-Warray-bounds warnings like:
  warning: array subscript 'apr_bucket[0]' is partly outside array bounds
  of 'apr_bucket_brigade[1]' [-Warray-bounds=]

and clang -fsanitize=undefined reports the same accesses at run time.
They are false positives: the sentinel is a fake element (only its
link, i.e. the head's next/prev, is ever accessed) overlaying the head
and the object around it, but the compiler sees an element-sized
access into a smaller object whenever the head is static, on the stack
or allocated in view.

So far we worked around it per user (httpd's event.c, simple_run.c and
htcacheclean.c declare whole elements just to hold a ring head,
apr_brigade_split_ex() was changed too), and other projects disabled
-Warray-bounds (Linux for GCC 11/12, haproxy).

The attached patches fix it in apr_ring.h for everyone:

- apr_ring-1.diff (trunk down to 1.6.x): with GCC/clang, the ring
macros compute the sentinel, and the element
APR_RING_SPLICE_BEFORE/AFTER() insert next to (often
APR_BRIGADE_SENTINEL()), through an empty asm hiding its origin from
the compiler, as Linux's RELOC_HIDE() or NetBSD's __launder_type() do.
No API/ABI change (APR_RING_SENTINEL() is untouched, still an address
constant), no cost (the asm emits no code). Other compilers don't
raise these false positives (so far) and keep the plain computation;
APR_RING_NO_HIDE opts out. It also documents why the ring pointers are
volatile and adds the new testring.

- apr_ring-2.diff (1.8.x and trunk, new API):
APR_RING_FOREACH_REVERSE[_SAFE]() to complete the ones which avoid
using the sentinel directly, and APR_RING_SENTINEL_HIDE() for user
code comparing with the sentinel anyway.

With gcc 15 and clang 21 at -O2, the new testring gives 88 warnings
and 30 sanitizer errors against the current apr_ring.h, none with the
patches.
What's left is user code passing APR_RING_SENTINEL() itself to
INSERT_BEFORE/AFTER with the link not first in the element (the plain
computation points before the object by itself); apr_ring.h now says
to prefer the ring macros there (INSERT_TAIL/HEAD, FOREACH[_REVERSE],
APR_RING_SENTINEL_HIDE()).

Alternatives considered:
- hiding in APR_RING_SENTINEL() itself (no longer an address constant,
breaks static initializers);
- in APR_BRIGADE_SENTINEL() only (other rings left out, and it's
apr-util's in 1.x);
- padding APR_RING_HEAD() to an element's size like in httpd (ABI change);
-  may_alias (GCC still checks the enclosing struct);
- a volatile optimizer blocker (a load per use, and a new symbol).

Feedback welcome, especially on the backports, and on any
non-GCC/clang compiler defining __GNUC__ that we still care about and
that might not be compatible with the changes.


Regards;
Yann.
commit 0ae6f950a0b856e022d16bbf5e0a01cbc3074c02
Author: ylavic <[email protected]>
Date:   Thu Nov 26 18:36:48 2026 +0100

    apr_ring: avoid false -Warray-bounds and sanitizer errors next to the sentinel.
    
    With GCC 11+ and clang -fsanitize=undefined, the ring macros' accesses
    through the sentinel (a fake element overlaying the head and the object
    around it) are reported as out of bounds when the compiler sees that
    object, e.g. a brigade that is static, on the stack or allocated in the
    same function. With GCC/clang, hide the sentinel's origin from the
    compiler with an empty asm statement inside the ring macros, while
    APR_RING_SENTINEL() stays an address constant (APR_RING_NO_HIDE opts
    out), and document why the ring pointers are volatile.
    
    * include/apr_ring.h
      (APR_RING_HEAD): Document why the pointers are volatile.
      (APR_RING_SENTINEL): Document the overlay and the macros to prefer.
      (APR_RING__SENTINEL, APR_RING__HIDE): New internal macros.
      (APR_RING_INIT, APR_RING_EMPTY, APR_RING_SPLICE_HEAD,
      APR_RING_SPLICE_TAIL, APR_RING_FOREACH, APR_RING_FOREACH_SAFE,
      APR_RING_CHECK, APR_RING_CHECK_CONSISTENCY): Use APR_RING__SENTINEL().
      (APR_RING_SPLICE_BEFORE, APR_RING_SPLICE_AFTER): Hide the origin of the
      element to insert next to (lep), which may be a sentinel.
    
    * test/testring.c: New test of the ring macros next to the sentinel.
    
    * test/Makefile.in, test/abts_tests.h, test/testutil.h, CMakeLists.txt:
      Add it.
    
    * CHANGES: Add an entry.

diff --git a/CHANGES b/CHANGES
index fc4966d72..9e728f944 100644
--- a/CHANGES
+++ b/CHANGES
@@ -1,6 +1,10 @@
                                                      -*- coding: utf-8 -*-
 Changes for APR 2.0.0
 
+  *) apr_ring: With GNU C compilers, hide the sentinel's origin in the ring
+     macros, avoiding -Warray-bounds warnings and object-size sanitizer errors
+     for rings (and brigades) next to their sentinel. [Yann Ylavic]
+
   *) apr_wait_for_io_or_timeout: Fix integer overflow for larger timeout values.
      PR 69542 [Alejandro Perez <[email protected]>]
 
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 8ba770de8..452626bc2 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -470,6 +470,7 @@ set(APR_TEST_SUITES
   testrand
   testredis
   testreslist
+  testring
   testrmm
   testshm
   testsiphash
diff --git a/include/apr_ring.h b/include/apr_ring.h
index 1cad0a3d6..18c55042c 100644
--- a/include/apr_ring.h
+++ b/include/apr_ring.h
@@ -87,6 +87,14 @@
  *
  * The first element in the ring is next after the head, and the last
  * element is just before the head.
+ *
+ * The next and prev pointers are volatile because the head is also accessed
+ * through the sentinel, as an element: one object under two types, which
+ * strict aliasing (-O2) assumes can't happen. Without volatile the compiler
+ * could e.g. not reload the head's next pointer after an APR_RING_REMOVE()
+ * of the first element, and loop forever draining a ring. A may_alias
+ * attribute isn't enough with GCC when the head is embedded in another
+ * struct (e.g. apr_bucket_brigade).
  */
 #define APR_RING_HEAD(head, elem)            \
     struct head {                            \
@@ -152,6 +160,18 @@
  * sentinel may have bad implications on very perverse implementations
  * of C -- see the warning in APR_RING_ENTRY.
  *
+ * Note finally that the sentinel overlays the head and the object around
+ * it, usually smaller than an element: only its APR_RING_ENTRY may be
+ * accessed (the head's next and prev), as the _INSERT_*(), _SPLICE_*() and
+ * _REMOVE() macros do. When the compiler sees the object containing the
+ * head, such accesses (or the mere computation, if the entry isn't first
+ * in the element) trigger -Warray-bounds warnings or object-size sanitizer
+ * errors, false positives here. The ring macros avoid them, so prefer them
+ * to APR_RING_SENTINEL() where they fit: APR_RING_INSERT_TAIL() rather than
+ * APR_RING_INSERT_BEFORE() on the sentinel, APR_RING_FOREACH() for loops;
+ * otherwise (e.g. a reverse loop), APR_RING__SENTINEL() can be compared with
+ * at run time (not in a static initializer).
+ *
  * @param hp   The head of the ring
  * @param elem The name of the element struct
  * @param link The name of the APR_RING_ENTRY in the element struct
@@ -159,6 +179,47 @@
 #define APR_RING_SENTINEL(hp, elem, link)                \
     (struct elem *)((char *)(&(hp)->next) - APR_OFFSETOF(struct elem, link))
 
+#if defined(__GNUC__) && !defined(APR_RING_NO_HIDE) && !defined(DOXYGEN)
+/* Internal, but usable for run-time comparisons with the sentinel (see
+ * above): the sentinel as the ring macros use it, and the element next to
+ * which APR_RING_SPLICE_BEFORE/AFTER() operate (possibly a sentinel given by
+ * APR_RING_SENTINEL()). With GCC/clang (__GNUC__), they go through an empty
+ * asm statement that hides their origin from the compiler (which then only
+ * can't fold some comparisons with the sentinel), the sentinel being
+ * computed as an integer; other compilers don't raise these false positives
+ * (so far), they get the plain computation, as does defining
+ * APR_RING_NO_HIDE (for a compiler that can't build the asm). p is named
+ * per expansion (per line without __COUNTER__) so that it can't shadow a
+ * name in hp or ep.
+ */
+#define APR_RING__CAT_(a, b) a##b
+#define APR_RING__CAT(a, b) APR_RING__CAT_(a, b)
+#ifdef __COUNTER__
+#define APR_RING__VAR APR_RING__CAT(apr_ring__p__, __COUNTER__)
+#else
+#define APR_RING__VAR APR_RING__CAT(apr_ring__p__, __LINE__)
+#endif
+#define APR_RING__SENTINEL_(hp, elem, link, p)                         \
+    (__extension__ ({                                                  \
+        apr_uintptr_t p = (apr_uintptr_t)&(hp)->next                   \
+                          - APR_OFFSETOF(struct elem, link);           \
+        __asm__("" : "+r"(p));                                         \
+        (struct elem *)p;                                              \
+    }))
+#define APR_RING__SENTINEL(hp, elem, link)                             \
+    APR_RING__SENTINEL_(hp, elem, link, APR_RING__VAR)
+#define APR_RING__HIDE_(ep, p)                                         \
+    (__extension__ ({                                                  \
+        __typeof__((ep) + 0) p = (ep); /* no const/volatile */         \
+        __asm__("" : "+r"(p));                                         \
+        p;                                                             \
+    }))
+#define APR_RING__HIDE(ep) APR_RING__HIDE_(ep, APR_RING__VAR)
+#else
+#define APR_RING__SENTINEL(hp, elem, link) APR_RING_SENTINEL(hp, elem, link)
+#define APR_RING__HIDE(ep) (ep)
+#endif
+
 /**
  * The first element of the ring
  * @param hp   The head of the ring
@@ -189,9 +250,9 @@
  * @param elem The name of the element struct
  * @param link The name of the APR_RING_ENTRY in the element struct
  */
-#define APR_RING_INIT(hp, elem, link) do {                \
-    APR_RING_FIRST((hp)) = APR_RING_SENTINEL((hp), elem, link);    \
-    APR_RING_LAST((hp))  = APR_RING_SENTINEL((hp), elem, link);    \
+#define APR_RING_INIT(hp, elem, link) do {                          \
+    APR_RING_FIRST((hp)) = APR_RING__SENTINEL((hp), elem, link);    \
+    APR_RING_LAST((hp))  = APR_RING__SENTINEL((hp), elem, link);    \
     } while (0)
 
 /**
@@ -202,7 +263,7 @@
  * @return true or false
  */
 #define APR_RING_EMPTY(hp, elem, link)                    \
-    (APR_RING_FIRST((hp)) == APR_RING_SENTINEL((hp), elem, link))
+    (APR_RING_FIRST((hp)) == APR_RING__SENTINEL((hp), elem, link))
 
 /**
  * Initialize a singleton element
@@ -225,11 +286,11 @@
  * @param epN  Last element in the sequence to splice in
  * @param link The name of the APR_RING_ENTRY in the element struct
  */
-#define APR_RING_SPLICE_BEFORE(lep, ep1, epN, link) do {        \
-    APR_RING_NEXT((epN), link) = (lep);                \
-    APR_RING_PREV((ep1), link) = APR_RING_PREV((lep), link);    \
-    APR_RING_NEXT(APR_RING_PREV((lep), link), link) = (ep1);    \
-    APR_RING_PREV((lep), link) = (epN);                \
+#define APR_RING_SPLICE_BEFORE(lep, ep1, epN, link) do {                   \
+    APR_RING_NEXT((epN), link) = APR_RING__HIDE(lep);                      \
+    APR_RING_PREV((ep1), link) = APR_RING_PREV(APR_RING__HIDE(lep), link); \
+    APR_RING_NEXT(APR_RING_PREV(APR_RING__HIDE(lep), link), link) = (ep1); \
+    APR_RING_PREV(APR_RING__HIDE(lep), link) = (epN);                      \
     } while (0)
 
 /**
@@ -242,11 +303,11 @@
  * @param epN  Last element in the sequence to splice in
  * @param link The name of the APR_RING_ENTRY in the element struct
  */
-#define APR_RING_SPLICE_AFTER(lep, ep1, epN, link) do {         \
-    APR_RING_PREV((ep1), link) = (lep);                \
-    APR_RING_NEXT((epN), link) = APR_RING_NEXT((lep), link);    \
-    APR_RING_PREV(APR_RING_NEXT((lep), link), link) = (epN);    \
-    APR_RING_NEXT((lep), link) = (ep1);                \
+#define APR_RING_SPLICE_AFTER(lep, ep1, epN, link) do {                    \
+    APR_RING_PREV((ep1), link) = APR_RING__HIDE(lep);                      \
+    APR_RING_NEXT((epN), link) = APR_RING_NEXT(APR_RING__HIDE(lep), link); \
+    APR_RING_PREV(APR_RING_NEXT(APR_RING__HIDE(lep), link), link) = (epN); \
+    APR_RING_NEXT(APR_RING__HIDE(lep), link) = (ep1);                      \
     } while (0)
 
 /**
@@ -283,11 +344,11 @@
  * @param elem The name of the element struct
  * @param link The name of the APR_RING_ENTRY in the element struct
  */
-#define APR_RING_SPLICE_HEAD(hp, ep1, epN, elem, link) do {        \
-    APR_RING_PREV((ep1), link) = APR_RING_SENTINEL((hp), elem, link);\
-    APR_RING_NEXT((epN), link) = APR_RING_FIRST((hp));        \
-    APR_RING_PREV(APR_RING_FIRST((hp)), link) = (epN);        \
-    APR_RING_FIRST((hp)) = (ep1);                    \
+#define APR_RING_SPLICE_HEAD(hp, ep1, epN, elem, link) do {            \
+    APR_RING_PREV((ep1), link) = APR_RING__SENTINEL((hp), elem, link); \
+    APR_RING_NEXT((epN), link) = APR_RING_FIRST((hp));                 \
+    APR_RING_PREV(APR_RING_FIRST((hp)), link) = (epN);                 \
+    APR_RING_FIRST((hp)) = (ep1);                                      \
     } while (0)
 
 /**
@@ -299,11 +360,11 @@
  * @param elem The name of the element struct
  * @param link The name of the APR_RING_ENTRY in the element struct
  */
-#define APR_RING_SPLICE_TAIL(hp, ep1, epN, elem, link) do {        \
-    APR_RING_NEXT((epN), link) = APR_RING_SENTINEL((hp), elem, link);\
-    APR_RING_PREV((ep1), link) = APR_RING_LAST((hp));               \
-    APR_RING_NEXT(APR_RING_LAST((hp)), link) = (ep1);               \
-    APR_RING_LAST((hp)) = (epN);                                    \
+#define APR_RING_SPLICE_TAIL(hp, ep1, epN, elem, link) do {            \
+    APR_RING_NEXT((epN), link) = APR_RING__SENTINEL((hp), elem, link); \
+    APR_RING_PREV((ep1), link) = APR_RING_LAST((hp));                  \
+    APR_RING_NEXT(APR_RING_LAST((hp)), link) = (ep1);                  \
+    APR_RING_LAST((hp)) = (epN);                                       \
     } while (0)
 
 /**
@@ -390,7 +451,7 @@
  */
 #define APR_RING_FOREACH(ep, head, elem, link)                          \
     for (ep = APR_RING_FIRST(head);                                     \
-         ep != APR_RING_SENTINEL(head, elem, link);                     \
+         ep != APR_RING__SENTINEL(head, elem, link);                    \
          ep = APR_RING_NEXT(ep, link))
 
 /**
@@ -403,7 +464,7 @@
  */
 #define APR_RING_FOREACH_SAFE(ep1, ep2, head, elem, link)               \
     for (ep1 = APR_RING_FIRST(head), ep2 = APR_RING_NEXT(ep1, link);    \
-         ep1 != APR_RING_SENTINEL(head, elem, link);                    \
+         ep1 != APR_RING__SENTINEL(head, elem, link);                   \
          ep1 = ep2, ep2 = APR_RING_NEXT(ep1, link))
 
 /* Debugging tools: */
@@ -416,7 +477,7 @@
     fprintf(stderr, "*** %s %p\n", msg, ptr)
 
 #define APR_RING_CHECK(hp, elem, link, msg)                \
-    APR_RING_CHECK_ELEM(APR_RING_SENTINEL(hp, elem, link), elem, link, msg)
+    APR_RING_CHECK_ELEM(APR_RING__SENTINEL(hp, elem, link), elem, link, msg)
 
 #define APR_RING_CHECK_ELEM(ep, elem, link, msg) do {            \
     struct elem *start = (ep);                    \
@@ -445,8 +506,8 @@
     fprintf(stderr, "*** ring check end\n");            \
     } while (0)
 
-#define APR_RING_CHECK_CONSISTENCY(hp, elem, link)            \
-    APR_RING_CHECK_ELEM_CONSISTENCY(APR_RING_SENTINEL(hp, elem, link),\
+#define APR_RING_CHECK_CONSISTENCY(hp, elem, link)                      \
+    APR_RING_CHECK_ELEM_CONSISTENCY(APR_RING__SENTINEL(hp, elem, link), \
                     elem, link)
 
 #define APR_RING_CHECK_ELEM_CONSISTENCY(ep, elem, link) do {        \
diff --git a/test/Makefile.in b/test/Makefile.in
index 8189b8cd1..19c8087a1 100644
--- a/test/Makefile.in
+++ b/test/Makefile.in
@@ -38,7 +38,7 @@ TESTS = testtime.lo teststr.lo testvsn.lo testipsub.lo testshm.lo	\
 	testreslist.lo testbase64.lo testhooks.lo testlfsabi.lo		\
 	testlfsabi32.lo testlfsabi64.lo testescape.lo testskiplist.lo	\
 	testsiphash.lo testredis.lo testencode.lo testjson.lo           \
-	testjose.lo testbuffer.lo testldap.lo testcharset.lo
+	testjose.lo testbuffer.lo testldap.lo testcharset.lo testring.lo
 
 OTHER_PROGRAMS = \
 	echod@EXEEXT@ \
diff --git a/test/abts_tests.h b/test/abts_tests.h
index 2cdb40313..80b1c3280 100644
--- a/test/abts_tests.h
+++ b/test/abts_tests.h
@@ -91,6 +91,7 @@ const struct testlist {
     {testdbm},
     {testqueue},
     {testreslist},
+    {testring},
     {testlfsabi},
     {testskiplist},
     {testsiphash},
diff --git a/test/testring.c b/test/testring.c
new file mode 100644
index 000000000..054ab8c3c
--- /dev/null
+++ b/test/testring.c
@@ -0,0 +1,357 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/* The ring macros next to the sentinel (see APR_RING_SENTINEL). Running the
+ * tests checks the rings' behaviour only; what the ring macros' hiding of
+ * the sentinel's origin avoids shows when building this file with GCC or
+ * clang at -O2 and -Warray-bounds (-Wall), or -fsanitize=undefined
+ * (object-size): without it, respectively warnings and runtime errors.
+ */
+
+#include "testutil.h"
+#include "apr.h"
+#include "apr_general.h"
+#include "apr_pools.h"
+#include "apr_ring.h"
+#include "apr_buckets.h"
+
+#include <stdlib.h>
+
+/* Elements larger than the object containing their ring head, with the
+ * APR_RING_ENTRY first (like apr_bucket) or not, so that the sentinel
+ * overlays bytes outside of that object.
+ */
+struct first_elem {
+    APR_RING_ENTRY(first_elem) link;
+    int val;
+    char data[64];
+};
+APR_RING_HEAD(first_ring, first_elem);
+
+struct inner_elem {
+    char data[64];
+    APR_RING_ENTRY(inner_elem) link;
+    int val;
+};
+APR_RING_HEAD(inner_ring, inner_elem);
+
+/* Like apr_bucket_brigade, a head embedded in a small struct */
+struct first_holder {
+    void *before;
+    struct first_ring list;
+    void *after;
+};
+struct inner_holder {
+    void *before;
+    struct inner_ring list;
+    void *after;
+};
+
+#define NELTS 4
+
+/* The two first insertions next to the sentinel, written differently per
+ * kind of element. With the APR_RING_ENTRY first in the element (like
+ * apr_bucket), on APR_RING_SENTINEL() as user code does (e.g.
+ * APR_BUCKET_INSERT_BEFORE(APR_BRIGADE_SENTINEL(bb), e)): this goes through
+ * the hiding of the origin of the element APR_RING_SPLICE_BEFORE/AFTER()
+ * operate next to, which the object-size sanitizer checks. With the entry
+ * not first, with APR_RING_INSERT_TAIL/HEAD(): APR_RING_SENTINEL() is then
+ * itself out of the bounds of the object containing the head (see
+ * APR_RING_SENTINEL), a -Warray-bounds warning that no hiding inside the
+ * macros can avoid.
+ */
+#define INSERT_FIRST_ELEM(hp, ep1, ep0, elem, link) do {                      \
+    APR_RING_INSERT_BEFORE(APR_RING_SENTINEL((hp), elem, link), (ep1), link); \
+    APR_RING_INSERT_AFTER(APR_RING_SENTINEL((hp), elem, link), (ep0), link);  \
+} while (0)
+#define INSERT_INNER_ELEM(hp, ep1, ep0, elem, link) do {                    \
+    APR_RING_INSERT_TAIL((hp), (ep1), elem, link);                          \
+    APR_RING_INSERT_HEAD((hp), (ep0), elem, link);                          \
+} while (0)
+
+/* Fill the ring with the elements (values 1..NELTS) by inserting next to
+ * the sentinel, check the order both ways and the sentinel's links (with
+ * APR_RING__SENTINEL(), for the reason above), then empty it by removing
+ * the first and last elements, which accesses the sentinel's links too,
+ * then the others with the safe loop.
+ * Returns the number of errors.
+ */
+#define RING_EXERCISE(hp, elts, elem, link, errs, insert) do {              \
+    struct elem *ep_, *ep2_;                                                \
+    int i_, v_;                                                             \
+    APR_RING_INIT((hp), elem, link);                                        \
+    if (!APR_RING_EMPTY((hp), elem, link)) {                                \
+        (errs)++;                                                           \
+    }                                                                       \
+    for (i_ = 0; i_ < NELTS; i_++) {                                        \
+        (elts)[i_].val = i_ + 1;                                            \
+        APR_RING_ELEM_INIT(&(elts)[i_], link);                              \
+    }                                                                       \
+    /* 2, then 1 2, then 1 2 3, then 1 2 3 4 */                             \
+    insert((hp), &(elts)[1], &(elts)[0], elem, link);                       \
+    APR_RING_INSERT_TAIL((hp), &(elts)[2], elem, link);                     \
+    ep_ = APR_RING_LAST((hp));                                              \
+    APR_RING_INSERT_AFTER(ep_, &(elts)[3], link);                           \
+    v_ = 0;                                                                 \
+    APR_RING_FOREACH(ep_, (hp), elem, link) {                               \
+        if (ep_->val != ++v_) {                                             \
+            (errs)++;                                                       \
+        }                                                                   \
+    }                                                                       \
+    if (v_ != NELTS) {                                                      \
+        (errs)++;                                                           \
+    }                                                                       \
+    for (ep_ = APR_RING_LAST((hp));                                         \
+         ep_ != APR_RING__SENTINEL((hp), elem, link);                       \
+         ep_ = APR_RING_PREV(ep_, link)) {                                  \
+        if (ep_->val != v_--) {                                             \
+            (errs)++;                                                       \
+        }                                                                   \
+    }                                                                       \
+    if (v_ != 0) {                                                          \
+        (errs)++;                                                           \
+    }                                                                       \
+    if (APR_RING_NEXT(APR_RING_LAST((hp)), link)                            \
+            != APR_RING__SENTINEL((hp), elem, link)                         \
+        || APR_RING_PREV(APR_RING_FIRST((hp)), link)                        \
+               != APR_RING__SENTINEL((hp), elem, link)) {                   \
+        (errs)++;                                                           \
+    }                                                                       \
+    ep_ = APR_RING_LAST((hp));                                              \
+    APR_RING_REMOVE(ep_, link);                                             \
+    ep_ = APR_RING_FIRST((hp));                                             \
+    APR_RING_REMOVE(ep_, link);                                             \
+    if (APR_RING_FIRST((hp))->val != 2 || APR_RING_LAST((hp))->val != 3) {  \
+        (errs)++;                                                           \
+    }                                                                       \
+    /* 2 3, then 3, then empty */                                           \
+    APR_RING_FOREACH_SAFE(ep_, ep2_, (hp), elem, link) {                    \
+        if (ep_->val == 2) {                                                \
+            APR_RING_REMOVE(ep_, link);                                     \
+        }                                                                   \
+    }                                                                       \
+    if (APR_RING_FIRST((hp)) != APR_RING_LAST((hp))                         \
+        || APR_RING_FIRST((hp))->val != 3) {                                \
+        (errs)++;                                                           \
+    }                                                                       \
+    APR_RING_FOREACH_SAFE(ep_, ep2_, (hp), elem, link) {                    \
+        APR_RING_REMOVE(ep_, link);                                         \
+    }                                                                       \
+    if (!APR_RING_EMPTY((hp), elem, link)) {                                \
+        (errs)++;                                                           \
+    }                                                                       \
+} while (0)
+
+static struct first_ring static_first_ring;
+static struct inner_ring static_inner_ring;
+static struct first_holder static_first_holder;
+static struct inner_holder static_inner_holder;
+
+static void test_ring_static(abts_case *tc, void *data)
+{
+    struct first_elem fe[NELTS];
+    struct inner_elem ie[NELTS];
+    int errs = 0;
+
+    RING_EXERCISE(&static_first_ring, fe, first_elem, link, errs,
+                  INSERT_FIRST_ELEM);
+    ABTS_INT_EQUAL(tc, 0, errs);
+    RING_EXERCISE(&static_inner_ring, ie, inner_elem, link, errs,
+                  INSERT_INNER_ELEM);
+    ABTS_INT_EQUAL(tc, 0, errs);
+    RING_EXERCISE(&static_first_holder.list, fe, first_elem, link, errs,
+                  INSERT_FIRST_ELEM);
+    ABTS_INT_EQUAL(tc, 0, errs);
+    RING_EXERCISE(&static_inner_holder.list, ie, inner_elem, link, errs,
+                  INSERT_INNER_ELEM);
+    ABTS_INT_EQUAL(tc, 0, errs);
+}
+
+static void test_ring_stack(abts_case *tc, void *data)
+{
+    struct first_elem fe[NELTS];
+    struct inner_elem ie[NELTS];
+    struct first_ring first_ring;
+    struct inner_ring inner_ring;
+    struct first_holder first_holder;
+    struct inner_holder inner_holder;
+    int errs = 0;
+
+    RING_EXERCISE(&first_ring, fe, first_elem, link, errs,
+                  INSERT_FIRST_ELEM);
+    ABTS_INT_EQUAL(tc, 0, errs);
+    RING_EXERCISE(&inner_ring, ie, inner_elem, link, errs,
+                  INSERT_INNER_ELEM);
+    ABTS_INT_EQUAL(tc, 0, errs);
+    RING_EXERCISE(&first_holder.list, fe, first_elem, link, errs,
+                  INSERT_FIRST_ELEM);
+    ABTS_INT_EQUAL(tc, 0, errs);
+    RING_EXERCISE(&inner_holder.list, ie, inner_elem, link, errs,
+                  INSERT_INNER_ELEM);
+    ABTS_INT_EQUAL(tc, 0, errs);
+}
+
+static void test_ring_alloc(abts_case *tc, void *data)
+{
+    struct first_elem fe[NELTS];
+    struct inner_elem ie[NELTS];
+    struct first_holder *first_holder;
+    struct inner_holder *inner_holder;
+    int errs = 0;
+
+    first_holder = apr_palloc(p, sizeof(*first_holder));
+    RING_EXERCISE(&first_holder->list, fe, first_elem, link, errs,
+                  INSERT_FIRST_ELEM);
+    ABTS_INT_EQUAL(tc, 0, errs);
+    inner_holder = apr_palloc(p, sizeof(*inner_holder));
+    RING_EXERCISE(&inner_holder->list, ie, inner_elem, link, errs,
+                  INSERT_INNER_ELEM);
+    ABTS_INT_EQUAL(tc, 0, errs);
+
+    first_holder = malloc(sizeof(*first_holder));
+    ABTS_PTR_NOTNULL(tc, first_holder);
+    RING_EXERCISE(&first_holder->list, fe, first_elem, link, errs,
+                  INSERT_FIRST_ELEM);
+    ABTS_INT_EQUAL(tc, 0, errs);
+    free(first_holder);
+    inner_holder = malloc(sizeof(*inner_holder));
+    ABTS_PTR_NOTNULL(tc, inner_holder);
+    RING_EXERCISE(&inner_holder->list, ie, inner_elem, link, errs,
+                  INSERT_INNER_ELEM);
+    ABTS_INT_EQUAL(tc, 0, errs);
+    free(inner_holder);
+}
+
+/* Drain a ring by removing its first element until it's empty: the removal
+ * writes the head's next pointer through the sentinel (as an element), and
+ * the loop must reload it through the head. Called through a volatile
+ * function pointer so that the compiler knows nothing about the ring.
+ */
+static int drain_ring(struct first_holder *h)
+{
+    struct first_elem *ep;
+    int n = 0;
+    while (!APR_RING_EMPTY(&h->list, first_elem, link)) {
+        ep = APR_RING_FIRST(&h->list);
+        APR_RING_REMOVE(ep, link);
+        if (++n > NELTS) {
+            break;
+        }
+    }
+    return n;
+}
+static int (*volatile drain_ring_fn)(struct first_holder *) = drain_ring;
+
+static void test_ring_drain(abts_case *tc, void *data)
+{
+    struct first_elem fe[NELTS];
+    struct first_holder h;
+    int i;
+
+    APR_RING_INIT(&h.list, first_elem, link);
+    for (i = 0; i < NELTS; i++) {
+        APR_RING_INSERT_TAIL(&h.list, &fe[i], first_elem, link);
+    }
+    ABTS_INT_EQUAL(tc, NELTS, drain_ring_fn(&h));
+    ABTS_TRUE(tc, APR_RING_EMPTY(&h.list, first_elem, link));
+}
+
+static void test_ring_concat(abts_case *tc, void *data)
+{
+    struct first_elem fe[NELTS], *ep;
+    struct first_ring r1, r2;
+    int v = 0;
+
+    APR_RING_INIT(&r1, first_elem, link);
+    APR_RING_INIT(&r2, first_elem, link);
+    fe[0].val = 1;
+    fe[1].val = 2;
+    fe[2].val = 3;
+    fe[3].val = 4;
+    APR_RING_INSERT_TAIL(&r1, &fe[1], first_elem, link);
+    APR_RING_INSERT_TAIL(&r2, &fe[0], first_elem, link);
+    APR_RING_PREPEND(&r1, &r2, first_elem, link);
+    ABTS_TRUE(tc, APR_RING_EMPTY(&r2, first_elem, link));
+    APR_RING_INSERT_TAIL(&r2, &fe[2], first_elem, link);
+    APR_RING_INSERT_TAIL(&r2, &fe[3], first_elem, link);
+    APR_RING_CONCAT(&r1, &r2, first_elem, link);
+    ABTS_TRUE(tc, APR_RING_EMPTY(&r2, first_elem, link));
+    APR_RING_FOREACH(ep, &r1, first_elem, link) {
+        ABTS_INT_EQUAL(tc, ++v, ep->val);
+    }
+    ABTS_INT_EQUAL(tc, NELTS, v);
+    APR_RING_UNSPLICE(&fe[0], &fe[3], link);
+    APR_RING_INIT(&r1, first_elem, link);
+    ABTS_TRUE(tc, APR_RING_EMPTY(&r1, first_elem, link));
+}
+
+/* A brigade not created by apr_brigade_create() */
+static apr_bucket_brigade static_bb;
+
+static apr_size_t brigade_exercise(apr_bucket_brigade *bb,
+                                   apr_bucket_alloc_t *ba)
+{
+    apr_bucket *e;
+    apr_size_t len = 0;
+
+    bb->p = p;
+    bb->bucket_alloc = ba;
+    APR_RING_INIT(&bb->list, apr_bucket, link);
+
+    e = apr_bucket_immortal_create("bc", 2, ba);
+    APR_BUCKET_INSERT_BEFORE(APR_BRIGADE_SENTINEL(bb), e);
+    e = apr_bucket_immortal_create("a", 1, ba);
+    APR_BUCKET_INSERT_AFTER(APR_BRIGADE_SENTINEL(bb), e);
+    e = apr_bucket_immortal_create("def", 3, ba);
+    APR_BRIGADE_INSERT_TAIL(bb, e);
+    for (e = APR_BRIGADE_LAST(bb); e != APR_BRIGADE_SENTINEL(bb);
+         e = APR_BUCKET_PREV(e)) {
+        len = len * 10 + e->length;
+    }
+    while (!APR_BRIGADE_EMPTY(bb)) {
+        e = APR_BRIGADE_FIRST(bb);
+        APR_BUCKET_REMOVE(e);
+        apr_bucket_destroy(e);
+    }
+    return len;
+}
+
+static void test_ring_brigade(abts_case *tc, void *data)
+{
+    apr_bucket_alloc_t *ba = apr_bucket_alloc_create(p);
+    apr_bucket_brigade stack_bb, *bb;
+
+    ABTS_SIZE_EQUAL(tc, 321, brigade_exercise(&static_bb, ba));
+    ABTS_SIZE_EQUAL(tc, 321, brigade_exercise(&stack_bb, ba));
+    bb = apr_palloc(p, sizeof(*bb));
+    ABTS_SIZE_EQUAL(tc, 321, brigade_exercise(bb, ba));
+
+    apr_bucket_alloc_destroy(ba);
+}
+
+abts_suite *testring(abts_suite *suite)
+{
+    suite = ADD_SUITE(suite);
+
+    abts_run_test(suite, test_ring_static, NULL);
+    abts_run_test(suite, test_ring_stack, NULL);
+    abts_run_test(suite, test_ring_alloc, NULL);
+    abts_run_test(suite, test_ring_drain, NULL);
+    abts_run_test(suite, test_ring_concat, NULL);
+    abts_run_test(suite, test_ring_brigade, NULL);
+
+    return suite;
+}
diff --git a/test/testutil.h b/test/testutil.h
index 8c838e4bd..f36ad5c37 100644
--- a/test/testutil.h
+++ b/test/testutil.h
@@ -125,6 +125,7 @@ abts_suite *testdate(abts_suite *suite);
 abts_suite *testmemcache(abts_suite *suite);
 abts_suite *testredis(abts_suite *suite);
 abts_suite *testreslist(abts_suite *suite);
+abts_suite *testring(abts_suite *suite);
 abts_suite *testqueue(abts_suite *suite);
 abts_suite *testxml(abts_suite *suite);
 abts_suite *testxlate(abts_suite *suite);
commit 85b283ee5115e415da3329f28e7421c73e90e13d
Author: ylavic <[email protected]>
Date:   Thu Nov 26 18:36:58 2026 +0100

    apr_ring: add APR_RING_SENTINEL_HIDE() and APR_RING_FOREACH_REVERSE[_SAFE]().
    
    Comparisons with APR_RING_SENTINEL() still trigger -Warray-bounds when
    the APR_RING_ENTRY is not first in the element and the compiler sees the
    object containing the head, since the plain computation points before
    it. Give users the sentinel the ring macros use, its origin hidden, and
    reverse loops built on it like APR_RING_FOREACH[_SAFE]().
    
    * include/apr_ring.h
      (APR_RING_SENTINEL): Recommend the new macros in the docs instead of the
      internal APR_RING__SENTINEL().
      (APR_RING__SENTINEL, APR_RING__HIDE): Name APR_RING_SENTINEL_HIDE() as
      the public sentinel.
      (APR_RING_SENTINEL_HIDE, APR_RING_FOREACH_REVERSE,
      APR_RING_FOREACH_REVERSE_SAFE): New macros.
    
    * test/testring.c (RING_EXERCISE): Check with the new macros.
    
    * CHANGES: Add an entry.

diff --git a/CHANGES b/CHANGES
index 9e728f944..1572d53ec 100644
--- a/CHANGES
+++ b/CHANGES
@@ -1,6 +1,9 @@
                                                      -*- coding: utf-8 -*-
 Changes for APR 2.0.0
 
+  *) apr_ring: Add APR_RING_SENTINEL_HIDE(), APR_RING_FOREACH_REVERSE()
+     and APR_RING_FOREACH_REVERSE_SAFE(). [Yann Ylavic]
+
   *) apr_ring: With GNU C compilers, hide the sentinel's origin in the ring
      macros, avoiding -Warray-bounds warnings and object-size sanitizer errors
      for rings (and brigades) next to their sentinel. [Yann Ylavic]
diff --git a/include/apr_ring.h b/include/apr_ring.h
index 18c55042c..47e9f81c1 100644
--- a/include/apr_ring.h
+++ b/include/apr_ring.h
@@ -168,9 +168,10 @@
  * in the element) trigger -Warray-bounds warnings or object-size sanitizer
  * errors, false positives here. The ring macros avoid them, so prefer them
  * to APR_RING_SENTINEL() where they fit: APR_RING_INSERT_TAIL() rather than
- * APR_RING_INSERT_BEFORE() on the sentinel, APR_RING_FOREACH() for loops;
- * otherwise (e.g. a reverse loop), APR_RING__SENTINEL() can be compared with
- * at run time (not in a static initializer).
+ * APR_RING_INSERT_BEFORE() on the sentinel, APR_RING_FOREACH() and
+ * APR_RING_FOREACH_REVERSE() for loops; otherwise compare with
+ * APR_RING_SENTINEL_HIDE() (at run time only, not in a static
+ * initializer).
  *
  * @param hp   The head of the ring
  * @param elem The name of the element struct
@@ -180,9 +181,9 @@
     (struct elem *)((char *)(&(hp)->next) - APR_OFFSETOF(struct elem, link))
 
 #if defined(__GNUC__) && !defined(APR_RING_NO_HIDE) && !defined(DOXYGEN)
-/* Internal, but usable for run-time comparisons with the sentinel (see
- * above): the sentinel as the ring macros use it, and the element next to
- * which APR_RING_SPLICE_BEFORE/AFTER() operate (possibly a sentinel given by
+/* Not part of the API: the sentinel as the ring macros use it (public as
+ * APR_RING_SENTINEL_HIDE()), and the element next to which
+ * APR_RING_SPLICE_BEFORE/AFTER() operate (possibly a sentinel given by
  * APR_RING_SENTINEL()). With GCC/clang (__GNUC__), they go through an empty
  * asm statement that hides their origin from the compiler (which then only
  * can't fold some comparisons with the sentinel), the sentinel being
@@ -220,6 +221,19 @@
 #define APR_RING__HIDE(ep) (ep)
 #endif
 
+/**
+ * The sentinel as the ring macros use it: the same element as
+ * APR_RING_SENTINEL(), but with GCC/clang its origin is hidden from the
+ * compiler, so that it can't trigger -Warray-bounds warnings or object-size
+ * sanitizer errors (see APR_RING_SENTINEL). It is not an address constant,
+ * so it can't be used in a static initializer.
+ * @param hp   The head of the ring
+ * @param elem The name of the element struct
+ * @param link The name of the APR_RING_ENTRY in the element struct
+ */
+#define APR_RING_SENTINEL_HIDE(hp, elem, link)                         \
+    APR_RING__SENTINEL(hp, elem, link)
+
 /**
  * The first element of the ring
  * @param hp   The head of the ring
@@ -467,6 +481,32 @@
          ep1 != APR_RING__SENTINEL(head, elem, link);                   \
          ep1 = ep2, ep2 = APR_RING_NEXT(ep1, link))
 
+/**
+ * Iterate over a ring in reverse order
+ * @param ep The current element
+ * @param head The head of the ring
+ * @param elem The name of the element struct
+ * @param link The name of the APR_RING_ENTRY in the element struct
+ */
+#define APR_RING_FOREACH_REVERSE(ep, head, elem, link)                  \
+    for (ep = APR_RING_LAST(head);                                      \
+         ep != APR_RING__SENTINEL(head, elem, link);                    \
+         ep = APR_RING_PREV(ep, link))
+
+/**
+ * Iterate over a ring in reverse order safe against removal of the current
+ * element
+ * @param ep1 The current element
+ * @param ep2 Iteration cursor
+ * @param head The head of the ring
+ * @param elem The name of the element struct
+ * @param link The name of the APR_RING_ENTRY in the element struct
+ */
+#define APR_RING_FOREACH_REVERSE_SAFE(ep1, ep2, head, elem, link)       \
+    for (ep1 = APR_RING_LAST(head), ep2 = APR_RING_PREV(ep1, link);     \
+         ep1 != APR_RING__SENTINEL(head, elem, link);                   \
+         ep1 = ep2, ep2 = APR_RING_PREV(ep1, link))
+
 /* Debugging tools: */
 
 #ifdef APR_RING_DEBUG
diff --git a/test/testring.c b/test/testring.c
index 054ab8c3c..d43cd7da0 100644
--- a/test/testring.c
+++ b/test/testring.c
@@ -84,9 +84,9 @@ struct inner_holder {
 
 /* Fill the ring with the elements (values 1..NELTS) by inserting next to
  * the sentinel, check the order both ways and the sentinel's links (with
- * APR_RING__SENTINEL(), for the reason above), then empty it by removing
- * the first and last elements, which accesses the sentinel's links too,
- * then the others with the safe loop.
+ * APR_RING_SENTINEL_HIDE(), for the reason above), then empty it by
+ * removing the first and last elements, which accesses the sentinel's
+ * links too, then the others with the safe loops.
  * Returns the number of errors.
  */
 #define RING_EXERCISE(hp, elts, elem, link, errs, insert) do {              \
@@ -114,9 +114,7 @@ struct inner_holder {
     if (v_ != NELTS) {                                                      \
         (errs)++;                                                           \
     }                                                                       \
-    for (ep_ = APR_RING_LAST((hp));                                         \
-         ep_ != APR_RING__SENTINEL((hp), elem, link);                       \
-         ep_ = APR_RING_PREV(ep_, link)) {                                  \
+    APR_RING_FOREACH_REVERSE(ep_, (hp), elem, link) {                       \
         if (ep_->val != v_--) {                                             \
             (errs)++;                                                       \
         }                                                                   \
@@ -125,9 +123,9 @@ struct inner_holder {
         (errs)++;                                                           \
     }                                                                       \
     if (APR_RING_NEXT(APR_RING_LAST((hp)), link)                            \
-            != APR_RING__SENTINEL((hp), elem, link)                         \
+            != APR_RING_SENTINEL_HIDE((hp), elem, link)                     \
         || APR_RING_PREV(APR_RING_FIRST((hp)), link)                        \
-               != APR_RING__SENTINEL((hp), elem, link)) {                   \
+               != APR_RING_SENTINEL_HIDE((hp), elem, link)) {               \
         (errs)++;                                                           \
     }                                                                       \
     ep_ = APR_RING_LAST((hp));                                              \
@@ -147,7 +145,7 @@ struct inner_holder {
         || APR_RING_FIRST((hp))->val != 3) {                                \
         (errs)++;                                                           \
     }                                                                       \
-    APR_RING_FOREACH_SAFE(ep_, ep2_, (hp), elem, link) {                    \
+    APR_RING_FOREACH_REVERSE_SAFE(ep_, ep2_, (hp), elem, link) {            \
         APR_RING_REMOVE(ep_, link);                                         \
     }                                                                       \
     if (!APR_RING_EMPTY((hp), elem, link)) {                                \

Reply via email to