daemon/utils.c: Fix potential unbounded stack usage

utils.c: In function 'prog_exists':
utils.c:650:1: error: stack usage might be unbounded [-Werror=stack-usage=]
  650 | prog_exists (const char *prog)
      | ^
This commit is contained in:
Richard W.M. Jones
2021-09-07 15:43:45 +01:00
parent 58599031f8
commit a7245fba7a

View File

@@ -638,11 +638,6 @@ mountable_to_string (const mountable_t *mountable)
}
}
#if defined(__GNUC__) && GUESTFS_GCC_VERSION >= 40800 /* gcc >= 4.8.0 */
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wstack-usage="
#endif
/**
* Check program exists and is executable on C<$PATH>.
*/
@@ -650,23 +645,20 @@ int
prog_exists (const char *prog)
{
const char *pathc = getenv ("PATH");
if (!pathc)
return 0;
const size_t proglen = strlen (prog);
CLEANUP_FREE char *path = strdup (pathc);
if (path == NULL) abort ();
const char *elem;
char *saveptr;
const size_t len = strlen (pathc) + 1;
char path[len];
strcpy (path, pathc);
elem = strtok_r (path, ":", &saveptr);
while (elem) {
const size_t n = strlen (elem) + proglen + 2;
char testprog[n];
CLEANUP_FREE char *testprog;
snprintf (testprog, n, "%s/%s", elem, prog);
if (asprintf (&testprog, "%s/%s", elem, prog) == -1) abort ();
if (access (testprog, X_OK) == 0)
return 1;
@@ -677,10 +669,6 @@ prog_exists (const char *prog)
return 0;
}
#if defined(__GNUC__) && GUESTFS_GCC_VERSION >= 40800 /* gcc >= 4.8.0 */
#pragma GCC diagnostic pop
#endif
/**
* Pass a template such as C<"/sysroot/XXXXXXXX.XXX">. This updates
* the template to contain a randomly named file. Any C<'X'>