]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Fix bug in dirname
authorJoel Rosdahl <joel@rosdahl.net>
Sat, 15 Nov 2014 15:17:15 +0000 (16:17 +0100)
committerJoel Rosdahl <joel@rosdahl.net>
Sat, 15 Nov 2014 15:17:15 +0000 (16:17 +0100)
This avoids a crash when running "ccache -s" with CCACHE_DIR="".

test/test_util.c
util.c

index c499b49d86b0785c6f37365ed2309df84ccad07d..a9c54793b1976746b94f1a5b8189bd6726602d19 100644 (file)
@@ -38,6 +38,7 @@ TEST(dirname)
        CHECK_STR_EQ_FREE2(".", dirname("foo.c"));
        CHECK_STR_EQ_FREE2(".", dirname(""));
        CHECK_STR_EQ_FREE2("/", dirname("/"));
+       CHECK_STR_EQ_FREE2("/", dirname("/foo.c"));
        CHECK_STR_EQ_FREE2("dir1/dir2", dirname("dir1/dir2/foo.c"));
        CHECK_STR_EQ_FREE2("/dir", dirname("/dir/foo.c"));
        CHECK_STR_EQ_FREE2("dir1/dir2", dirname("dir1/dir2/"));
diff --git a/util.c b/util.c
index 072f53b9bf602fab8998768dd4582aa373636b97..30c14cc967facc2d36489247f5ed11f6d71296e8 100644 (file)
--- a/util.c
+++ b/util.c
@@ -780,24 +780,27 @@ char *
 dirname(const char *path)
 {
        char *p;
-       char *p2 = NULL;
+#ifdef _WIN32
+       char *p2;
+#endif
        char *s;
        s = x_strdup(path);
        p = strrchr(s, '/');
 #ifdef _WIN32
        p2 = strrchr(s, '\\');
-#endif
-       if (p < p2)
+       if (!p || (p2 && p < p2)) {
                p = p2;
-       if (p == s) {
-               return s;
-       } else if (p) {
-               *p = 0;
-               return s;
-       } else {
+       }
+#endif
+       if (!p) {
                free(s);
-               return x_strdup(".");
+               s = x_strdup(".");
+       } else if (p == s) {
+               *(p + 1) = 0;
+       } else {
+               *p = 0;
        }
+       return s;
 }
 
 /*