mirror of
https://github.com/jqlang/jq.git
synced 2026-10-12 02:16:58 +00:00
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:
+6
-2
@@ -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);
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user