From 70e72abea3267810c19937d83eff69a546d24a68 Mon Sep 17 00:00:00 2001 From: itchyny Date: Tue, 18 Feb 2025 07:52:58 +0900 Subject: [PATCH] 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. --- src/jv_parse.c | 2 +- tests/shtest | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/jv_parse.c b/src/jv_parse.c index 519c2047..0c50c67c 100644 --- a/src/jv_parse.c +++ b/src/jv_parse.c @@ -493,7 +493,7 @@ static pfunc found_string(struct jv_parser* p) { return "Invalid escape"; } } else { - if (c >= 0 && c <= 0x001f) + if (!(c & ~0x1F)) return "Invalid string: control characters from U+0000 through U+001F must be escaped"; *out++ = c; } diff --git a/tests/shtest b/tests/shtest index 7a482e51..efb63771 100755 --- a/tests/shtest +++ b/tests/shtest @@ -94,6 +94,20 @@ if printf '1\n' | $JQ -cen --seq '[inputs] == []' >/dev/null 2> $d/out; then fi cmp $d/out $d/expected +# Test control characters for #2909 +cat > $d/expected < $d/out 2>&1; then + printf 'Error expected but jq exited successfully\n' 1>&2 + exit 2 + fi + cmp $d/out $d/expected +done +printf '" ~\\u007f"\n' > $d/expected +printf "\"$(printf '\\%03o' 32 126 127)\"" | $JQ '.' > $d/out 2>&1 +cmp $d/out $d/expected ## Test --exit-status data='{"i": 1}\n{"i": 2}\n{"i": 3}\n'