ryantimwilson / rpms / systemd

Forked from rpms/systemd 2 months ago
Clone
572a44
From 43406f091aaa58ed23250a9967f46ad9cfa0b007 Mon Sep 17 00:00:00 2001
572a44
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
572a44
Date: Fri, 11 Oct 2013 19:33:13 -0400
572a44
Subject: [PATCH] Never call qsort on potentially NULL arrays
572a44
572a44
This extends 62678ded 'efi: never call qsort on potentially
572a44
NULL arrays' to all other places where qsort is used and it
572a44
is not obvious that the count is non-zero.
572a44
---
572a44
 src/analyze/systemd-analyze.c     |  2 +-
572a44
 src/cgtop/cgtop.c                 |  2 +-
572a44
 src/core/namespace.c              | 38 ++++++++++++++++++++------------------
572a44
 src/journal/catalog.c             |  2 +-
572a44
 src/journal/journal-vacuum.c      |  3 +--
572a44
 src/journal/journalctl.c          |  2 +-
572a44
 src/libsystemd-bus/bus-match.c    |  2 +-
572a44
 src/libudev/libudev-enumerate.c   |  2 +-
572a44
 src/nss-myhostname/netlink.c      |  3 ++-
572a44
 src/readahead/readahead-collect.c | 39 ++++++++++++++++++++++-----------------
572a44
 src/shared/cgroup-show.c          |  2 ++
572a44
 src/shared/conf-files.c           |  2 +-
572a44
 src/shared/efivars.c              |  3 +--
572a44
 src/shared/fileio.c               |  1 +
572a44
 src/shared/util.h                 | 12 ++++++++++++
572a44
 src/systemctl/systemctl.c         | 10 +++++-----
572a44
 16 files changed, 73 insertions(+), 52 deletions(-)
