Add jetc --lint-sql to find <sql> that pastes values (phase 2)

Implements phase 2 of docs/design-sql-bound-parameters.md. jetc --lint-sql
[--strict] <file|dir>... lists each <sql> whose statement contains a $[...]
or #[...] reference instead of a <bind>, with file:line and where each value
comes from. Row values ($[.col], $[session.col], $[?N], $[#]) are only
reported with --strict. References in <bind>, <if>, <set> and the other
attribute-only tags, <comment> and |> <| are skipped. A custom tag defined
as <sql><container></sql> (barant.com's <showtable>) is checked at each use.
Exit status is 0 for none found, 1 for findings, 2 for an unreadable path.

- jetc/JetLint.*: the scanner; jetc.cpp: the option
- build scripts and runbindtests.sh: link JetLint.cpp
- tests/runlinttests.sh and tests/lintsql/: fixtures and expected output
- docs: worklist for the local copies of both sites, design doc status,
  manual note on the lint

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
brad ArantandClaude Opus 5.5 committed 2026-11-20 20:29:28 -08:00
1 parent fd81bb877f
commit 2847ce5fa7
22 files changed
+667 -10

No files matched your search

+1
View File
@@ -0,0 +1 @@
lint-sql: 0 of 1 <sql> blocks paste values into the statement, in 0 of 1 files
+1
View File
@@ -0,0 +1 @@
<sql>select * from t where id=?<bind value="$[id]" type="int"></sql>
+2
View File
@@ -0,0 +1,2 @@
jetc: cannot open no-such-dir
lint-sql: 0 of 1 <sql> blocks paste values into the statement, in 0 of 1 files
+6
View File
@@ -0,0 +1,6 @@
site/kinds.page:1: <sql> pastes $[plain] (global), $[:form] (cgi), $[@sess] (session)
site/kinds.page:2: <sql> pastes $[&HTTP_HOST] (env), $[%kw] (keyword), $[!g] (global), #[loc] (local)
site/kinds.page:3: <sql> pastes $[late] (global)
site/kinds.page:6: <sql> pastes $[name$[ix]] (global)
site/uses.page:1: <showtable> (wraps sql) pastes $[_login_id] (global)
lint-sql: 5 of 11 <sql> blocks (and uses of tags that wrap <sql>) paste values into the statement, in 2 of 4 files (cgi 1, env 1, global 5, keyword 1, local 1, session 1)
+1
View File
@@ -0,0 +1 @@
<sql>select $[hidden]</sql>
+6
View File
@@ -0,0 +1,6 @@
<sql>select * from t where a='$[plain]' and b=$[:form] and c='$[@sess]'</sql>
<sql>select '$[&HTTP_HOST]', '$[%kw]', '$[!g]', '#[loc]'</sql>
<SQL sessionid="#[sid]">select 1
from t
where x=$[late] and y=$[late]</SQL>
<sql>select * from a where k=$[name$[ix]]</sql>
+1
View File
@@ -0,0 +1 @@
no queries here $[x]
+11
View File
@@ -0,0 +1,11 @@
<sql>select $[.col], $[projman.col], $[?1], $[#], $[?#] from t</sql>
<sql>select * from t where a=? and b=?
<bind value="$[email]">
<if value1="$[x]" value2="1"><bind value="$[y]" type="int"></if>
<set name="z" value="$[z]">
<comment> uses $[nothing] </comment>
</sql>
<sql>select '|>$[not_parsed]<|'</sql>
<sql sessionid="x"/>
<sqlx>$[not_sql]</sqlx>
$[outside_sql]
+4
View File
@@ -0,0 +1,4 @@
<tag name="showTable" keywords="sessionid">
<sql sessionid="#[sessionid]"><container></sql>
</tag>
<tag name="plain"><b><container></b></tag>
+3
View File
@@ -0,0 +1,3 @@
<showtable sessionid="1">select * from t where u=$[_login_id]</showtable>
<showtable>select * from t where u=$[.id]</showtable>
<plain>$[not_sql]</plain>
+8
View File
@@ -0,0 +1,8 @@
site/kinds.page:1: <sql> pastes $[plain] (global), $[:form] (cgi), $[@sess] (session)
site/kinds.page:2: <sql> pastes $[&HTTP_HOST] (env), $[%kw] (keyword), $[!g] (global), #[loc] (local)
site/kinds.page:3: <sql> pastes $[late] (global)
site/kinds.page:6: <sql> pastes $[name$[ix]] (global)
site/safe.page:1: <sql> pastes $[.col] (row), $[projman.col] (row), $[?1] (row), $[#] (row), $[?#] (row)
site/uses.page:1: <showtable> (wraps sql) pastes $[_login_id] (global)
site/uses.page:2: <showtable> (wraps sql) pastes $[.id] (row)
lint-sql: 7 of 11 <sql> blocks (and uses of tags that wrap <sql>) paste values into the statement, in 3 of 4 files (cgi 1, env 1, global 5, keyword 1, local 1, row 6, session 1)
+1 -1
View File
@@ -36,7 +36,7 @@ else
fi
echo "== jetc =="
g++ -g -std=c++23 -o "$WORK/jetc" "$JC/jetc/jetc.cpp" "$JC/jetc/JetParser.cpp" "$JC/jetc/JetCodegen.cpp" || exit 1
g++ -g -std=c++23 -o "$WORK/jetc" "$JC/jetc/jetc.cpp" "$JC/jetc/JetParser.cpp" "$JC/jetc/JetCodegen.cpp" "$JC/jetc/JetLint.cpp" || exit 1
# A bare <bind> must not swallow the content after it looking for </bind>.
printf 'A<sql>select ?<bind value="$[x]"></sql>B<bind value="y">C\n' > "$WORK/void.jet"
"$WORK/jetc" "$WORK/void.jet" "$WORK/void.cpp" >/dev/null || fail=1
+34
View File
@@ -0,0 +1,34 @@
#!/bin/bash
# Tests for jetc --lint-sql (docs/design-sql-bound-parameters.md, phase 2).
# Runs the lint over tests/lintsql/ and compares with the .expected files.
# Needs no database.
set -u
T="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
JC="$(cd "$T/.." && pwd)"
WORK="$(mktemp -d)"
trap 'rm -rf "$WORK"' EXIT
fail=0
g++ -g -std=c++23 -o "$WORK/jetc" "$JC/jetc/jetc.cpp" "$JC/jetc/JetParser.cpp" "$JC/jetc/JetCodegen.cpp" "$JC/jetc/JetLint.cpp" || exit 1
# check NAME EXPECTED_EXIT ARGS... - output is compared with lintsql/NAME.expected
check() {
local name="$1" want="$2"; shift 2
(cd "$T/lintsql" && "$WORK/jetc" --lint-sql "$@") > "$WORK/$name.out" 2>&1
local got=$?
if [ "$got" != "$want" ]; then
echo "FAIL $name: exit $got, expected $want"; fail=1
elif ! diff -u "$T/lintsql/$name.expected" "$WORK/$name.out"; then
echo "FAIL $name: output differs"; fail=1
else
echo "PASS $name"
fi
}
check site 1 site
check strict 1 --strict site
check clean 0 clean
check missing 2 clean no-such-dir
[ $fail = 0 ] && echo "ALL PASSED" || echo "FAILURES"
exit $fail