From 25984fa1bdb72428c118524871cd9bd232c2785d Mon Sep 17 00:00:00 2001 From: Joel Rosdahl Date: Wed, 9 Apr 2014 22:11:25 +0200 Subject: [PATCH] Fix bug in common_dir_prefix_length Based on a patch by Douglas Graham . --- test/test_util.c | 4 +++- util.c | 22 ++++++---------------- 2 files changed, 9 insertions(+), 17 deletions(-) diff --git a/test/test_util.c b/test/test_util.c index afa457ae6..258c32795 100644 --- a/test/test_util.c +++ b/test/test_util.c @@ -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 93d5a618f..c52a8f65f 100644 --- 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; } -- 2.47.2