Skip to content
Myr

Fix assertion failure on too many parameters in a nested function (#3663)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

A call passing more than 4095 arguments at the start of a nested function body aborted jq after reporting the compile errors, with "Assertion failed: (nemb > 0 && sz > 0), function jv_mem_calloc" and exit status 134. Stop compiling a function before its bytecode is allocated when it has compile errors, since the bytecode is only generated and used when compilation succeeds. jq now prints the same compile errors followed by "jq: 2 compile errors" and exits with status 3, as it does for other compile errors, instead of crashing.

authored by itchyny · committed by GitHub · parent 0404249 · Browse files · 2 files+21 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+21 added−1 removed

  • tests:+14 added−0 removed
  • other:+7 added−1 removed

2 files+21 added−1 removed

modified:src/compile.c+7 added-1 removed
1304 unmodified lines
1305 } else {1306 bc->nsubfunctions = 0;1307 bc->subfunctions = 0;1308 }1309 if (errors) {1310 bc->code = 0;1311 bc->constants = jv_invalid();1312 block_free(b);1313 return errors;1314 }1315 uint16_t* code = jv_mem_calloc(bc->codelen, sizeof(uint16_t));1316 bc->code = code;1317 pos = 0;1318 jv constant_pool = jv_array();1319 int maxvar = -1;1314 if (!errors) for (inst* curr = b.first; curr; curr = curr->next) {1320 for (inst* curr = b.first; curr; curr = curr->next) {1321 const struct opcode_description* op = opcode_describe(curr->op);1322 if (op->length == 0)1323 continue;1324 code[pos++] = curr->op;
80 unmodified lines
modified:tests/shtest+14 added
950 unmodified lines
951 exit 1952}953diff $d/out $d/expected954955# Test 3: Too many function parameters in a nested function (issue #3661)956echo "Testing function parameter limit in a nested function..."957cat > $d/expected <<EOF958jq: error: function has too many parameters (max 4095)959jq: error: too many function parameters or local function definitions (max 4095)960jq: 2 compile errors961EOF962$JQ -rn '"def g(\([range(4097) | "a\(.)"] | join(";"))): .; def f: g(\([range(4097)] | join(";"))); f"' > $d/prog.jq963$JQ -nf $d/prog.jq 2> $d/out && {964 echo "Expected compile error for too many parameters in a nested function"965 exit 1966}967diff $d/out $d/expected968969# CVE-2026-33948: No NUL truncation in the JSON parser970if printf '{}\x00{}' | $JQ >/dev/null 2> /dev/null; then971 printf 'Error expected but jq exited successfully\n' 1>&2972 exit 1
27 unmodified lines