923a60
From 0881ff2b6842798836faef3a55a04a3e6e0cbb66 Mon Sep 17 00:00:00 2001
923a60
From: Michal Schmidt <mschmidt@redhat.com>
923a60
Date: Mon, 16 Mar 2015 22:04:21 +0100
923a60
Subject: [PATCH] core/namespace: fix path sorting
923a60
923a60
The comparison function we use for qsorting paths is overly indifferent.
923a60
Consider these 3 paths for sorting:
923a60
 /foo
923a60
 /bar
923a60
 /foo/foo
923a60
qsort() may compare:
923a60
 "/foo" with "/bar" => 0, indifference
923a60
 "/bar" with "/foo/foo" => 0, indifference
923a60
and assume transitively that "/foo" and "/foo/foo" are also indifferent.
923a60
923a60
But this is wrong, we want "/foo" sorted before "/foo/foo".
923a60
The comparison function must be transitive.
923a60
923a60
Use path_compare(), which behaves properly.
923a60
923a60
Fixes: https://bugzilla.redhat.com/show_bug.cgi?id=1184016
923a60
(cherry picked from commit a0827e2b123010c46cfe4f03eebba57d92f9efc4)
923a60
---
923a60
 src/core/namespace.c | 12 ++++--------
923a60
 1 file changed, 4 insertions(+), 8 deletions(-)
923a60
923a60
diff --git a/src/core/namespace.c b/src/core/namespace.c
923a60
index 4fecd32363..d4f1c86211 100644
923a60
--- a/src/core/namespace.c
923a60
+++ b/src/core/namespace.c
923a60
@@ -91,9 +91,11 @@ static int append_mounts(BindMount **p, char **strv, MountMode mode) {
923a60
 
923a60
 static int mount_path_compare(const void *a, const void *b) {
923a60
         const BindMount *p = a, *q = b;
923a60
+        int d;
923a60
 
923a60
-        if (path_equal(p->path, q->path)) {
923a60
+        d = path_compare(p->path, q->path);
923a60
 
923a60
+        if (!d) {
923a60
                 /* If the paths are equal, check the mode */
923a60
                 if (p->mode < q->mode)
923a60
                         return -1;
923a60
@@ -105,13 +107,7 @@ static int mount_path_compare(const void *a, const void *b) {
923a60
         }
923a60
 
923a60
         /* If the paths are not equal, then order prefixes first */
923a60
-        if (path_startswith(p->path, q->path))
923a60
-                return 1;
923a60
-
923a60
-        if (path_startswith(q->path, p->path))
923a60
-                return -1;
923a60
-
923a60
-        return 0;
923a60
+        return d;
923a60
 }
923a60
 
923a60
 static void drop_duplicates(BindMount *m, unsigned *n) {