]> git.kaiwu.me - haproxy.git/commitdiff
MINOR: htx: Add macros for the header/trailer name and value max lengths
authorChristopher Faulet <cfaulet@haproxy.com>
Thu, 17 Sep 2026 06:44:23 +0000 (08:44 +0200)
committerChristopher Faulet <cfaulet@haproxy.com>
Fri, 18 Sep 2026 15:05:18 +0000 (17:05 +0200)
The maximum length of the name and the value of a header or a trailer were
hardcoded in the different places where they are tested. Let's define
HTX_HDR_NAME_MAX_LEN and HTX_HDR_VALUE_MAX_LEN to avoid these magic values
and document why these limits exist.

include/haproxy/htx-t.h
include/haproxy/htx.h
src/htx.c

index 2e2b1b58d87f9e5312c0c8118f58946b8a2667bc..42b10b0f5246a894caf7c1e54757150502b7d1fb 100644 (file)
@@ -220,6 +220,15 @@ struct htx_blk {
        uint32_t info; /* information about the block (type, length) */
 };
 
+/* Maximum length of the name and the value of a header or a trailer. For these
+ * blocks, the <info> field above encodes the name length on 8 bits and the
+ * value length on 20 bits. Above these limits, the lengths would silently
+ * overflow on the other fields, corrupting the block. So these limits must
+ * always be checked when a header or a trailer is added or updated.
+ */
+#define HTX_HDR_NAME_MAX_LEN   255
+#define HTX_HDR_VALUE_MAX_LEN  1048575
+
 /* Composite return value used by some HTX functions */
 struct htx_ret {
        int32_t ret;         /* A numerical value */
index 55b41838f5034c27468d6f696a785a9b7d752bd7..1fe851cf7a5816a81fad4f08c254bd68f790f0e9 100644 (file)
@@ -360,7 +360,7 @@ static inline void htx_change_blk_value_len(struct htx *htx, struct htx_blk *blk
        switch (type) {
                case HTX_BLK_HDR:
                case HTX_BLK_TLR:
-                       BUG_ON(newlen > 1048575);
+                       BUG_ON(newlen > HTX_HDR_VALUE_MAX_LEN);
                        oldlen = (blk->info >> 8) & 0xfffff;
                        blk->info = (type << 28) + (newlen << 8) + (blk->info & 0xff);
                        break;
@@ -393,7 +393,7 @@ static inline void htx_set_blk_value_len(struct htx_blk *blk, uint32_t vlen)
        switch (type) {
                case HTX_BLK_HDR:
                case HTX_BLK_TLR:
-                       BUG_ON(vlen > 1048575);
+                       BUG_ON(vlen > HTX_HDR_VALUE_MAX_LEN);
                        blk->info = (type << 28) + (vlen << 8) + (blk->info & 0xff);
                        break;
                case HTX_BLK_REQ_SL:
@@ -505,7 +505,7 @@ static inline struct htx_blk *htx_add_header(struct htx *htx, const struct ist n
 {
        struct htx_blk *blk, *tailblk;
 
-       if (name.len > 255 || value.len > 1048575)
+       if (name.len > HTX_HDR_NAME_MAX_LEN || value.len > HTX_HDR_VALUE_MAX_LEN)
                return NULL;
 
        tailblk = htx_get_tail_blk(htx);
@@ -529,7 +529,7 @@ static inline struct htx_blk *htx_add_trailer(struct htx *htx, const struct ist
 {
        struct htx_blk *blk, *tailblk;
 
-       if (name.len > 255 || value.len > 1048575)
+       if (name.len > HTX_HDR_NAME_MAX_LEN || value.len > HTX_HDR_VALUE_MAX_LEN)
                return NULL;
 
        tailblk = htx_get_tail_blk(htx);
index 100abab325c099eadf8e729b596560ed14b6627e..bac133a7c5eff3b2d1a90c5661ccb32ae5e9b31a 100644 (file)
--- a/src/htx.c
+++ b/src/htx.c
@@ -659,7 +659,7 @@ struct htx_blk *htx_replace_blk_value(struct htx *htx, struct htx_blk *blk,
         * must not overflow on the other fields of the block descriptor.
         */
        if (unlikely((type == HTX_BLK_HDR || type == HTX_BLK_TLR) &&
-                    v.len + delta > 1048575))
+                    v.len + delta > HTX_HDR_VALUE_MAX_LEN))
                return NULL;
 
        if (unlikely(type < HTX_BLK_EOH && htx_hdrs_too_big(htx, delta)))
@@ -929,7 +929,7 @@ struct htx_blk *htx_replace_header(struct htx *htx, struct htx_blk *blk,
        if (type != HTX_BLK_HDR)
                return NULL;
 
-       if (name.len > 255 || value.len > 1048575)
+       if (name.len > HTX_HDR_NAME_MAX_LEN || value.len > HTX_HDR_VALUE_MAX_LEN)
                return NULL;
 
        delta = name.len + value.len - htx_get_blksz(blk);