qrap: Handle case of PATH environment variable being unset

clang-tidy warns that in passing getenv("PATH") to strncpy() we could be
passing a NULL pointer.  While it's unusual for PATH to be unset, it's not
impossible and this would indeed cause getenv() to return NULL.

Handle this case by never recognizing argv[2] as a qemu binary name if
PATH is not set.  This is... no flakier than the detection of whether
it's a binary name already is.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
This commit is contained in:
David Gibson 2022-09-28 14:33:30 +10:00 committed by Stefano Brivio
parent b35a6cfa0c
commit 740ea28f29

5
qrap.c
View file

@ -173,12 +173,13 @@ int main(int argc, char **argv)
char probe_r;
if (argc >= 3) {
const char *path = getenv("PATH");
errno = 0;
fd = strtol(argv[1], NULL, 0);
if (fd >= 3 && fd < INT_MAX && !errno) {
if (fd >= 3 && fd < INT_MAX && !errno && path) {
char env_path[ARG_MAX + 1], *p, command[ARG_MAX];
strncpy(env_path, getenv("PATH"), ARG_MAX);
strncpy(env_path, path, ARG_MAX);
/* cppcheck-suppress strtokCalled */
p = strtok(env_path, ":");
while (p) {