572a44
572a44
diff --git a/src/analyze/systemd-analyze.c b/src/analyze/systemd-analyze.c
572a44
index 27d063c..a4f15eb 100644
572a44
--- a/src/analyze/systemd-analyze.c
572a44
+++ b/src/analyze/systemd-analyze.c
572a44
@@ -768,7 +768,7 @@ static int list_dependencies_one(DBusConnection *bus, const char *name, unsigned
572a44
         if (r < 0)
572a44
                 return r;
572a44
 
572a44
-        qsort(deps, strv_length(deps), sizeof (char*), list_dependencies_compare);
572a44
+        qsort_safe(deps, strv_length(deps), sizeof (char*), list_dependencies_compare);
572a44
 
572a44
         r = acquire_boot_times(bus, &boot);
572a44
         if (r < 0)
572a44
diff --git a/src/cgtop/cgtop.c b/src/cgtop/cgtop.c
572a44
index cacf705..293a211 100644
572a44
--- a/src/cgtop/cgtop.c
572a44
+++ b/src/cgtop/cgtop.c
572a44
@@ -461,7 +461,7 @@ static int display(Hashmap *a) {
572a44
                 if (g->n_tasks_valid || g->cpu_valid || g->memory_valid || g->io_valid)
572a44
                         array[n++] = g;
572a44
 
572a44
-        qsort(array, n, sizeof(Group*), group_compare);
572a44
+        qsort_safe(array, n, sizeof(Group*), group_compare);
572a44
 
572a44
         /* Find the longest names in one run */
572a44
         for (j = 0; j < n; j++) {
572a44
diff --git a/src/core/namespace.c b/src/core/namespace.c
572a44
index 16b132b..936f368 100644
572a44
--- a/src/core/namespace.c
572a44
+++ b/src/core/namespace.c
572a44
@@ -222,7 +222,7 @@ int setup_namespace(char** read_write_dirs,
572a44
                      strv_length(read_only_dirs) +
572a44
                      strv_length(inaccessible_dirs) +
572a44
                      (private_tmp ? 2 : 0);
572a44
-        BindMount *m, *mounts;
572a44
+        BindMount *m, *mounts = NULL;
572a44
         int r = 0;
572a44
 
572a44
         if (!mount_flags)
572a44
@@ -231,27 +231,29 @@ int setup_namespace(char** read_write_dirs,
572a44
         if (unshare(CLONE_NEWNS) < 0)
572a44
                 return -errno;
572a44
 
572a44
-        m = mounts = (BindMount *) alloca(n * sizeof(BindMount));
572a44
-        if ((r = append_mounts(&m, read_write_dirs, READWRITE)) < 0 ||
572a44
-                (r = append_mounts(&m, read_only_dirs, READONLY)) < 0 ||
572a44
-                (r = append_mounts(&m, inaccessible_dirs, INACCESSIBLE)) < 0)
572a44
-                return r;
572a44
+        if (n) {
572a44
+                m = mounts = (BindMount *) alloca(n * sizeof(BindMount));
572a44
+                if ((r = append_mounts(&m, read_write_dirs, READWRITE)) < 0 ||
572a44
+                    (r = append_mounts(&m, read_only_dirs, READONLY)) < 0 ||
572a44
+                    (r = append_mounts(&m, inaccessible_dirs, INACCESSIBLE)) < 0)
572a44
+                        return r;
572a44
+
572a44
+                if (private_tmp) {
572a44
+                        m->path = "/tmp";
572a44
+                        m->mode = PRIVATE_TMP;
572a44
+                        m++;
572a44
+
572a44
+                        m->path = "/var/tmp";
572a44
+                        m->mode = PRIVATE_VAR_TMP;
572a44
+                        m++;
572a44
+                }
572a44
 
572a44
-        if (private_tmp) {
572a44
-                m->path = "/tmp";
572a44
-                m->mode = PRIVATE_TMP;
572a44
-                m++;
572a44
+                assert(mounts + n == m);
572a44
 
572a44
-                m->path = "/var/tmp";
572a44
-                m->mode = PRIVATE_VAR_TMP;
572a44
-                m++;
572a44
+                qsort(mounts, n, sizeof(BindMount), mount_path_compare);
572a44
+                drop_duplicates(mounts, &n);
572a44
         }
572a44
 
572a44
-        assert(mounts + n == m);
572a44
-
572a44
-        qsort(mounts, n, sizeof(BindMount), mount_path_compare);
572a44
-        drop_duplicates(mounts, &n);
572a44
-
572a44
         /* Remount / as SLAVE so that nothing now mounted in the namespace
572a44
            shows up in the parent */
572a44
         if (mount(NULL, "/", NULL, MS_SLAVE|MS_REC, NULL) < 0)
572a44
diff --git a/src/journal/catalog.c b/src/journal/catalog.c
572a44
index 7738d24..90ca008 100644
572a44
--- a/src/journal/catalog.c
572a44
+++ b/src/journal/catalog.c
572a44
@@ -399,7 +399,7 @@ int catalog_update(const char* database, const char* root, const char* const* di
572a44
         }
572a44
 
572a44
         assert(n == hashmap_size(h));
572a44
-        qsort(items, n, sizeof(CatalogItem), catalog_compare_func);
572a44
+        qsort_safe(items, n, sizeof(CatalogItem), catalog_compare_func);
572a44
 
572a44
         r = write_catalog(database, h, sb, items, n);
572a44
         if (r < 0)
572a44
diff --git a/src/journal/journal-vacuum.c b/src/journal/journal-vacuum.c
572a44
index 8d5effb..d4a1c6c 100644
572a44
--- a/src/journal/journal-vacuum.c
572a44
+++ b/src/journal/journal-vacuum.c
572a44
@@ -299,8 +299,7 @@ int journal_directory_vacuum(
572a44
                 n_list ++;
572a44
         }
572a44
 
572a44
-        if (n_list > 0)
572a44
-                qsort(list, n_list, sizeof(struct vacuum_info), vacuum_compare);
572a44
+        qsort_safe(list, n_list, sizeof(struct vacuum_info), vacuum_compare);
572a44
 
572a44
         for (i = 0; i < n_list; i++) {
572a44
                 struct statvfs ss;
572a44
diff --git a/src/journal/journalctl.c b/src/journal/journalctl.c
572a44
index 9a2d255..0876ee6 100644
572a44
--- a/src/journal/journalctl.c
572a44
+++ b/src/journal/journalctl.c
572a44
@@ -755,7 +755,7 @@ static int get_relative_boot_id(sd_journal *j, sd_id128_t *boot_id, int relative
572a44
                 sd_journal_flush_matches(j);
572a44
         }
572a44
 
572a44
-        qsort(all_ids, count, sizeof(boot_id_t), boot_id_cmp);
572a44
+        qsort_safe(all_ids, count, sizeof(boot_id_t), boot_id_cmp);
572a44
 
572a44
         if (sd_id128_equal(*boot_id, SD_ID128_NULL)) {
572a44
                 if (relative > (int) count || relative <= -(int)count)
572a44
diff --git a/src/libsystemd-bus/bus-match.c b/src/libsystemd-bus/bus-match.c
572a44
index 1411167..916682a 100644
572a44
--- a/src/libsystemd-bus/bus-match.c
572a44
+++ b/src/libsystemd-bus/bus-match.c
572a44
@@ -768,7 +768,7 @@ int bus_match_parse(
572a44
         }
572a44
 
572a44
         /* Order the whole thing, so that we always generate the same tree */
572a44
-        qsort(components, n_components, sizeof(struct bus_match_component), match_component_compare);
572a44
+        qsort_safe(components, n_components, sizeof(struct bus_match_component), match_component_compare);
572a44
 
572a44
         /* Check for duplicates */
572a44
         for (i = 0; i+1 < n_components; i++)
572a44
diff --git a/src/libudev/libudev-enumerate.c b/src/libudev/libudev-enumerate.c
572a44
index 8146f27..e71d766 100644
572a44
--- a/src/libudev/libudev-enumerate.c
572a44
+++ b/src/libudev/libudev-enumerate.c
572a44
@@ -276,7 +276,7 @@ _public_ struct udev_list_entry *udev_enumerate_get_list_entry(struct udev_enume
572a44
                 size_t move_later_prefix = 0;
572a44
 
572a44
                 udev_list_cleanup(&udev_enumerate->devices_list);
572a44
-                qsort(udev_enumerate->devices, udev_enumerate->devices_cur, sizeof(struct syspath), syspath_cmp);
572a44
+                qsort_safe(udev_enumerate->devices, udev_enumerate->devices_cur, sizeof(struct syspath), syspath_cmp);
572a44
 
572a44
                 max = udev_enumerate->devices_cur;
572a44
                 for (i = 0; i < max; i++) {
572a44
diff --git a/src/nss-myhostname/netlink.c b/src/nss-myhostname/netlink.c
572a44
index b1ef912..47a41f5 100644
572a44
--- a/src/nss-myhostname/netlink.c
572a44
+++ b/src/nss-myhostname/netlink.c
572a44
@@ -197,7 +197,8 @@ finish:
572a44
                 return r;
572a44
         }
572a44
 
572a44
-        qsort(list, n_list, sizeof(struct address), address_compare);
572a44
+        if (n_list)
572a44
+                qsort(list, n_list, sizeof(struct address), address_compare);
572a44
 
572a44
         *_list = list;
572a44
         *_n_list = n_list;
572a44
diff --git a/src/readahead/readahead-collect.c b/src/readahead/readahead-collect.c
572a44
index 32888ad..6b74866 100644
572a44
--- a/src/readahead/readahead-collect.c
572a44
+++ b/src/readahead/readahead-collect.c
572a44
@@ -536,8 +536,7 @@ done:
572a44
                 HASHMAP_FOREACH_KEY(q, p, files, i)
572a44
                         pack_file(pack, p, on_btrfs);
572a44
         } else {
572a44
-                struct item *ordered, *j;
572a44
-                unsigned k, n;
572a44
+                unsigned n;
572a44
 
572a44
                 /* On rotating media, order things by the block
572a44
                  * numbers */
572a44
@@ -545,25 +544,31 @@ done:
572a44
                 log_debug("Ordering...");
572a44
 
572a44
                 n = hashmap_size(files);
572a44
-                if (!(ordered = new(struct item, n))) {
572a44
-                        r = log_oom();
572a44
-                        goto finish;
572a44
-                }
572a44
-
572a44
-                j = ordered;
572a44
-                HASHMAP_FOREACH_KEY(q, p, files, i) {
572a44
-                        memcpy(j, q, sizeof(struct item));
572a44
-                        j++;
572a44
-                }
572a44
+                if (n) {
572a44
+                        _cleanup_free_ struct item *ordered;
572a44
+                        struct item *j;
572a44
+                        unsigned k;
572a44
+
572a44
+                        ordered = new(struct item, n);
572a44
+                        if (!ordered) {
572a44
+                                r = log_oom();
572a44
+                                goto finish;
572a44
+                        }
572a44
 
572a44
-                assert(ordered + n == j);
572a44
+                        j = ordered;
572a44
+                        HASHMAP_FOREACH_KEY(q, p, files, i) {
572a44
+                                memcpy(j, q, sizeof(struct item));
572a44
+                                j++;
572a44
+                        }
572a44
 
572a44
-                qsort(ordered, n, sizeof(struct item), qsort_compare);
572a44
+                        assert(ordered + n == j);
572a44
 
572a44
-                for (k = 0; k < n; k++)
572a44
-                        pack_file(pack, ordered[k].path, on_btrfs);
572a44
+                        qsort(ordered, n, sizeof(struct item), qsort_compare);
572a44
 
572a44
-                free(ordered);
572a44
+                        for (k = 0; k < n; k++)
572a44
+                                pack_file(pack, ordered[k].path, on_btrfs);
572a44
+                } else
572a44
+                        log_warning("No pack files");
572a44
         }
572a44
 
572a44
         log_debug("Finalizing...");
572a44
diff --git a/src/shared/cgroup-show.c b/src/shared/cgroup-show.c
572a44
index e971f36..cc44ab4 100644
572a44
--- a/src/shared/cgroup-show.c
572a44
+++ b/src/shared/cgroup-show.c
572a44
@@ -44,6 +44,8 @@ static void show_pid_array(int pids[], unsigned n_pids, const char *prefix, unsi
572a44
         unsigned i, m, pid_width;
572a44
         pid_t biggest = 0;
572a44
 
572a44
+        assert(n_pids > 0);
572a44
+
572a44
         /* Filter duplicates */
572a44
         m = 0;
572a44
         for (i = 0; i < n_pids; i++) {
572a44
diff --git a/src/shared/conf-files.c b/src/shared/conf-files.c
572a44
index 6d99739..ed4070c 100644
572a44
--- a/src/shared/conf-files.c
572a44
+++ b/src/shared/conf-files.c
572a44
@@ -127,7 +127,7 @@ static int conf_files_list_strv_internal(char ***strv, const char *suffix, const
572a44
                 return -ENOMEM;
572a44
         }
572a44
 
572a44
-        qsort(files, hashmap_size(fh), sizeof(char *), base_cmp);
572a44
+        qsort_safe(files, hashmap_size(fh), sizeof(char *), base_cmp);
572a44
         *strv = files;
572a44
 
572a44
         hashmap_free(fh);
572a44
diff --git a/src/shared/efivars.c b/src/shared/efivars.c
572a44
index c015b16..f3eb6a6 100644
572a44
--- a/src/shared/efivars.c
572a44
+++ b/src/shared/efivars.c
572a44
@@ -384,8 +384,7 @@ int efi_get_boot_options(uint16_t **options) {
572a44
                 list[count ++] = id;
572a44
         }
572a44
 
572a44
-        if (list)
572a44
-                qsort(list, count, sizeof(uint16_t), cmp_uint16);
572a44
+        qsort_safe(list, count, sizeof(uint16_t), cmp_uint16);
572a44
 
572a44
         *options = list;
572a44
         return count;
572a44
diff --git a/src/shared/fileio.c b/src/shared/fileio.c
572a44
index 603a1c7..733b320 100644
572a44
--- a/src/shared/fileio.c
572a44
+++ b/src/shared/fileio.c
572a44
@@ -662,6 +662,7 @@ int get_status_field(const char *filename, const char *pattern, char **field) {
572a44
         int r;
572a44
 
572a44
         assert(filename);
572a44
+        assert(pattern);
572a44
         assert(field);
572a44
 
572a44
         r = read_full_file(filename, &status, NULL);
572a44
diff --git a/src/shared/util.h b/src/shared/util.h
572a44
index 1b845b3..222abe0 100644
572a44
--- a/src/shared/util.h
572a44
+++ b/src/shared/util.h
572a44
@@ -764,3 +764,15 @@ bool id128_is_valid(const char *s) _pure_;
572a44
 void parse_user_at_host(char *arg, char **user, char **host);
572a44
 
572a44
 int split_pair(const char *s, const char *sep, char **l, char **r);
572a44
+
572a44
+/**
572a44
+ * Normal qsort requires base to be nonnull. Here were require
572a44
+ * that only if nmemb > 0.
572a44
+ */
572a44
+static inline void qsort_safe(void *base, size_t nmemb, size_t size,
572a44
+                              int (*compar)(const void *, const void *)) {
572a44
+        if (nmemb) {
572a44
+                assert(base);
572a44
+                qsort(base, nmemb, size, compar);
572a44
+        }
572a44
+}
572a44
diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c
572a44
index bb7ada9..0d1a487 100644
572a44
--- a/src/systemctl/systemctl.c
572a44
+++ b/src/systemctl/systemctl.c
572a44
@@ -471,7 +471,7 @@ static int list_units(DBusConnection *bus, char **args) {
572a44
         if (r < 0)
572a44
                 return r;
572a44
 
572a44
-        qsort(unit_infos, c, sizeof(struct unit_info), compare_unit_info);
572a44
+        qsort_safe(unit_infos, c, sizeof(struct unit_info), compare_unit_info);
572a44
 
572a44
         output_units_list(unit_infos, c);
572a44
 
572a44
@@ -733,8 +733,8 @@ static int list_sockets(DBusConnection *bus, char **args) {
572a44
                 listen = triggered = NULL; /* avoid cleanup */
572a44
         }
572a44
 
572a44
-        qsort(socket_infos, cs, sizeof(struct socket_info),
572a44
-              (__compar_fn_t) socket_info_compare);
572a44
+        qsort_safe(socket_infos, cs, sizeof(struct socket_info),
572a44
+                   (__compar_fn_t) socket_info_compare);
572a44
 
572a44
         output_sockets_list(socket_infos, cs);
572a44
 
572a44
@@ -1108,7 +1108,7 @@ static int list_dependencies_one(DBusConnection *bus, const char *name, int leve
572a44
         if (r < 0)
572a44
                 return r;
572a44
 
572a44
-        qsort(deps, strv_length(deps), sizeof (char*), list_dependencies_compare);
572a44
+        qsort_safe(deps, strv_length(deps), sizeof (char*), list_dependencies_compare);
572a44
 
572a44
         STRV_FOREACH(c, deps) {
572a44
                 if (strv_contains(u, *c)) {
572a44
@@ -3532,7 +3532,7 @@ static int show_all(const char* verb,
572a44
         if (r < 0)
572a44
                 return r;
572a44
 
572a44
-        qsort(unit_infos, c, sizeof(struct unit_info), compare_unit_info);
572a44
+        qsort_safe(unit_infos, c, sizeof(struct unit_info), compare_unit_info);
572a44
 
572a44
         for (u = unit_infos; u < unit_infos + c; u++) {
572a44
                 _cleanup_free_ char *p = NULL;