daandemeyer / rpms / systemd

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