]> Sergey Matveev's repositories - nnn.git/commitdiff
Avoid unneeded memory allocation in xdirname()
authorsin <sin@2f30.org>
Wed, 6 Jan 2016 15:53:04 +0000 (15:53 +0000)
committersin <sin@2f30.org>
Wed, 6 Jan 2016 15:53:04 +0000 (15:53 +0000)
noice.c

diff --git a/noice.c b/noice.c
index ada28fcb51b46b4794452058faf5428a090708f3..50674243164e52ba0e4f00246d76a8936209fdf0 100644 (file)
--- a/noice.c
+++ b/noice.c
@@ -158,21 +158,17 @@ xstrdup(const char *s)
 char *
 xdirname(const char *path)
 {
-       char *p, *tmp;
+       char tmp[PATH_MAX], *p;
 
        /* Some implementations of dirname(3) may modify `path' and some
         * return a pointer inside `path' and we cannot free(3) the
         * original string if we lose track of it. */
-       tmp = xstrdup(path);
+       strlcpy(tmp, path, sizeof(tmp));
        p = dirname(tmp);
-       if (p == NULL) {
-               free(tmp);
+       if (p == NULL)
                printerr(1, "dirname");
-       }
        /* Make sure this is a malloc(3)-ed string */
-       p = xstrdup(p);
-       free(tmp);
-       return p;
+       return xstrdup(p);
 }
 
 void