systemf(): fix improper handling of WEXITSTATUS()

The call to system() may return -1, e.g. if fork() fails or return
status from execl() cannot be read.  In these cases we should end
immediately and not call WIFEXITED() et al.

Also, WEXITSTATUS() should only be called if WEXITED() returns true.

For details, see system(3) and wait(2).

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg
2021-03-26 12:32:43 +01:00
parent c15e883427
commit 8ce46eb439
+4 -2
View File
@@ -43,8 +43,9 @@ int systemf(const char *fmt, ...)
va_end(ap);
status = system(cmd);
if (status == -1)
return -1;
rc = WEXITSTATUS(status);
if (!WIFEXITED(status)) {
if (WIFSIGNALED(status) &&
(WTERMSIG(status) == SIGINT ||
@@ -59,7 +60,8 @@ int systemf(const char *fmt, ...)
*/
rc = 1;
}
}
} else
rc = WEXITSTATUS(status);
return rc;
}