]> git.kaiwu.me - haproxy.git/commitdiff
CLEANUP/MINOR: ncbuf: avoid noop memcpy when data ends on wrapping
authorAmaury Denoyelle <adenoyelle@haproxy.com>
Thu, 19 Nov 2026 09:49:41 +0000 (10:49 +0100)
committerAmaury Denoyelle <adenoyelle@haproxy.com>
Thu, 19 Nov 2026 14:08:35 +0000 (15:08 +0100)
The ncbuf add operation implements support for wrapping in case the
newly inserted data exceeds the buffer end.

This operation is inefficient due to an excessive comparison, in case
the data just ends at the buffer boundary. Two memcpy are used, the
second one with a 0 length argument which is a noop.

Change the comparison to greater-than to select the single memcpy path.
The main objective of this patch is to clean up wrapping comparison and
avoid future incorrect code duplication.

src/ncbuf.c

index e1452f1d27c819e3652e8c7818d10b66f3e3de66..430d8fcedb27e585354a6225a280fea27cf416d5 100644 (file)
@@ -313,7 +313,7 @@ static ncb_sz_t ncb_fill_data_blk(const struct ncbuf *buf,
        if (mode == NCB_ADD_OVERWRT) {
                ptr = ncb_peek(buf, blk->off + off);
 
-               if (ptr + to_copy >= ncb_wrap(buf)) {
+               if (ptr + to_copy > ncb_wrap(buf)) {
                        const ncb_sz_t sz1 = ncb_wrap(buf) - ptr;
                        memcpy(ptr, data, sz1);
                        memcpy(ncb_orig(buf), data + sz1, to_copy - sz1);
@@ -363,7 +363,7 @@ static ncb_sz_t ncb_fill_gap_blk(const struct ncbuf *buf,
 
        /* fill the gap with new data */
        ptr = ncb_peek(buf, blk->off + off);
-       if (ptr + to_copy >= ncb_wrap(buf)) {
+       if (ptr + to_copy > ncb_wrap(buf)) {
                ncb_sz_t sz1 = ncb_wrap(buf) - ptr;
                memcpy(ptr, data, sz1);
                memcpy(ncb_orig(buf), data + sz1, to_copy - sz1);