Skip to content
Myr

Limit number of function parameters and definitions to prevent SEGV (#3460)

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

authored by Ze Sheng · committed by GitHub · parent b33a763 · Browse files · 2 files+41 added−0 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+41 added−0 removed

  • tests:+27 added−0 removed
  • other:+14 added−0 removed

2 files+41 added−0 removed

modified:src/compile.c+14 added
1239 unmodified lines
1240 }12411242 if (curr->op == CLOSURE_CREATE) {1243 assert(curr->bound_by == curr);1244 // Prevent subfunction index from overflowing into ARG_NEWCLOSURE flag1245 if (bc->nsubfunctions >= ARG_NEWCLOSURE) {1246 locfile_locate(lf, curr->source,1247 "too many function parameters or local function definitions (max %d)", ARG_NEWCLOSURE - 1);1248 errors++;1249 break;1250 }1251 curr->imm.intval = bc->nsubfunctions++;1252 }1253 if (curr->op == CLOSURE_CREATE_C) {1254 assert(curr->bound_by == curr);
25 unmodified lines
1280 jv params = jv_array();1281 for (inst* param = curr->arglist.first; param; param = param->next) {1282 assert(param->op == CLOSURE_PARAM);1283 assert(param->bound_by == param);1284 // Prevent closure index from overflowing into ARG_NEWCLOSURE flag1285 if (subfn->nclosures >= ARG_NEWCLOSURE) {1286 locfile_locate(lf, curr->source,1287 "function has too many parameters (max %d)", ARG_NEWCLOSURE - 1);1288 errors++;1289 break;1290 }1291 param->imm.intval = subfn->nclosures++;1292 param->compiled = subfn;1293 params = jv_array_append(params, jv_string(param->symbol));1294 }
99 unmodified lines
modified:tests/shtest+27 added
842 unmodified lines
843 exit 1844 fi845fi846847# Test ARG_NEWCLOSURE overflow protection (issue #3458)848# Test 1: Too many function parameters (4097 params triggers the limit)849echo "Testing function parameter limit..."850cat > $d/expected <<EOF851jq: error: too many function parameters or local function definitions (max 4095)852jq: 1 compile error853EOF854$JQ -rn '"def f(\([range(4097) | "a\(.)"] | join(";"))): .; f(\([range(4097)] | join(";")))"' > $d/prog.jq855$JQ -nf $d/prog.jq 2> $d/out && {856 echo "Expected compile error for too many parameters"857 exit 1858}859diff $d/out $d/expected860861# Test 2: Too many local functions (4097 functions triggers the limit)862echo "Testing local function limit..."863cat > $d/expected <<EOF864jq: error: too many function parameters or local function definitions (max 4095)865jq: 1 compile error866EOF867$JQ -rn '"\([range(4097) | "def f\(.): \(.)"] | join("; ")); \([range(4097) | "f\(.)"] | join(" + "))"' > $d/prog.jq868$JQ -nf $d/prog.jq 2> $d/out && {869 echo "Expected compile error for too many local functions"870 exit 1871}872diff $d/out $d/expected873874exit 0