Skip to content

Commit ebec842

Browse files
jrngitster
authored andcommitted
run-command: prettify -D_FORTIFY_SOURCE workaround
Current gcc + glibc with -D_FORTIFY_SOURCE try very aggressively to protect against a programming style which uses write(...) without checking the return value for errors. Even the usual hint of casting to (void) does not suppress the warning. Sometimes when there is an output error, especially right before exit, there really is nothing to be done. The obvious solution, adopted in v1.7.0.3~20^2 (run-command.c: fix build warnings on Ubuntu, 2010-01-30), is to save the return value to a dummy variable: ssize_t dummy; dummy = write(...); But that (1) is ugly and (2) triggers -Wunused-but-set-variable warnings with gcc-4.6 -Wall, so we are not much better off than when we started. Instead, use an "if" statement with an empty body to make the intent clear. if (write(...)) ; /* yes, yes, there was an error. */ Signed-off-by: Jonathan Nieder <jrnieder@gmail.com> Improved-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent a3ca9b0 commit ebec842

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

run-command.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,21 +67,26 @@ static int child_notifier = -1;
6767

6868
static void notify_parent(void)
6969
{
70-
ssize_t unused;
71-
unused = write(child_notifier, "", 1);
70+
/*
71+
* execvp failed. If possible, we'd like to let start_command
72+
* know, so failures like ENOENT can be handled right away; but
73+
* otherwise, finish_command will still report the error.
74+
*/
75+
if (write(child_notifier, "", 1))
76+
; /* yes, dear gcc -D_FORTIFY_SOURCE, there was an error. */
7277
}
7378

7479
static NORETURN void die_child(const char *err, va_list params)
7580
{
7681
char msg[4096];
77-
ssize_t unused;
7882
int len = vsnprintf(msg, sizeof(msg), err, params);
7983
if (len > sizeof(msg))
8084
len = sizeof(msg);
8185

82-
unused = write(child_err, "fatal: ", 7);
83-
unused = write(child_err, msg, len);
84-
unused = write(child_err, "\n", 1);
86+
if (write(child_err, "fatal: ", 7) ||
87+
write(child_err, msg, len) ||
88+
write(child_err, "\n", 1))
89+
; /* yes, gcc -D_FORTIFY_SOURCE, we know there was an error. */
8590
exit(128);
8691
}
8792
#endif

0 commit comments

Comments
 (0)