From 7ff7394d9e4e9189c30fd018235e6b1728c6f2d0 Mon Sep 17 00:00:00 2001 From: Zbigniew Jędrzejewski-Szmek Date: Fri, 11 Oct 2013 19:33:13 -0400 Subject: Never call qsort on potentially NULL arrays This extends 62678ded 'efi: never call qsort on potentially NULL arrays' to all other places where qsort is used and it is not obvious that the count is non-zero. --- src/core/namespace.c | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) (limited to 'src/core') diff --git a/src/core/namespace.c b/src/core/namespace.c index 16b132ba56..936f36839b 100644 --- a/src/core/namespace.c +++ b/src/core/namespace.c @@ -222,7 +222,7 @@ int setup_namespace(char** read_write_dirs, strv_length(read_only_dirs) + strv_length(inaccessible_dirs) + (private_tmp ? 2 : 0); - BindMount *m, *mounts; + BindMount *m, *mounts = NULL; int r = 0; if (!mount_flags) @@ -231,27 +231,29 @@ int setup_namespace(char** read_write_dirs, if (unshare(CLONE_NEWNS) < 0) return -errno; - m = mounts = (BindMount *) alloca(n * sizeof(BindMount)); - if ((r = append_mounts(&m, read_write_dirs, READWRITE)) < 0 || - (r = append_mounts(&m, read_only_dirs, READONLY)) < 0 || - (r = append_mounts(&m, inaccessible_dirs, INACCESSIBLE)) < 0) - return r; + if (n) { + m = mounts = (BindMount *) alloca(n * sizeof(BindMount)); + if ((r = append_mounts(&m, read_write_dirs, READWRITE)) < 0 || + (r = append_mounts(&m, read_only_dirs, READONLY)) < 0 || + (r = append_mounts(&m, inaccessible_dirs, INACCESSIBLE)) < 0) + return r; + + if (private_tmp) { + m->path = "/tmp"; + m->mode = PRIVATE_TMP; + m++; + + m->path = "/var/tmp"; + m->mode = PRIVATE_VAR_TMP; + m++; + } - if (private_tmp) { - m->path = "/tmp"; - m->mode = PRIVATE_TMP; - m++; + assert(mounts + n == m); - m->path = "/var/tmp"; - m->mode = PRIVATE_VAR_TMP; - m++; + qsort(mounts, n, sizeof(BindMount), mount_path_compare); + drop_duplicates(mounts, &n); } - assert(mounts + n == m); - - qsort(mounts, n, sizeof(BindMount), mount_path_compare); - drop_duplicates(mounts, &n); - /* Remount / as SLAVE so that nothing now mounted in the namespace shows up in the parent */ if (mount(NULL, "/", NULL, MS_SLAVE|MS_REC, NULL) < 0) -- cgit v1.2.3-54-g00ecf