Skip to content
Myr

Reject raw NUL bytes in the parser instead of silently truncating tokens (#3606)

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

authored by august · committed by GitHub · parent 8cf81cb · Browse files · 2 files+26 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+26 added−0 removed

  • tests:+21 added−0 removed
  • other:+5 added−0 removed

2 files+26 added−0 removed

modified:src/jv_parse.c+5 added
561 unmodified lines
562} chclass;563564static chclass classify(char c) {565 switch (c) {566 case '\0':567 // RFC 8259 forbids raw NUL in JSON text. Treating it as a literal lets it568 // slip into the token buffer, where check_literal() reads the buffer as a569 // NUL-terminated C string and silently truncates the token. Reject it.570 return INVALID;571 case ' ':572 case '\t':573 case '\r':574 case '\n':
350 unmodified lines
modified:tests/shtest+21 added
117 unmodified lines
118printf '" ~\\u007f"\n' > $d/expected119printf "\"$(printf '\\%03o' 32 126 127)\"" | $JQ '.' > $d/out 2>&1120cmp $d/out $d/expected121122# Regression test for #3110: a raw NUL byte outside of a string must be a clean123# parse error, not silently truncate the surrounding token. Previously124# `printf '12\0003'` printed 12 and dropped the trailing 3.125cat > $d/expected <<EOF126jq: parse error: Invalid character at line 1, column 3127EOF128if printf '12\0003' | $JQ '.' > $d/out 2>&1; then129 printf 'Error expected but jq exited successfully\n' 1>&2130 exit 2131fi132cmp $d/out $d/expected133# A NUL at the very start of the input is rejected too.134cat > $d/expected <<EOF135jq: parse error: Invalid character at line 1, column 1136EOF137if printf '\000' | $JQ '.' > $d/out 2>&1; then138 printf 'Error expected but jq exited successfully\n' 1>&2139 exit 2140fi141cmp $d/out $d/expected142143## Test --exit-status144data='{"i": 1}\n{"i": 2}\n{"i": 3}\n'145printf "$data" | $JQ --exit-status 'select(.i==1)' > /dev/null 2>&1146printf "$data" | $JQ --exit-status 'select(.i==2)' > /dev/null 2>&1
813 unmodified lines