install: allow paths like LookupPath.generator to be NULL
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Sun, 17 Apr 2016 03:08:23 +0000 (23:08 -0400)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Sun, 17 Apr 2016 03:08:23 +0000 (23:08 -0400)
Fixes #3047.

src/basic/path-util.h
src/shared/install.c
src/test/test-path-util.c

index f43d477..34d5cd1 100644 (file)
@@ -48,6 +48,10 @@ bool path_equal(const char *a, const char *b) _pure_;
 bool path_equal_or_files_same(const char *a, const char *b);
 char* path_join(const char *root, const char *path, const char *rest);
 
+static inline bool path_equal_ptr(const char *a, const char *b) {
+        return !!a == !!b && (!a || path_equal(a, b));
+}
+
 /* Note: the search terminates on the first NULL item. */
 #define PATH_IN_SET(p, ...)                                     \
         ({                                                      \
index 74de814..10c724e 100644 (file)
@@ -119,9 +119,9 @@ static int path_is_generator(const LookupPaths *p, const char *path) {
         if (!parent)
                 return -ENOMEM;
 
-        return path_equal(p->generator, parent) ||
-                path_equal(p->generator_early, parent) ||
-                path_equal(p->generator_late, parent);
+        return path_equal_ptr(parent, p->generator) ||
+               path_equal_ptr(parent, p->generator_early) ||
+               path_equal_ptr(parent, p->generator_late);
 }
 
 static int path_is_transient(const LookupPaths *p, const char *path) {
@@ -134,7 +134,7 @@ static int path_is_transient(const LookupPaths *p, const char *path) {
         if (!parent)
                 return -ENOMEM;
 
-        return path_equal(p->transient, parent);
+        return path_equal_ptr(parent, p->transient);
 }
 
 static int path_is_control(const LookupPaths *p, const char *path) {
@@ -147,8 +147,8 @@ static int path_is_control(const LookupPaths *p, const char *path) {
         if (!parent)
                 return -ENOMEM;
 
-        return path_equal(parent, p->persistent_control) ||
-                path_equal(parent, p->runtime_control);
+        return path_equal_ptr(parent, p->persistent_control) ||
+               path_equal_ptr(parent, p->runtime_control);
 }
 
 static int path_is_config(const LookupPaths *p, const char *path) {
@@ -164,8 +164,8 @@ static int path_is_config(const LookupPaths *p, const char *path) {
         if (!parent)
                 return -ENOMEM;
 
-        return path_equal(parent, p->persistent_config) ||
-               path_equal(parent, p->runtime_config);
+        return path_equal_ptr(parent, p->persistent_config) ||
+               path_equal_ptr(parent, p->runtime_config);
 }
 
 static int path_is_runtime(const LookupPaths *p, const char *path) {
@@ -186,12 +186,12 @@ static int path_is_runtime(const LookupPaths *p, const char *path) {
         if (!parent)
                 return -ENOMEM;
 
-        return path_equal(parent, p->runtime_config) ||
-               path_equal(parent, p->generator) ||
-               path_equal(parent, p->generator_early) ||
-               path_equal(parent, p->generator_late) ||
-               path_equal(parent, p->transient) ||
-               path_equal(parent, p->runtime_control);
+        return path_equal_ptr(parent, p->runtime_config) ||
+               path_equal_ptr(parent, p->generator) ||
+               path_equal_ptr(parent, p->generator_early) ||
+               path_equal_ptr(parent, p->generator_late) ||
+               path_equal_ptr(parent, p->transient) ||
+               path_equal_ptr(parent, p->runtime_control);
 }
 
 static int path_is_vendor(const LookupPaths *p, const char *path) {
index 7ce2695..5d77e29 100644 (file)
@@ -96,6 +96,12 @@ static void test_path(void) {
         assert_se(PATH_IN_SET("/bin", "/foo/bar", "/bin"));
         assert_se(PATH_IN_SET("/", "/", "/", "/foo/bar"));
         assert_se(!PATH_IN_SET("/", "/abc", "/def"));
+
+        assert_se(path_equal_ptr(NULL, NULL));
+        assert_se(path_equal_ptr("/a", "/a"));
+        assert_se(!path_equal_ptr("/a", "/b"));
+        assert_se(!path_equal_ptr("/a", NULL));
+        assert_se(!path_equal_ptr(NULL, "/a"));
 }
 
 static void test_find_binary(const char *self) {