-- Regression tests for bugs found by the 2026-11-19 audit (fix_brace). -- Each case names its audit finding (A). -- A13: a for-of / foreach over query() with no arguments is rejected, not a crash DO LANGUAGE plxjs $$ for (const r of query()) { } $$; ERROR: plxjs: query requires a SQL argument DETAIL: at plxjs source line 1 DO LANGUAGE plxts $$ for (const r of query()) { } $$; ERROR: plxts: query requires a SQL argument DETAIL: at plxts source line 1 DO LANGUAGE plxphp $$ foreach (query() as $r) { } $$; ERROR: plxphp: query requires a SQL argument DETAIL: at plxphp source line 1 SELECT 'A13 backend still alive' AS ok; ok ------------------------- A13 backend still alive (1 row) -- N-pyquery: a Python for over query() with no arguments is rejected, not a crash DO LANGUAGE plxpython3 $$ for r in query(): pass $$; ERROR: plxpython3: query requires a SQL argument DETAIL: at plxpython3 source line 2 SELECT 'N-pyquery backend still alive' AS ok; ok ------------------------------- N-pyquery backend still alive (1 row) -- N-tscatch: a TypeScript catch with a type annotation binds the variable, not the type CREATE FUNCTION fb_tscatch(d int) RETURNS text LANGUAGE plxts AS $$ let r: number = 0; try { r = 10 / d; return "ok"; } catch (e: unknown) { return `caught ${e.sqlstate}`; } $$; SELECT fb_tscatch(0); fb_tscatch -------------- caught 22012 (1 row) -- A14: a catch class maps to its own condition; an unknown class is rejected \set VERBOSITY terse CREATE FUNCTION fb_ct1(d int) RETURNS text LANGUAGE plxphp AS $$ $r = 0; try { $r = 10 / $d; return "ok $r"; } catch (PG::SerializationFailure $e) { return "retry"; } $$; SELECT fb_ct1(0); ERROR: division by zero CREATE FUNCTION fb_ct2(d int) RETURNS text LANGUAGE plxphp AS $$ $r = 0; try { $r = 10 / $d; return "ok $r"; } catch (PG::DivisionByZero $e) { return "div " . $e->sqlstate; } $$; SELECT fb_ct2(0); fb_ct2 ----------- div 22012 (1 row) CREATE FUNCTION fb_ct3(d int) RETURNS text LANGUAGE plxphp AS $$ $r = 0; try { $r = 10 / $d; return "ok $r"; } catch (PG::UniqueViolation | DivisionByZeroError $e) { return "union"; } $$; SELECT fb_ct3(0); fb_ct3 -------- union (1 row) CREATE FUNCTION fb_ct4(d int) RETURNS text LANGUAGE plxphp AS $$ $r = 0; try { $r = 10 / $d; return "ok $r"; } catch (TypeError $e) { return "type"; } $$; ERROR: plxphp: unsupported exception class "TypeError" CREATE FUNCTION fb_ct5(d int) RETURNS text LANGUAGE plxphp AS $$ $r = 0; try { $r = 10 / $d; return "ok $r"; } catch (PG::NoSuchCondition $e) { return "none"; } $$; ERROR: unrecognized exception condition "no_such_condition" SELECT fb_ct5(0); ERROR: function fb_ct5(integer) does not exist at character 8 CREATE FUNCTION fb_ct6(d int) RETURNS text LANGUAGE plxphp AS $$ $r = 0; try { $r = 10 / $d; return "ok $r"; } catch (\Throwable $e) { return "any"; } $$; SELECT fb_ct6(0); fb_ct6 -------- any (1 row) \set VERBOSITY default -- A15: a break nested inside a switch arm leaves the switch, not the enclosing loop CREATE FUNCTION fb_sw1(n int) RETURNS int LANGUAGE plxjs AS $$ let total = 0; for (let i = 0; i < n; i++) { switch (i) { case 1: if (n > 2) { break; } total = total + 100; break; default: total = total + 1; break; } } return total; $$; SELECT fb_sw1(5); fb_sw1 -------- 4 (1 row) CREATE FUNCTION fb_sw2(n int) RETURNS int LANGUAGE plxjs AS $$ let r = 0; switch (n) { case 1: if (n > 0) { break; } r = 5; break; default: r = 9; break; } return r; $$; SELECT fb_sw2(1), fb_sw2(2); fb_sw2 | fb_sw2 --------+-------- 0 | 9 (1 row) CREATE FUNCTION fb_sw3(n int) RETURNS int LANGUAGE plxjs AS $$ let r = 0; switch (n) { case 1: try { if (n > 0) { break; } r = 5; } finally { r = r + 10; } r = r + 100; break; default: r = 9; break; } return r; $$; SELECT fb_sw3(1); fb_sw3 -------- 10 (1 row) CREATE FUNCTION fb_sw4(n int) RETURNS int LANGUAGE plxjs AS $$ let c = 0; switch (n) { case 1: for (let i = 0; i < 5; i++) { if (i == 2) { break; } c = c + 1; } c = c + 100; break; default: c = -1; break; } return c; $$; SELECT fb_sw4(1); fb_sw4 -------- 102 (1 row) CREATE FUNCTION fb_sw5(n int) RETURNS int LANGUAGE plxphp AS $$ $t = 0; for ($i = 0; $i < $n; $i++) { switch ($i) { case 1: if ($n > 2) { break; } $t = $t + 100; break; default: $t = $t + 1; break; } } return $t; $$; SELECT fb_sw5(5); fb_sw5 -------- 4 (1 row) -- N-swlabel: a switch arm may end with a labelled break or with continue CREATE FUNCTION fb_sw6(n int) RETURNS int LANGUAGE plxjs AS $$ let c = 0; outer: for (let i = 0; i < n; i++) { switch (i) { case 2: break outer; default: c = c + 1; break; } } return c; $$; SELECT fb_sw6(5); fb_sw6 -------- 2 (1 row) CREATE FUNCTION fb_sw7(n int) RETURNS int LANGUAGE plxjs AS $$ let c = 0; for (let i = 0; i < n; i++) { switch (i) { case 2: continue; default: c = c + 1; break; } c = c + 10; } return c; $$; SELECT fb_sw7(5); fb_sw7 -------- 44 (1 row) -- A16: PHP break N and continue N leave or continue the N-th enclosing loop or switch CREATE FUNCTION fb_bn1(n int) RETURNS int LANGUAGE plxphp AS $$ $c = 0; for ($i = 0; $i < $n; $i++) { for ($j = 0; $j < $n; $j++) { $c = $c + 1; if ($j == 1) { break 2; } } } return $c; $$; SELECT fb_bn1(5); fb_bn1 -------- 2 (1 row) CREATE FUNCTION fb_bn2(n int) RETURNS int LANGUAGE plxphp AS $$ $c = 0; for ($i = 0; $i < $n; $i++) { switch ($i % 2) { case 0: continue 2; default: $c = $c + 1; break; } $c = $c + 10; } return $c; $$; SELECT fb_bn2(4); fb_bn2 -------- 22 (1 row) CREATE FUNCTION fb_bn3(n int) RETURNS int LANGUAGE plxphp AS $$ $c = 0; for ($i = 0; $i < $n; $i++) { switch ($i) { case 3: break 2; default: $c = $c + 1; break; } } return $c; $$; SELECT fb_bn3(10); fb_bn3 -------- 3 (1 row) CREATE FUNCTION fb_bn4(n int) RETURNS int LANGUAGE plxphp AS $$ $c = 0; for ($i = 0; $i < $n; $i++) { for ($j = 0; $j < $n; $j++) { try { if ($j == 1) { break 2; } $c = $c + 1; } finally { $c = $c + 100; } } } return $c; $$; SELECT fb_bn4(5); fb_bn4 -------- 201 (1 row) CREATE FUNCTION fb_bn5(n int) RETURNS int LANGUAGE plxphp AS $$ $c = 0; for ($i = 0; $i < $n; $i++) { for ($j = 0; $j < $n; $j++) { if ($j == 2) { continue 2; } $c = $c + 1; } } return $c; $$; SELECT fb_bn5(4); fb_bn5 -------- 8 (1 row) \set VERBOSITY terse CREATE FUNCTION fb_bn6(n int) RETURNS int LANGUAGE plxphp AS $$ for ($i = 0; $i < $n; $i++) { for ($j = 0; $j < $n; $j++) { break 3; } } return 0; $$; ERROR: plxphp: break 3 needs 3 enclosing loops or switches, but there are 2 CREATE FUNCTION fb_bn7(n int) RETURNS int LANGUAGE plxphp AS $$ for ($i = 0; $i < $n; $i++) { break 0; } return 0; $$; ERROR: plxphp: break 0 is not allowed; the level must be at least 1 CREATE FUNCTION fb_bn8(n int) RETURNS int LANGUAGE plxphp AS $$ switch ($n) { case 1: switch ($n) { case 1: continue 2; default: break; } break; default: break; } return 0; $$; ERROR: plxphp: continue 2 targets a switch; write break 2 CREATE FUNCTION fb_bn9(n int) RETURNS int LANGUAGE plxjs AS $$ for (let i = 0; i < n; i++) { for (let j = 0; j < n; j++) { break 2; } } return 0; $$; ERROR: plxjs: break takes a loop label, not a number \set VERBOSITY default -- A17: a for that is not a plain counting loop is rejected, not lowered to a wrong FOR \set VERBOSITY terse CREATE FUNCTION fb_f1(n int) RETURNS int LANGUAGE plxjs AS $$ let c = 0; for (let i = 0; c < n; i++) { c = c + 2; } return c; $$; ERROR: plxjs: for-loop condition must test the loop variable "i"; use a while loop for other conditions CREATE FUNCTION fb_f2(n int) RETURNS int LANGUAGE plxjs AS $$ let c = 0; let lim = n; for (let i = 0; i < lim; i++) { lim = 2; c = c + 1; } return c; $$; ERROR: plxjs: the for-loop body assigns "lim", which the loop bound or step uses; a counting for evaluates them once, so use a while loop CREATE FUNCTION fb_f3() RETURNS int LANGUAGE plxjs AS $$ let i = 0; let c = 0; for (i = 0; i < 10; i++) { i = i + 1; c = c + 1; } return c; $$; ERROR: plxjs: the for-loop body assigns the loop variable "i"; use a while loop CREATE FUNCTION fb_f4(n int) RETURNS int LANGUAGE plxjs AS $$ let i = 0; for (i = 0; i < n; i++) { } return i; $$; ERROR: plxjs: the loop variable "i" is read after the for loop, but a counting for does not keep its final value; use a while loop CREATE FUNCTION fb_f5(n int) RETURNS int LANGUAGE plxjs AS $$ let c = 0; for (let i = 0; i < n && c < 2; i++) { c = c + 1; } return c; $$; ERROR: plxjs: for-loop bound must be a single value, not a condition; use a while loop CREATE FUNCTION fb_f6(n int) RETURNS int LANGUAGE plxjs AS $$ let c = 0; let j = 0; for (let i = 0; i < n; i++, j++) { c = c + 1; } return c; $$; ERROR: plxjs: for-loop step must be 'v++' or 'v += K' CREATE FUNCTION fb_f7(n int) RETURNS int LANGUAGE plxphp AS $$ for ($i = 0; $i < $n; $i++) { } return $i; $$; ERROR: plxphp: the loop variable "i" is read after the for loop, but a counting for does not keep its final value; use a while loop CREATE FUNCTION fb_f8(n int) RETURNS int LANGUAGE plxjs AS $$ let c = 0; for (let i = 0; i < n; j++) { c = c + 1; } return c; $$; ERROR: plxjs: for-loop step must change the loop variable "i" \set VERBOSITY default -- a counting loop, reused after the loop only by another for, still compiles CREATE FUNCTION fb_f9(n int) RETURNS int LANGUAGE plxphp AS $$ $t = 0; for ($i = 0; $i < $n; $i++) { $t = $t + $i; } for ($i = 0; $i < $n; $i += 2) { $t = $t + 1; } return $t; $$; SELECT fb_f9(4); fb_f9 ------- 8 (1 row) -- a loop variable assigned again after the loop is not read after it CREATE FUNCTION fb_f10(n int) RETURNS int LANGUAGE plxphp AS $$ $t = 0; for ($i = 1; $i <= $n; $i++) { for ($j = 1; $j <= $i; $j++) { $t = $t + 1; } } $i = 100; return $t + $i; $$; SELECT fb_f10(3); fb_f10 -------- 106 (1 row) -- A19: throw of anything but a constructor call is rejected, not cut to its first argument \set VERBOSITY terse DO LANGUAGE plxjs $$ throw upper("abc") + lower("XYZ"); $$; ERROR: plxjs: throw requires 'new Exception(...)' CREATE FUNCTION fb_th1(x int) RETURNS int LANGUAGE plxjs AS $$ throw `code ` + abs(x); $$; ERROR: plxjs: throw requires 'new Exception(...)' DO LANGUAGE plxjs $$ throw new Error("x").foo(1); $$; ERROR: plxjs: throw requires 'new Exception(...)' DO LANGUAGE plxphp $$ throw new \RuntimeException("php " . upper("ns")); $$; ERROR: php NS DO LANGUAGE plxjs $$ throw new Error(`code ${abs(-7)}`); $$; ERROR: code 7 DO LANGUAGE plxjs $$ throw Error("plain"); $$; ERROR: plain \set VERBOSITY default -- A16: break 2 out of a switch inside two try/finally blocks runs both finally bodies CREATE FUNCTION fb_rv1(n int) RETURNS int LANGUAGE plxphp AS $$ $c = 0; $fb = 0; $fc = 0; for ($i = 0; $i < $n; $i++) { try { try { switch ($i) { case 1: break 2; default: $c = $c + 1; break; } } finally { $fb = $fb + 1; } } finally { $fc = $fc + 1; } } return $c * 100 + $fb * 10 + $fc; $$; SELECT fb_rv1(3); fb_rv1 -------- 122 (1 row) CREATE FUNCTION fb_rv2(n int) RETURNS int LANGUAGE plxphp AS $$ $c = 0; $f = 0; $g = 0; for ($i = 0; $i < $n; $i++) { try { try { lbl: for ($j = 0; $j < $n; $j++) { if ($j == 1) { break 2; } $c = $c + 1; } } finally { $f = $f + 1; } } finally { $g = $g + 1; } } return $c * 100 + $f * 10 + $g; $$; SELECT fb_rv2(3); fb_rv2 -------- 111 (1 row) -- A16: PHP continue or continue 1 whose target is a switch is rejected, since PHP runs it as break \set VERBOSITY terse CREATE FUNCTION fb_rv3(n int) RETURNS int LANGUAGE plxphp AS $$ $c = 0; for ($i = 0; $i < $n; $i++) { switch ($i) { case 1: continue; default: break; } $c = $c + 1; } return $c; $$; ERROR: plxphp: continue inside a switch runs as break in PHP; write break, or continue 2 to continue the loop CREATE FUNCTION fb_rv4(n int) RETURNS int LANGUAGE plxphp AS $$ $c = 0; for ($i = 0; $i < $n; $i++) { switch ($i) { case 1: continue 1; default: break; } $c = $c + 1; } return $c; $$; ERROR: plxphp: continue inside a switch runs as break in PHP; write break, or continue 2 to continue the loop CREATE FUNCTION fb_rv5(n int) RETURNS int LANGUAGE plxphp AS $$ $c = 0; for ($i = 0; $i < $n; $i++) { switch ($i) { case 1: if ($n > 2) { continue; } $c = $c + 100; break; default: break; } $c = $c + 1; } return $c; $$; ERROR: plxphp: continue inside a switch runs as break in PHP; write break, or continue 2 to continue the loop \set VERBOSITY default -- A17: a for whose variable an enclosing loop reads before the for is rejected \set VERBOSITY terse CREATE FUNCTION fb_rv6() RETURNS int LANGUAGE plxjs AS $$ let i = 0; let x = 0; let k = 0; while (k < 2) { x = x + i; for (i = 0; i < 3; i++) { } k = k + 1; } return x; $$; ERROR: plxjs: the loop variable "i" is read again by an enclosing loop, but a counting for does not keep its final value; use a while loop CREATE FUNCTION fb_rv7() RETURNS int LANGUAGE plxphp AS $$ $i = 0; $x = 0; $k = 0; while ($k < 2) { $x = $x + $i; for ($i = 0; $i < 3; $i++) { } $k = $k + 1; } return $x; $$; ERROR: plxphp: the loop variable "i" is read again by an enclosing loop, but a counting for does not keep its final value; use a while loop CREATE FUNCTION fb_rv8() RETURNS int LANGUAGE plxjs AS $$ let i = 0; let k = 0; while (i < 3 && k < 5) { for (i = 0; i < 3; i++) { } k = k + 1; } return k; $$; ERROR: plxjs: the loop variable "i" is read again by an enclosing loop, but a counting for does not keep its final value; use a while loop \set VERBOSITY default -- an enclosing loop that reads the variable only inside another counting for still compiles CREATE FUNCTION fb_rv9() RETURNS int LANGUAGE plxjs AS $$ let x = 0; let k = 0; let i = 0; while (k < 2) { if (k > 0) { for (i = 0; i < 3; i++) { x = x + i; } } for (i = 0; i < 2; i++) { x = x + 10; } k = k + 1; } return x; $$; SELECT fb_rv9(); fb_rv9 -------- 43 (1 row) -- A17: for (var i ...) keeps i after the loop in JavaScript, so a read after the loop is rejected \set VERBOSITY terse CREATE FUNCTION fb_rv10(n int) RETURNS int LANGUAGE plxjs AS $$ var i = 7; for (var i = 0; i < n; i++) { } return i; $$; ERROR: plxjs: the loop variable "i" is read after the for loop, but a counting for does not keep its final value; use a while loop CREATE FUNCTION fb_rv11(n int) RETURNS int LANGUAGE plxts AS $$ for (var i = 0; i < n; i++) { } return i; $$; ERROR: plxts: the loop variable "i" is read after the for loop, but a counting for does not keep its final value; use a while loop \set VERBOSITY default -- a for (var i ...) whose variable is not read after the loop still compiles CREATE FUNCTION fb_rv12(n int) RETURNS int LANGUAGE plxjs AS $$ let c = 0; for (var i = 0; i < n; i++) { c = c + i; } return c; $$; SELECT fb_rv12(4); fb_rv12 --------- 6 (1 row) -- A14: a PHP catch with no variable maps its class, not every error \set VERBOSITY terse CREATE FUNCTION fb_rv13(d int) RETURNS text LANGUAGE plxphp AS $$ try { $r = 0; $r = 10 / $d; return "ok"; } catch (TypeError) { return "type"; } $$; ERROR: plxphp: unsupported exception class "TypeError" CREATE FUNCTION fb_rv14(d int) RETURNS text LANGUAGE plxphp AS $$ try { $r = 0; $r = 10 / $d; return "ok"; } catch (PG::SerializationFailure) { return "retry"; } $$; SELECT fb_rv14(0); ERROR: division by zero \set VERBOSITY default CREATE FUNCTION fb_rv15(d int) RETURNS text LANGUAGE plxphp AS $$ try { $r = 0; $r = 10 / $d; return "ok"; } catch (PG::UniqueViolation | DivisionByZeroError) { return "union"; } $$; SELECT fb_rv15(0); fb_rv15 --------- union (1 row) -- A19: throw new Error(...) with no semicolon, last in its block, is accepted \set VERBOSITY terse CREATE FUNCTION fb_rv16(x int) RETURNS int LANGUAGE plxts AS $$ if (x < 0) { throw new Error("neg") } return x $$; SELECT fb_rv16(-1); ERROR: neg CREATE FUNCTION fb_rv17(x int) RETURNS int LANGUAGE plxjs AS $$ if (x < 0) { throw new Error(`neg ${x}`) } return x $$; SELECT fb_rv17(-2); ERROR: neg -2 \set VERBOSITY default -- A14: a union of classes that does not match the error lets it through \set VERBOSITY terse CREATE FUNCTION fb_rv18(d int) RETURNS text LANGUAGE plxphp AS $$ $r = 0; try { $r = 10 / $d; return "ok $r"; } catch (PG::UniqueViolation | PG::SerializationFailure $e) { return "union"; } $$; SELECT fb_rv18(0); ERROR: division by zero \set VERBOSITY default -- A14: a comment inside a catch class name is not part of the class CREATE FUNCTION fb_rv19(d int) RETURNS text LANGUAGE plxphp AS $$ $r = 0; try { $r = 10 / $d; return "ok $r"; } catch (PG:: /* SQLSTATE 22012 */ DivisionByZero $e) { return "div"; } $$; SELECT fb_rv19(0); fb_rv19 --------- div (1 row) -- A14: a union with a catch-all member catches every error CREATE FUNCTION fb_rv20(d int) RETURNS text LANGUAGE plxphp AS $$ $r = 0; try { $r = 10 / $d; return "ok $r"; } catch (PG::UniqueViolation | Exception $e) { return "any"; } $$; SELECT fb_rv20(0); fb_rv20 --------- any (1 row) SELECT prosrc LIKE '%WHEN OTHERS THEN%' AS plain_others FROM pg_proc WHERE proname = 'fb_rv20'; plain_others -------------- t (1 row) -- A15: only a switch with a nested break gets a plpgsql label and block SELECT proname, prosrc LIKE '%<<__plx_sw_%' AS labelled FROM pg_proc WHERE proname IN ('fb_sw1', 'fb_sw4') ORDER BY proname; proname | labelled ---------+---------- fb_sw1 | t fb_sw4 | f (2 rows)