From 39d313d1d474dbe025270b9555a5dfb03286c8c9 Mon Sep 17 00:00:00 2001 From: Simon Ruderich Date: Wed, 1 Jan 2014 16:37:48 +0100 Subject: [PATCH] Fix error_at_line() not always exiting if status != 0. Also add tests. --- NEWS | 8 ++++++++ src/coloredstderr.c | 3 ++- tests/example_error.c | 27 +++++++++++++++++++++++---- tests/example_error.expected | 25 +++++++++++++++++++++---- 4 files changed, 54 insertions(+), 9 deletions(-) diff --git a/NEWS b/NEWS index 2c5e477..9c37519 100644 --- a/NEWS +++ b/NEWS @@ -1,6 +1,14 @@ NEWS ==== +0.X +--- + +- Fix hook for GNU's error_at_line() not exiting if status != 0. If + error_one_per_line was used and suppressed a message, then error_at_line() + didn't exit. + + 0.1 --- diff --git a/src/coloredstderr.c b/src/coloredstderr.c index e6f1f6a..2970a1e 100644 --- a/src/coloredstderr.c +++ b/src/coloredstderr.c @@ -371,7 +371,7 @@ static void error_vararg(int status, int errnum, if (error_one_per_line && filename != NULL && linenum != 0 && filename == last_filename && linenum == last_linenum) { - return; + goto out; } last_filename = filename; last_linenum = linenum; @@ -404,6 +404,7 @@ static void error_vararg(int status, int errnum, fprintf(stderr, "\n"); +out: if (status != 0) { exit(status); } diff --git a/tests/example_error.c b/tests/example_error.c index 0ed8746..0d979db 100644 --- a/tests/example_error.c +++ b/tests/example_error.c @@ -20,12 +20,16 @@ #include #define _GNU_SOURCE /* for program_invocation_name */ -#include -#include #include #include +#include +#include +#include +#include +#include #include "../src/compiler.h" +#include "example.h" void (*error_print_progname)(void); @@ -37,26 +41,41 @@ static void print_progname(void) { int main(int argc unused, char **argv unused) { - program_invocation_name = "./example_error"; + pid_t pid; + + char name[] = "./example_error"; + program_invocation_name = name; error(0, 0, ""); error_at_line(0, 0, "file", 42, ""); + FORKED_TEST(pid) { error(1, 0, ""); } + FORKED_TEST(pid) { error_at_line(1, 0, "file", 42, ""); } error(0, ENOMEM, ""); error_at_line(0, ENOMEM, "file", 42, ""); error_at_line(0, ENOMEM, "file", 42, ""); + FORKED_TEST(pid) { error(1, ENOMEM, ""); } + FORKED_TEST(pid) { error_at_line(1, ENOMEM, "file", 42, ""); } + + fflush(stdout); + printf("\n\n"); + fflush(stdout); error_print_progname = print_progname; error_one_per_line = 1; error(0, 0, ""); error_at_line(0, 0, "file", 42, ""); + FORKED_TEST(pid) { error(1, 0, ""); } + FORKED_TEST(pid) { error_at_line(1, 0, "file", 42, ""); } error(0, ENOMEM, ""); error_at_line(0, ENOMEM, "file", 42, ""); error_at_line(0, ENOMEM, "file", 42, ""); + FORKED_TEST(pid) { error(1, ENOMEM, ""); } - /* Exit codes are not tested. */ + error_one_per_line = 0; + FORKED_TEST(pid) { error_at_line(1, ENOMEM, "file", 42, ""); } return EXIT_SUCCESS; } diff --git a/tests/example_error.expected b/tests/example_error.expected index 4302575..01c76b8 100644 --- a/tests/example_error.expected +++ b/tests/example_error.expected @@ -1,10 +1,27 @@ >STDERR>./example_error: ./example_error:file:42: -./example_error: : Cannot allocate memory +./example_error: +STDERR>./example_error:file:42: +STDERR>./example_error: : Cannot allocate memory ./example_error:file:42: : Cannot allocate memory ./example_error:file:42: : Cannot allocate memory -PROG +./example_error: : Cannot allocate memory +STDERR>./example_error:file:42: : Cannot allocate memory +STDERR>PROG PROGfile:42: -PROG: Cannot allocate memory +PROG +STDERR>PROG: Cannot allocate memory PROGfile:42: : Cannot allocate memory -: Cannot allocate memory +STDERR>PROGfile:42: : Cannot allocate memory +