From 342a9bb54b36fc054c59cec961322a025634229f Mon Sep 17 00:00:00 2001 From: Paul Smith Date: Sun, 6 Feb 2022 16:22:40 -0500 Subject: [PATCH] Don't write $(shell ...) stdout to stderr on failure If a $(shell ...) invocation failed due to a command-not-found error, make wrote the stdout of that shell to our stderr for some reason. That seems very wrong. If the command's stderr was not redirected then its output would have already been written to its stderr, and if it was redirected then we shouldn't take it upon ourselves to force it to go to stderr! * src/function.c (func_shell_base): Append shell stdout even if the shell command failed. * tests/run_make_tests.pl: Determine the error generated for command-not-found situations. * tests/scripts/functions/shell: Verify that redirecting stderr to stdout will behave properly if the command is not found. --- src/function.c | 22 ++++------------------ tests/run_make_tests.pl | 10 ++++++++++ tests/scripts/functions/shell | 22 +++++++++++++++------- 3 files changed, 29 insertions(+), 25 deletions(-) diff --git a/src/function.c b/src/function.c index d7c13923..9add8f65 100644 --- a/src/function.c +++ b/src/function.c @@ -2012,24 +2012,10 @@ func_shell_base (char *o, char **argv, int trim_newlines) } shell_function_pid = 0; - /* shell_completed() will set shell_function_completed to 1 when the - child dies normally, or to -1 if it dies with status 127, which is - most likely an exec fail. */ - - if (shell_function_completed == -1) - { - /* This likely means that the execvp failed, so we should just - write the error message in the pipe from the child. */ - fputs (buffer, stderr); - fflush (stderr); - } - else - { - /* The child finished normally. Replace all newlines in its output - with spaces, and put that in the variable output buffer. */ - fold_newlines (buffer, &i, trim_newlines); - o = variable_buffer_output (o, buffer, i); - } + /* Replace all newlines in the command's output with spaces, and put that + in the variable output buffer. */ + fold_newlines (buffer, &i, trim_newlines); + o = variable_buffer_output (o, buffer, i); free (buffer); } diff --git a/tests/run_make_tests.pl b/tests/run_make_tests.pl index 4cc375e5..188a57c4 100644 --- a/tests/run_make_tests.pl +++ b/tests/run_make_tests.pl @@ -108,6 +108,7 @@ $ERR_read_only_file = undef; $ERR_unreadable_file = undef; $ERR_nonexe_file = undef; $ERR_exe_dir = undef; +$ERR_command_not_found = undef; { use locale; @@ -164,6 +165,15 @@ $ERR_exe_dir = undef; unlink('file.out') or die "Failed to delete file.out: $!\n"; + $_ = `/bin/sh -c 'bad-command 2>&1'`; + if ($? == 0) { + print "Invoked invalid file! Skipping related tests.\n"; + } else { + chomp($_); + s/bad-command/#CMDNAME#/g; + $ERR_command_not_found = $_; + } + $loc and POSIX::setlocale(&POSIX::LC_ALL, $loc); } diff --git a/tests/scripts/functions/shell b/tests/scripts/functions/shell index 59865293..63320a2b 100644 --- a/tests/scripts/functions/shell +++ b/tests/scripts/functions/shell @@ -46,10 +46,11 @@ if ($port_type ne 'W32') { # This needs to be ported to Windows, or else Windows error messages # need to converted to look like more normal make errors. run_make_test(' +.RECIPEPREFIX = > all: - @echo hi - $(shell ./basdfdfsed there) - @echo $(.SHELLSTATUS) +>@echo hi +>$(shell ./basdfdfsed there) +>@echo $(.SHELLSTATUS) ', '', "#MAKE#: ./basdfdfsed: $ERR_no_such_file\nhi\n127\n"); @@ -81,10 +82,17 @@ $(shell kill -2 $$$$) STAT := $(.SHELLSTATUS) all: ; @echo STAT=$(STAT) ','',"STAT=$ret\n"); + + # Test that not-found errors can be redirected + if ($ERR_command_not_found) { + $_ = $ERR_command_not_found; + s/#CMDNAME#/bad-command/g; + run_make_test(q! +out := $(shell bad-command 2>&1) +all: ; @echo '$(.SHELLSTATUS): $(out)' +!, + '', "127: $_\n"); + } } 1; - -### Local Variables: -### eval: (setq whitespace-action (delq 'auto-cleanup whitespace-action)) -### End: