chiark / gitweb /
Never call qsort on potentially NULL arrays
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Fri, 11 Oct 2013 23:33:13 +0000 (19:33 -0400)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Sun, 13 Oct 2013 21:56:54 +0000 (17:56 -0400)
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.

17 files changed:
src/analyze/systemd-analyze.c
src/cgtop/cgtop.c
src/core/namespace.c
src/journal/catalog.c
src/journal/journal-file.c
src/journal/journal-vacuum.c
src/journal/journalctl.c
src/libsystemd-bus/bus-match.c
src/libudev/libudev-enumerate.c
src/nss-myhostname/netlink.c
src/readahead/readahead-collect.c
src/shared/cgroup-show.c
src/shared/conf-files.c
src/shared/efivars.c
src/shared/fileio.c
src/shared/util.h
src/systemctl/systemctl.c

index 27d063c548bc4b44dffc064499ceaf632c421592..a4f15eb64652df518af4e64a06fb4f7b1bb903b7 100644 (file)
@@ -768,7 +768,7 @@ static int list_dependencies_one(DBusConnection *bus, const char *name, unsigned
         if (r < 0)
                 return r;
 
-        qsort(deps, strv_length(deps), sizeof (char*), list_dependencies_compare);
+        qsort_safe(deps, strv_length(deps), sizeof (char*), list_dependencies_compare);
 
         r = acquire_boot_times(bus, &boot);
         if (r < 0)
index cacf705a0abbdef61a8148bbba7ec823dea3d594..293a2113bdcbcc9510e34eeb4d0e262b0a6960af 100644 (file)
@@ -461,7 +461,7 @@ static int display(Hashmap *a) {
                 if (g->n_tasks_valid || g->cpu_valid || g->memory_valid || g->io_valid)
                         array[n++] = g;
 
-        qsort(array, n, sizeof(Group*), group_compare);
+        qsort_safe(array, n, sizeof(Group*), group_compare);
 
         /* Find the longest names in one run */
         for (j = 0; j < n; j++) {
index 16b132ba566327a7b4ed2908fba55d3c887cb2db..936f36839b7140cbc4027c8aa3cf045e0f179a0e 100644 (file)
@@ -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)
index 7738d243a58f3eaa575a10a2083d32c96f40385c..90ca00806448b3282df1f6bddf63554524255fc6 100644 (file)
@@ -399,7 +399,7 @@ int catalog_update(const char* database, const char* root, const char* const* di
         }
 
         assert(n == hashmap_size(h));
-        qsort(items, n, sizeof(CatalogItem), catalog_compare_func);
+        qsort_safe(items, n, sizeof(CatalogItem), catalog_compare_func);
 
         r = write_catalog(database, h, sb, items, n);
         if (r < 0)
index 78b937bb85f746553fe62fd00ccee72ffd4c27c7..901e71b6aa9f99b3ae67a1fdef846b9d019e0b30 100644 (file)
@@ -1344,7 +1344,7 @@ int journal_file_append_entry(JournalFile *f, const dual_timestamp *ts, const st
 
         /* Order by the position on disk, in order to improve seek
          * times for rotating media. */
-        qsort(items, n_iovec, sizeof(EntryItem), entry_item_cmp);
+        qsort_safe(items, n_iovec, sizeof(EntryItem), entry_item_cmp);
 
         r = journal_file_append_entry_internal(f, ts, xor_hash, items, n_iovec, seqnum, ret, offset);
 
index 8d5effb45ce63d83046f9917632752b8351f42b9..d4a1c6c9334ff736bbe3066e7d0b317c71119b7b 100644 (file)
@@ -299,8 +299,7 @@ int journal_directory_vacuum(
                 n_list ++;
         }
 
-        if (n_list > 0)
-                qsort(list, n_list, sizeof(struct vacuum_info), vacuum_compare);
+        qsort_safe(list, n_list, sizeof(struct vacuum_info), vacuum_compare);
 
         for (i = 0; i < n_list; i++) {
                 struct statvfs ss;
index 2f8be1b83e9d37b7382551571760583c4d0f50c8..275458c4821aabd30516f785a2f7dc37148c4b97 100644 (file)
@@ -761,7 +761,7 @@ static int get_relative_boot_id(sd_journal *j, sd_id128_t *boot_id, int relative
                 sd_journal_flush_matches(j);
         }
 
-        qsort(all_ids, count, sizeof(boot_id_t), boot_id_cmp);
+        qsort_safe(all_ids, count, sizeof(boot_id_t), boot_id_cmp);
 
         if (sd_id128_equal(*boot_id, SD_ID128_NULL)) {
                 if (relative > (int) count || relative <= -(int)count)
index 1411167a7f4af4eb34368c36e458c639609f840f..916682aa62ba7642a1cd32ad26743ff32bd4cd12 100644 (file)
@@ -768,7 +768,7 @@ int bus_match_parse(
         }
 
         /* Order the whole thing, so that we always generate the same tree */
-        qsort(components, n_components, sizeof(struct bus_match_component), match_component_compare);
+        qsort_safe(components, n_components, sizeof(struct bus_match_component), match_component_compare);
 
         /* Check for duplicates */
         for (i = 0; i+1 < n_components; i++)
index 8146f27e4f5c0c1acd4bf21b1a81a6ab3dd113fe..e71d766c0283a0755031ecec1786c00983e42f97 100644 (file)
@@ -276,7 +276,7 @@ _public_ struct udev_list_entry *udev_enumerate_get_list_entry(struct udev_enume
                 size_t move_later_prefix = 0;
 
                 udev_list_cleanup(&udev_enumerate->devices_list);
-                qsort(udev_enumerate->devices, udev_enumerate->devices_cur, sizeof(struct syspath), syspath_cmp);
+                qsort_safe(udev_enumerate->devices, udev_enumerate->devices_cur, sizeof(struct syspath), syspath_cmp);
 
                 max = udev_enumerate->devices_cur;
                 for (i = 0; i < max; i++) {
index b1ef912c8ac1d268f683a31b919ab270f8477eb2..47a41f55e6b060a8c9894243a9d3955b0cb9771e 100644 (file)
@@ -197,7 +197,8 @@ finish:
                 return r;
         }
 
-        qsort(list, n_list, sizeof(struct address), address_compare);
+        if (n_list)
+                qsort(list, n_list, sizeof(struct address), address_compare);
 
         *_list = list;
         *_n_list = n_list;
index 32888add01be1a84cc3b178689ab141426e6e809..6b748664c310b8a69603d772d5f156638d17bcf0 100644 (file)
@@ -536,8 +536,7 @@ done:
                 HASHMAP_FOREACH_KEY(q, p, files, i)
                         pack_file(pack, p, on_btrfs);
         } else {
-                struct item *ordered, *j;
-                unsigned k, n;
+                unsigned n;
 
                 /* On rotating media, order things by the block
                  * numbers */
@@ -545,25 +544,31 @@ done:
                 log_debug("Ordering...");
 
                 n = hashmap_size(files);
-                if (!(ordered = new(struct item, n))) {
-                        r = log_oom();
-                        goto finish;
-                }
-
-                j = ordered;
-                HASHMAP_FOREACH_KEY(q, p, files, i) {
-                        memcpy(j, q, sizeof(struct item));
-                        j++;
-                }
+                if (n) {
+                        _cleanup_free_ struct item *ordered;
+                        struct item *j;
+                        unsigned k;
+
+                        ordered = new(struct item, n);
+                        if (!ordered) {
+                                r = log_oom();
+                                goto finish;
+                        }
 
-                assert(ordered + n == j);
+                        j = ordered;
+                        HASHMAP_FOREACH_KEY(q, p, files, i) {
+                                memcpy(j, q, sizeof(struct item));
+                                j++;
+                        }
 
-                qsort(ordered, n, sizeof(struct item), qsort_compare);
+                        assert(ordered + n == j);
 
-                for (k = 0; k < n; k++)
-                        pack_file(pack, ordered[k].path, on_btrfs);
+                        qsort(ordered, n, sizeof(struct item), qsort_compare);
 
-                free(ordered);
+                        for (k = 0; k < n; k++)
+                                pack_file(pack, ordered[k].path, on_btrfs);
+                } else
+                        log_warning("No pack files");
         }
 
         log_debug("Finalizing...");
index e971f36190e216300e9641dec89bc95744265473..cc44ab4ea9ca1700135360cede64eb756f925ce9 100644 (file)
@@ -44,6 +44,8 @@ static void show_pid_array(int pids[], unsigned n_pids, const char *prefix, unsi
         unsigned i, m, pid_width;
         pid_t biggest = 0;
 
+        assert(n_pids > 0);
+
         /* Filter duplicates */
         m = 0;
         for (i = 0; i < n_pids; i++) {
index 6d99739353be97b0553059ffb0d205acff25459d..ed4070c662711cb1e3ae7f009b336e3ad9f19b3e 100644 (file)
@@ -127,7 +127,7 @@ static int conf_files_list_strv_internal(char ***strv, const char *suffix, const
                 return -ENOMEM;
         }
 
-        qsort(files, hashmap_size(fh), sizeof(char *), base_cmp);
+        qsort_safe(files, hashmap_size(fh), sizeof(char *), base_cmp);
         *strv = files;
 
         hashmap_free(fh);
index c015b168579ebb25c8eace507c775b572d79a317..f3eb6a6e5d1ca321555820a4141f2a0b7291f377 100644 (file)
@@ -384,8 +384,7 @@ int efi_get_boot_options(uint16_t **options) {
                 list[count ++] = id;
         }
 
-        if (list)
-                qsort(list, count, sizeof(uint16_t), cmp_uint16);
+        qsort_safe(list, count, sizeof(uint16_t), cmp_uint16);
 
         *options = list;
         return count;
index 603a1c7b38755ad35fec95f48915367341776361..733b3203880b83bbd8b555de78f0a7c855bea323 100644 (file)
@@ -662,6 +662,7 @@ int get_status_field(const char *filename, const char *pattern, char **field) {
         int r;
 
         assert(filename);
+        assert(pattern);
         assert(field);
 
         r = read_full_file(filename, &status, NULL);
index 26af5b30af668f3139191e064bcabda2c85f405c..09e556d0114b7da5756823727e767955fa37637a 100644 (file)
@@ -772,3 +772,15 @@ bool id128_is_valid(const char *s) _pure_;
 void parse_user_at_host(char *arg, char **user, char **host);
 
 int split_pair(const char *s, const char *sep, char **l, char **r);
+
+/**
+ * Normal qsort requires base to be nonnull. Here were require
+ * that only if nmemb > 0.
+ */
+static inline void qsort_safe(void *base, size_t nmemb, size_t size,
+                              int (*compar)(const void *, const void *)) {
+        if (nmemb) {
+                assert(base);
+                qsort(base, nmemb, size, compar);
+        }
+}
index d75281f5f658a875b5b0f37196d22afa9f2337d5..036828b5dd438f600cac5bdeda6dabd8986728f3 100644 (file)
@@ -471,7 +471,7 @@ static int list_units(DBusConnection *bus, char **args) {
         if (r < 0)
                 return r;
 
-        qsort(unit_infos, c, sizeof(struct unit_info), compare_unit_info);
+        qsort_safe(unit_infos, c, sizeof(struct unit_info), compare_unit_info);
 
         output_units_list(unit_infos, c);
 
@@ -733,8 +733,8 @@ static int list_sockets(DBusConnection *bus, char **args) {
                 listen = triggered = NULL; /* avoid cleanup */
         }
 
-        qsort(socket_infos, cs, sizeof(struct socket_info),
-              (__compar_fn_t) socket_info_compare);
+        qsort_safe(socket_infos, cs, sizeof(struct socket_info),
+                   (__compar_fn_t) socket_info_compare);
 
         output_sockets_list(socket_infos, cs);
 
@@ -1108,7 +1108,7 @@ static int list_dependencies_one(DBusConnection *bus, const char *name, int leve
         if (r < 0)
                 return r;
 
-        qsort(deps, strv_length(deps), sizeof (char*), list_dependencies_compare);
+        qsort_safe(deps, strv_length(deps), sizeof (char*), list_dependencies_compare);
 
         STRV_FOREACH(c, deps) {
                 if (strv_contains(u, *c)) {
@@ -3532,7 +3532,7 @@ static int show_all(const char* verb,
         if (r < 0)
                 return r;
 
-        qsort(unit_infos, c, sizeof(struct unit_info), compare_unit_info);
+        qsort_safe(unit_infos, c, sizeof(struct unit_info), compare_unit_info);
 
         for (u = unit_infos; u < unit_infos + c; u++) {
                 _cleanup_free_ char *p = NULL;