diff --git a/src/main.c b/src/main.c index 1ab5dec2..3d786070 100644 --- a/src/main.c +++ b/src/main.c @@ -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); diff --git a/tests/shtest b/tests/shtest index ba64016e..cde88152 100755 --- a/tests/shtest +++ b/tests/shtest @@ -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