summaryrefslogtreecommitdiff
path: root/src/readahead/readahead-collect.c
diff options
context:
space:
mode:
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2013-10-11 19:33:13 -0400
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2013-10-13 17:56:54 -0400
commit7ff7394d9e4e9189c30fd018235e6b1728c6f2d0 (patch)
tree37a98cdb42d8b62b56aeb6fad7fd00299449d38d /src/readahead/readahead-collect.c
parentfb1316462952d17d6ebf19c3f093b730c13016a7 (diff)
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.
Diffstat (limited to 'src/readahead/readahead-collect.c')
-rw-r--r--src/readahead/readahead-collect.c39
1 files changed, 22 insertions, 17 deletions
diff --git a/src/readahead/readahead-collect.c b/src/readahead/readahead-collect.c
index 32888add01..6b748664c3 100644
--- a/src/readahead/readahead-collect.c
+++ b/src/readahead/readahead-collect.c
@@ -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...");