Skip to content
Myr

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

Before adoption: this commit came in with the history the project adopted, so no landing records it.

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.

authored by qodelicious · committed by GitHub · parent a8e6d5f · Browse files · 2 files+15 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+15 added−2 removed

  • tests:+9 added−0 removed
  • other:+6 added−2 removed

2 files+15 added−2 removed

modified:src/main.c+6 added-2 removed
210 unmodified lines
211 // jq program invoked `halt` or `halt_error`212 jv exit_code = jq_get_exit_code(jq);213 if (!jv_is_valid(exit_code))214 ret = JQ_OK;215 else if (jv_get_kind(exit_code) == JV_KIND_NUMBER)215 else if (jv_get_kind(exit_code) == JV_KIND_NUMBER) {216 ret = jv_number_value(exit_code);217 // Negative values would be mistaken for JQ_OK_NULL_KIND and friends,218 // so keep the low 8 bits, like exit(3) does on POSIX systems.219 if (ret < 0)220 ret &= 0xff;217 else221 } else222 ret = JQ_ERROR_UNKNOWN;223 jv_free(exit_code);224 jv error_message = jq_get_error_message(jq);225 if (jv_get_kind(error_message) == JV_KIND_STRING) {
509 unmodified lines
modified:tests/shtest+9 added
469 unmodified lines
470elif [ $? -ne 11 ]; then471 echo "jq halt_error(11) had wrong error code" 1>&2472 exit 1473fi474for opt in '' -e; do475 if $VALGRIND $Q $VG_EXIT0 $JQ $opt -n 'halt_error(-1)'; then476 echo "jq $opt halt_error(-1) didn't work as expected" 1>&2477 exit 1478 elif [ $? -ne 255 ]; then479 echo "jq $opt halt_error(-1) had wrong error code" 1>&2480 exit 1481 fi482done483if [ -n "$($VALGRIND $Q $JQ -n 'halt_error(1)' 2>&1)" ]; then484 echo "jq halt_error(1) had unexpected output" 1>&2485 exit 1486fi
499 unmodified lines