Exit with the given status for negative halt_error codes (#3660)

halt_error with a negative exit code did not exit with that code. The
code collided with the internal status values for null or false output
and for no output, so halt_error(-1) exited with 0, or with 1 under
--exit-status.

Keep the low 8 bits of a negative code, as exit(3) does on POSIX
systems, so halt_error(-1) now exits with 255 and halt_error(-2) with
254, the same with or without --exit-status. Under --exit-status this
replaces the previous absolute value, so halt_error(-2) used to exit
with 2 and halt_error(-255) with 255, and they now exit with 254 and 1.
This commit is contained in:
qodelicious
2026-10-10 21:26:02 +09:00
committed by GitHub
parent a8e6d5f871
commit 04042494cf
2 changed files with 15 additions and 2 deletions
+6 -2
View File
@@ -212,9 +212,13 @@ static int process(jq_state *jq, jv value, int flags, int dumpopts, int options)
jv exit_code = jq_get_exit_code(jq);
if (!jv_is_valid(exit_code))
ret = JQ_OK;
else if (jv_get_kind(exit_code) == JV_KIND_NUMBER)
else if (jv_get_kind(exit_code) == JV_KIND_NUMBER) {
ret = jv_number_value(exit_code);
else
// Negative values would be mistaken for JQ_OK_NULL_KIND and friends,
// so keep the low 8 bits, like exit(3) does on POSIX systems.
if (ret < 0)
ret &= 0xff;
} else
ret = JQ_ERROR_UNKNOWN;
jv_free(exit_code);
jv error_message = jq_get_error_message(jq);
+9
View File
@@ -471,6 +471,15 @@ elif [ $? -ne 11 ]; then
echo "jq halt_error(11) had wrong error code" 1>&2
exit 1
fi
for opt in '' -e; do
if $VALGRIND $Q $VG_EXIT0 $JQ $opt -n 'halt_error(-1)'; then
echo "jq $opt halt_error(-1) didn't work as expected" 1>&2
exit 1
elif [ $? -ne 255 ]; then
echo "jq $opt halt_error(-1) had wrong error code" 1>&2
exit 1
fi
done
if [ -n "$($VALGRIND $Q $JQ -n 'halt_error(1)' 2>&1)" ]; then
echo "jq halt_error(1) had unexpected output" 1>&2
exit 1