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