From ab66dd2be92931bef04cbccdb3aa008615bd8eba Mon Sep 17 00:00:00 2001 From: Giuseppe Lumia Date: Sat, 7 Nov 2020 01:23:06 +0100 Subject: [PATCH] Simplify posix_dirname logic dirname has two main problems: 1. It could change in place the string that is passed to it. 2. It uses a static string for its return value, so one should copy it somewhere else as soon as possible to avoid subsequent calls to dirname to corrupt his data (see #48). We avoid 1. passing a copy of `path` to dirname and 2. copying it's return value into `dname`. --- src/util.c | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/util.c b/src/util.c index 573e8a7..2f62c29 100644 --- a/src/util.c +++ b/src/util.c @@ -19,13 +19,8 @@ posix_dirname(char *path, char *dname) assert(strlen(path) <= PATH_MAX); strcpy(p, path); - t = dirname(path); + t = dirname(p); memmove(dname, t, strlen(t) + 1); - - /* restore the path if dirname worked in-place */ - if (t == path && path != dname) { - strcpy(path, p); - } } /** Make directory and all of its parents */