From 708b21897f87d6582cc18fa9e8801393118baf7f Mon Sep 17 00:00:00 2001 From: Adam Sutton Date: Wed, 22 Aug 2012 21:23:47 +0100 Subject: [PATCH] Fix infinite loop in spawn code if bad PATH is supplied. Thanks to Rene Herbrich for spotting. Fixes #1144. --- src/spawn.c | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/spawn.c b/src/spawn.c index f12e9e10..c7ffbe51 100644 --- a/src/spawn.c +++ b/src/spawn.c @@ -61,17 +61,18 @@ _find_exec ( const char *name, char *out, size_t len ) path = strdup(path); tmp = strtok(path, ":"); while (tmp && !ret) { - if (!(dir = opendir(tmp))) continue; - while ((de = readdir(dir))) { - if (strstr(de->d_name, name) != de->d_name) continue; - snprintf(bin, sizeof(bin), "%s/%s", tmp, de->d_name); - if (lstat(bin, &st)) continue; - if (!S_ISREG(st.st_mode) || !(st.st_mode & S_IEXEC)) continue; - strncpy(out, bin, len); - ret = 1; - break; + if ((dir = opendir(tmp))) { + while ((de = readdir(dir))) { + if (strstr(de->d_name, name) != de->d_name) continue; + snprintf(bin, sizeof(bin), "%s/%s", tmp, de->d_name); + if (lstat(bin, &st)) continue; + if (!S_ISREG(st.st_mode) || !(st.st_mode & S_IEXEC)) continue; + strncpy(out, bin, len); + ret = 1; + break; + } + closedir(dir); } - closedir(dir); tmp = strtok(NULL, ":"); } free(path);