Skip to content

Commit 70e72ab

Browse files
authored
Fix compiler warning type-limits in found_string (ref #2911) (#3263)
Whether `char` has a sign is implementation dependent, and `c >= 0` may result in a compiler warning. We can use bitwise operator to check whether the character is a control character, regardless of the sign.
1 parent 6d8e9f9 commit 70e72ab

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/jv_parse.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ static pfunc found_string(struct jv_parser* p) {
493493
return "Invalid escape";
494494
}
495495
} else {
496-
if (c >= 0 && c <= 0x001f)
496+
if (!(c & ~0x1F))
497497
return "Invalid string: control characters from U+0000 through U+001F must be escaped";
498498
*out++ = c;
499499
}

tests/shtest

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,20 @@ if printf '1\n' | $JQ -cen --seq '[inputs] == []' >/dev/null 2> $d/out; then
9494
fi
9595
cmp $d/out $d/expected
9696

97+
# Test control characters for #2909
98+
cat > $d/expected <<EOF
99+
jq: parse error: Invalid string: control characters from U+0000 through U+001F must be escaped at line 1, column 3
100+
EOF
101+
for i in 1 2 30 31; do
102+
if printf "\"$(printf '\\%03o' $i)\"" | $JQ '.' > $d/out 2>&1; then
103+
printf 'Error expected but jq exited successfully\n' 1>&2
104+
exit 2
105+
fi
106+
cmp $d/out $d/expected
107+
done
108+
printf '" ~\\u007f"\n' > $d/expected
109+
printf "\"$(printf '\\%03o' 32 126 127)\"" | $JQ '.' > $d/out 2>&1
110+
cmp $d/out $d/expected
97111

98112
## Test --exit-status
99113
data='{"i": 1}\n{"i": 2}\n{"i": 3}\n'

0 commit comments

Comments
 (0)