/* Changes the size of the value. It is the caller responsibility to change the
* value itself, make sure there is enough space and update allocated
* value. This function updates the HTX message accordingly.
+ *
+ * It must never be used to empty a block. Only headers/trailers can have a
+ * value of size 0. Otherwise, Such block must be removed with htx_remove_blk()
+ * instead.
*/
static inline void htx_change_blk_value_len(struct htx *htx, struct htx_blk *blk, uint32_t newlen)
{
uint32_t oldlen, sz;
int32_t delta;
+ BUG_ON(!newlen && (type != HTX_BLK_HDR && type != HTX_BLK_TLR));
+
sz = htx_get_blksz(blk);
switch (type) {
case HTX_BLK_HDR:
* value itself, make sure there is enough space and update allocated
* value. Unlike the function htx_change_blk_value_len(), this one does not
* update the HTX message. So it should be used with caution.
+ *
+ * It must never be used to empty a block. Only headers/trailers can have a
+ * value of size 0. Otherwise, Such block must be removed with htx_remove_blk()
+ * instead.
*/
static inline void htx_set_blk_value_len(struct htx_blk *blk, uint32_t vlen)
{
enum htx_blk_type type = htx_get_blk_type(blk);
+ BUG_ON(!vlen && (type != HTX_BLK_HDR && type != HTX_BLK_TLR));
+
switch (type) {
case HTX_BLK_HDR:
case HTX_BLK_TLR:
* updated. This is used to mark that part of some data were transferred
* from a DATA block without removing this DATA block. No sanity check is
* performed, the caller is responsible for doing this exclusively on DATA
- * blocks, and never removing more than the block's size.
+ * blocks, and never removing more than the block's size. <n> must also never
+ * be equal to the block's size, a fully consumed block must be removed with
+ * htx_remove_blk() instead.
*/
static inline void htx_cut_data_blk(struct htx *htx, struct htx_blk *blk, uint32_t n)
{
+ BUG_ON(n >= htx_get_blksz(blk));
+
if (blk->addr == htx->end_addr)
htx->end_addr += n;
blk->addr += n;
* block is returned and the HTX message is updated. Space for this new block is
* reserved in the HTX message. But it is the caller responsibility to set right
* info in the block to reflect the stored data.
+ *
+ * <blksz> must never be 0: a block of any type must never be empty. Special
+ * blocks (EOH, EOT, NOOP) are created with a size of 1 for this reason. The
+ * single entry point for block creation, __htx_add_blk(), enforces this
+ * invariant.
*/
static struct htx_blk *htx_reserve_nxblk(struct htx *htx, uint32_t blksz)
{
* ended. It must only be used by the HTX API itself, to temporarily add a block
* after the one carrying the EOM flag, before moving it at the right place. Any
* other caller must use htx_add_blk() instead.
+ *
+ * A block of any type must never be empty: <blksz> must never be 0.
*/
struct htx_blk *__htx_add_blk(struct htx *htx, enum htx_blk_type type, uint32_t blksz)
{
struct htx_blk *blk;
BUG_ON(blksz >= 256 << 20);
+ if (unlikely(!blksz))
+ return NULL;
if (unlikely(type < HTX_BLK_EOH && htx_hdrs_too_big(htx, blksz)))
return NULL;
blk = htx_reserve_nxblk(htx, blksz);
* ended. It must only be used by the HTX API itself, to temporarily add a block
* after the one carrying the EOM flag, before moving it at the right place. Any
* other caller must use htx_add_data_type_atonce() instead.
+ *
+ * Nothing is added when <data> is empty: an empty DATA or RAW_DATA block must
+ * never be created (see __htx_add_blk()). NULL is returned in this case.
*/
struct htx_blk *__htx_add_data_type_atonce(struct htx *htx, struct ist data, enum htx_blk_type type)
{
BUG_ON(!htx_is_data_type(type));
+ if (!data.len)
+ return NULL;
+
if (htx->head == -1)
goto add_new_block;
* larger than the old one. This function works for any kind of block with
* attached data. It returns the new block on success, otherwise it returns
* NULL.
+ *
+ * A block of any type must never be emptied: NULL is returned if the
+ * replacement removes all the data of the block. The caller must remove the
+ * block with htx_remove_blk(), taking care of the EOM flag, if the removed
+ * block was carrying it.
*/
struct htx_blk *htx_replace_blk_value(struct htx *htx, struct htx_blk *blk,
const struct ist old, const struct ist new)
v = htx_get_blk_value(htx, blk);
delta = new.len - old.len;
+ if (unlikely((int32_t)htx_get_blksz(blk) + delta == 0))
+ return NULL;
+
/* The value length of a header or a trailer is encoded on 20 bits. It
* must not overflow on the other fields of the block descriptor.
*/