]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
BUG/MINOR: dns: fix ring offset calculation on first read
authorAurelien DARRAGON <adarragon@haproxy.com>
Tue, 7 Mar 2023 16:45:02 +0000 (17:45 +0100)
committerAmaury Denoyelle <adenoyelle@haproxy.com>
Wed, 8 Mar 2023 07:56:30 +0000 (08:56 +0100)
With 737d10f ("BUG/MEDIUM: dns: ensure ring offset is properly reajusted
to head") ring offset is now properly re-adjusted in dns_session_io_handler()
and dns_process_req().

But the previous patch does not cope well if the first read is performed
on a non-empty ring since relative ofs will be computed from ds->ofs=0 or
dss->ofs_req=0.
In this case: relative offset could become invalid since we mix up relative
offsets with absolute offsets.

To fix this, we apply the same logic performed in d9c7188 ("MEDIUM: ring:
make the offset relative to the head/tail instead of absolute") for the
cli_io_handler_show_ring() function: that is using b_peek_ofs(buf, 0) to
set the contextual offset instead of hard-coding it to 0.

This should be considered as a minor bugfix since this bug was discovered by
reading the code: 737d10f already survived a good amount of stress-tests as
shown in GH #2068.

No backport needed as 737d10f is not marked for backports.

src/dns.c

index 1e6f287bb52e744153faa17cec28c9508ef5e93d..5d7e1d940103d99b3a6b4805659558bb98aba221 100644 (file)
--- a/src/dns.c
+++ b/src/dns.c
@@ -489,9 +489,8 @@ static void dns_session_io_handler(struct appctx *appctx)
         * value cannot be produced after initialization.
         */
        if (unlikely(ds->ofs == ~0)) {
-               ds->ofs = 0;
-
-               HA_ATOMIC_INC(b_peek(buf, ds->ofs));
+               ds->ofs = b_peek_ofs(buf, 0);
+               HA_ATOMIC_INC(b_orig(buf) + ds->ofs);
        }
 
        /* in this loop, ofs always points to the counter byte that precedes
@@ -1120,8 +1119,8 @@ static struct task *dns_process_req(struct task *t, void *context, unsigned int
         * value cannot be produced after initialization.
         */
        if (unlikely(dss->ofs_req == ~0)) {
-               dss->ofs_req = 0;
-               HA_ATOMIC_INC(b_peek(buf, dss->ofs_req));
+               dss->ofs_req = b_peek_ofs(buf, 0);
+               HA_ATOMIC_INC(b_orig(buf) + dss->ofs_req);
        }
 
        /* we were already there, adjust the offset to be relative to