commit ab66dd2be92931bef04cbccdb3aa008615bd8eba
parent 4a6172f1bf9cb41eb1ce3a5f720f9ebe4febc62b
Author: Giuseppe Lumia <g.lumia@outlook.com>
Date: Sat, 7 Nov 2020 01:23:06 +0100
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`.
Diffstat:
1 file changed, 1 insertion(+), 6 deletions(-)
diff --git 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 */