]> git.kaiwu.me - njs.git/commit
Add optional per-chain byte cap in chained buffers
authorDmitry Volyntsev <xeioex@nginx.com>
Tue, 21 Apr 2026 01:51:30 +0000 (18:51 -0700)
committerDmitry Volyntsev <xeioexception@gmail.com>
Wed, 10 Jun 2026 20:38:58 +0000 (13:38 -0700)
commit5f5e990012758e72df7414d7d26b0982b8209964
tree12ea5bc5fa8281baf948646c9f470d7b42280b1e
parentd9c97cf9a13e7a26206345b774fcfe609fbe4ef3
Add optional per-chain byte cap in chained buffers

Previously, njs_chb_t accumulated bytes unbounded through the memory
pool until the final string boundary (njs_string_create_chb()) enforced
the NJS_STRING_MAX_LENGTH limit.  For string-producing chains this let
the pool grow to tens of GiB before the final alloc was refused, which
on adversarial inputs (such as String.prototype.replace with a regex
that matches the full string and a replacement containing thousands of
$N references) led to process OOM instead of a catchable RangeError.

The fix is to add two fields to njs_chb_t: total_size (O(1) running byte
count) and max_size (optional upper bound, 0 means unlimited).
Exceeding max_size puts the chain into a sticky NJS_CHB_ERR_OVERFLOW
state.  As part of the change njs_chb_drain() now returns early on a
failed chain, in line with njs_chb_drop().
src/njs_chb.c
src/njs_chb.h
src/njs_string.c
src/qjs.c
src/test/njs_unit_test.c