diff --git a/src/main.c b/src/main.c index abe4e959..43f7338c 100644 --- a/src/main.c +++ b/src/main.c @@ -222,15 +222,16 @@ static int process(jq_state *jq, jv value, int flags, int dumpopts, int options) ret = JQ_ERROR_UNKNOWN; jv_free(exit_code); jv error_message = jq_get_error_message(jq); - if (jv_get_kind(error_message) == JV_KIND_STRING) { - // No prefix should be added to the output of `halt_error`. - priv_fwrite(jv_string_value(error_message), jv_string_length_bytes(jv_copy(error_message)), - stderr, dumpopts & JV_PRINT_ISATTY); - } else if (jv_get_kind(error_message) == JV_KIND_NULL) { + if (jv_get_kind(error_message) == JV_KIND_NULL) { // Halt with no output } else if (jv_is_valid(error_message)) { - error_message = jv_dump_string(error_message, 0); - fprintf(stderr, "%s\n", jv_string_value(error_message)); + if (jv_get_kind(error_message) != JV_KIND_STRING) { + error_message = jv_dump_string(error_message, 0); + } + // No prefix or trailing newline should be added to the output of + // `halt_error`. + priv_fwrite(jv_string_value(error_message), jv_string_length_bytes(jv_copy(error_message)), + stderr, dumpopts & JV_PRINT_ISATTY); } // else no message on stderr; use --debug-trace to see a message fflush(stderr); jv_free(error_message); diff --git a/tests/shtest b/tests/shtest index 5f4185a9..941c0cac 100755 --- a/tests/shtest +++ b/tests/shtest @@ -566,7 +566,7 @@ if [ "$($VALGRIND $Q $JQ -n '"x\u0000y\u0000z" | halt_error(1)' 2>&1 | tr '\0' ' echo "jq halt_error(1) had unexpected output" 1>&2 exit 1 fi -if [ "$($VALGRIND $Q $JQ -n '{"a":"xyz"} | halt_error(1)' 2>&1)" != '{"a":"xyz"}' ]; then +if [ "$($VALGRIND $Q $JQ -n '{"a":"xyz"} | halt_error(1)' 2>&1 || echo "z")" != '{"a":"xyz"}z' ]; then echo "jq halt_error(1) had unexpected output" 1>&2 exit 1 fi