From: Vincent Bernat Date: Sun, 21 Mar 2021 14:22:06 +0000 (+0100) Subject: daemon: rewrite `mkdir_p()` to not use strtok/strcat X-Git-Tag: 1.0.9~7 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=03ed6a501726917fab102d22e5c8933094edbd9a;p=thirdparty%2Flldpd.git daemon: rewrite `mkdir_p()` to not use strtok/strcat --- diff --git a/src/daemon/priv.c b/src/daemon/priv.c index 08b58619..7810f16b 100644 --- a/src/daemon/priv.c +++ b/src/daemon/priv.c @@ -521,8 +521,8 @@ sig_chld(int sig) /* Create a directory recursively. */ static int mkdir_p(const char *pathname, mode_t mode) { - char path[PATH_MAX+1], current[PATH_MAX+1] = {}; - char *tok; + char path[PATH_MAX+1]; + char *current; if (strlcpy(path, pathname, sizeof(path)) >= sizeof(path)) { errno = ENAMETOOLONG; @@ -530,16 +530,12 @@ static int mkdir_p(const char *pathname, mode_t mode) } /* Use strtok which will provides non-empty tokens only. */ - if (path[0] == '/') current[0] = '/'; - tok = strtok(path, "/"); - while (tok) { - /* coverity[string_overflow] - No overflow possible because path is at most 4096 long */ - strcat(current, tok); + for (current = path + 1; *current; current++) { + if (*current != '/') continue; + *current = '\0'; if (mkdir(current, mode) != 0 && errno != EEXIST) return -1; - strcat(current, "/"); - tok = strtok(NULL, "/"); + *current = '/'; } errno = 0;