]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Fix bug in common_dir_prefix_length
authorJoel Rosdahl <joel@rosdahl.net>
Wed, 9 Apr 2014 20:11:25 +0000 (22:11 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Wed, 9 Apr 2014 20:11:25 +0000 (22:11 +0200)
Based on a patch by Douglas Graham <douglas.graham@ericsson.com>.

test/test_util.c
util.c

index afa457ae6e46c865fb790b215e7fb1040a38175a..258c3279565281b9cc90390354aa46731e6a3aa1 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2010, 2012-2013 Joel Rosdahl
+ * Copyright (C) 2010, 2012-2014 Joel Rosdahl
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License as published by the Free
@@ -53,6 +53,8 @@ TEST(common_dir_prefix_length)
        CHECK_UNS_EQ(2, common_dir_prefix_length("/a", "/a/b"));
        CHECK_UNS_EQ(2, common_dir_prefix_length("/a/b", "/a/c"));
        CHECK_UNS_EQ(4, common_dir_prefix_length("/a/b", "/a/b"));
+       CHECK_INT_EQ(2, common_dir_prefix_length("/a/bc", "/a/b"));
+       CHECK_INT_EQ(2, common_dir_prefix_length("/a/b", "/a/bc"));
 }
 
 TEST(get_relative_path)
diff --git a/util.c b/util.c
index 93d5a618fb179c059197ae0c92e16d6d6815714c..c52a8f65f87e549ef62818a1fc96b8753ee7732e 100644 (file)
--- a/util.c
+++ b/util.c
@@ -1,6 +1,6 @@
 /*
  * Copyright (C) 2002 Andrew Tridgell
- * Copyright (C) 2009-2013 Joel Rosdahl
+ * Copyright (C) 2009-2014 Joel Rosdahl
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License as published by the Free
@@ -986,24 +986,14 @@ common_dir_prefix_length(const char *s1, const char *s2)
                ++p1;
                ++p2;
        }
-       if (*p2 == '/') {
-               /* s2 starts with "s1/". */
-               return p1 - s1;
-       }
-       if (!*p2) {
-               /* s2 is equal to s1. */
-               if (p2 == s2 + 1) {
-                       /* Special case for s1 and s2 both being "/". */
-                       return 0;
-               } else {
-                       return p1 - s1;
-               }
-       }
-       /* Compute the common directory prefix */
-       while (p1 > s1 && *p1 != '/') {
+       while ((*p1 && *p1 != '/') || (*p2 && *p2 != '/')) {
                p1--;
                p2--;
        }
+       if (!*p1 && !*p2 && p2 == s2 + 1) {
+               /* Special case for s1 and s2 both being "/". */
+               return 0;
+       }
        return p1 - s1;
 }