]> git.kaiwu.me - haproxy.git/commitdiff
BUG/MINOR: htx: only rollback blocks copied by the current call in htx_xfer()
authorChristopher Faulet <cfaulet@haproxy.com>
Thu, 3 Dec 2026 17:05:26 +0000 (18:05 +0100)
committerChristopher Faulet <cfaulet@haproxy.com>
Fri, 4 Dec 2026 13:54:03 +0000 (14:54 +0100)
When partial copies of headers or trailers are not allowed, htx_xfer()
removes the start-line, header and trailer blocks found at the end of
<dst> when the copy is interrupted, and rolls back on <src> accordingly.
But this was not limited to the blocks copied by the current call. If
<dst> already ended with such blocks, for instance a trailer block added
by the cache applet, these blocks were removed too, and the rollback on
<src> went beyond its first block. In this case, <src> was then fully
drained.

Now, the start-line, header and trailer blocks copied at the end of
<dst> are counted, and only these blocks are removed on rollback.

This patch must be backported to 3.4.

src/htx.c

index bda321397fe92dc1270a7b30442d2042e708043c..6e6e41244034bfc9cbd48b02378f723ad6ff1741 100644 (file)
--- a/src/htx.c
+++ b/src/htx.c
@@ -871,6 +871,7 @@ size_t htx_xfer(struct htx *dst, struct htx *src, size_t count, unsigned int fla
        size_t ret = 0;
        size_t meta_sz = (flags & HTX_XFER_NO_METADATA) ? 0 : sizeof(*blk);
        uint32_t max, last_dstblk_sz;
+       uint32_t nb_hdrs = 0;
        int dst_full = 0;
 
        /* Note: no test on the end of the message here. Blocks are added one by
@@ -914,6 +915,7 @@ size_t htx_xfer(struct htx *dst, struct htx *src, size_t count, unsigned int fla
                        last_dstblk_sz = v.len;
                        count -= meta_sz + v.len;
                        ret += meta_sz + v.len;
+                       nb_hdrs = 0;
                        if (v.len != sz) {
                                dst_full = 1;
                                goto stop;
@@ -938,6 +940,15 @@ size_t htx_xfer(struct htx *dst, struct htx *src, size_t count, unsigned int fla
                        last_dstblk_sz = sz;
                        count -= meta_sz + sz;
                        ret += meta_sz + sz;
+
+                       /* Count the start-line, header and trailer blocks
+                        * copied at the end of <dst> by this call. Only these
+                        * ones may be removed if the copy is interrupted.
+                        */
+                       if (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL || type == HTX_BLK_HDR || type == HTX_BLK_TLR)
+                               nb_hdrs++;
+                       else
+                               nb_hdrs = 0;
                        break;
                }
 
@@ -961,15 +972,22 @@ size_t htx_xfer(struct htx *dst, struct htx *src, size_t count, unsigned int fla
                if (dstblk)
                        type = htx_get_blk_type(dstblk);
 
-               /* the last copied block is a start-line, a header or a trailer */
-               if (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL || type == HTX_BLK_HDR || type == HTX_BLK_TLR) {
+               /* the last copied block is a start-line, a header or a
+                * trailer. Blocks already present in <dst> before this call
+                * must not be considered.
+                */
+               if (nb_hdrs && (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL || type == HTX_BLK_HDR || type == HTX_BLK_TLR)) {
                        /* <src> cannot have partial headers or trailers part */
                        BUG_ON(blk == NULL);
 
-                       /* Remove partial headers/trailers from <dst> and rollback on <src> to not remove them later */
-                       while (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL || type == HTX_BLK_HDR || type == HTX_BLK_TLR) {
+                       /* Remove partial headers/trailers copied by this call
+                        * from <dst> and rollback on <src> to not remove them
+                        * later
+                        */
+                       while (nb_hdrs && (type == HTX_BLK_REQ_SL || type == HTX_BLK_RES_SL || type == HTX_BLK_HDR || type == HTX_BLK_TLR)) {
                                ret -= meta_sz + htx_get_blksz(dstblk);
                                htx_remove_blk(dst, dstblk);
+                               nb_hdrs--;
                                dstblk = htx_get_tail_blk(dst);
                                blk = htx_get_prev_blk(src, blk);
                                if (!dstblk)