From: Christopher Faulet Date: Thu, 17 Sep 2026 06:44:23 +0000 (+0200) Subject: MINOR: htx: Add macros for the header/trailer name and value max lengths X-Git-Tag: v3.5-dev8~176 X-Git-Url: https://git.kaiwu.me/?a=commitdiff_plain;h=dee32a05d7ed2391801ea40a631b9dd468e8312b;p=haproxy.git MINOR: htx: Add macros for the header/trailer name and value max lengths 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. --- diff --git a/include/haproxy/htx-t.h b/include/haproxy/htx-t.h index 2e2b1b58d..42b10b0f5 100644 --- a/include/haproxy/htx-t.h +++ b/include/haproxy/htx-t.h @@ -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 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 */ diff --git a/include/haproxy/htx.h b/include/haproxy/htx.h index 55b41838f..1fe851cf7 100644 --- a/include/haproxy/htx.h +++ b/include/haproxy/htx.h @@ -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); diff --git a/src/htx.c b/src/htx.c index 100abab32..bac133a7c 100644 --- 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);