]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
CLEANUP: http-conv: index the captures array with hdr->index in the converters
authorWilly Tarreau <w@1wt.eu>
Mon, 27 Jul 2026 10:03:39 +0000 (12:03 +0200)
committerChristopher Faulet <cfaulet@haproxy.com>
Mon, 27 Jul 2026 13:32:11 +0000 (15:32 +0200)
smp_conv_req_capture() and smp_conv_res_capture() look the capture slot up by
walking the proxy's capture list backwards until the decreasing counter matches
the requested id, then allocate the storage using <hdr->index> but write it
using <idx>:

if (smp->strm->req_cap[hdr->index] == NULL)
smp->strm->req_cap[hdr->index] = pool_alloc(hdr->pool);
...
memcpy(smp->strm->req_cap[idx], smp->data.u.str.area, len);

Both are in fact always equal, because every place that appends to the list
(cfgparse-listen.c, proxy.c, tcp_rules.c and http_act.c) does "hdr->next = px->
req_cap; hdr->index = px->nb_req_cap++; px->req_cap = hdr;", so the head always
carries the highest index and the walk keeps hdr->index == i. But relying on
that invariant to index an array while the neighbouring lines use the field that
actually describes the slot is confusing, and it silently ties these two
functions to the way the list happens to be built.

Let's use hdr->index everywhere. No functional change.

src/http_conv.c

index ed276b2e8ea2cd66195bca6aa515896ab1f373dd..65b75239ec9ab9c996d8f41c6ad56a2a1e7264b2 100644 (file)
@@ -402,9 +402,12 @@ static int smp_conv_req_capture(const struct arg *args, struct sample *smp, void
        if (len > hdr->len)
                len = hdr->len;
 
-       /* Capture input data. */
-       memcpy(smp->strm->req_cap[idx], smp->data.u.str.area, len);
-       smp->strm->req_cap[idx][len] = '\0';
+       /* Capture input data. Use hdr->index and not idx: while both are equal
+        * for a properly built capture list, only the former is guaranteed to
+        * be a valid slot number for the array allocated above.
+        */
+       memcpy(smp->strm->req_cap[hdr->index], smp->data.u.str.area, len);
+       smp->strm->req_cap[hdr->index][len] = '\0';
 
        return 1;
 }
@@ -447,9 +450,12 @@ static int smp_conv_res_capture(const struct arg *args, struct sample *smp, void
        if (len > hdr->len)
                len = hdr->len;
 
-       /* Capture input data. */
-       memcpy(smp->strm->res_cap[idx], smp->data.u.str.area, len);
-       smp->strm->res_cap[idx][len] = '\0';
+       /* Capture input data. Use hdr->index and not idx: while both are equal
+        * for a properly built capture list, only the former is guaranteed to
+        * be a valid slot number for the array allocated above.
+        */
+       memcpy(smp->strm->res_cap[hdr->index], smp->data.u.str.area, len);
+       smp->strm->res_cap[hdr->index][len] = '\0';
 
        return 1;
 }