]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
bootspec: sprinkle some argument assert()s all over the place
authorLennart Poettering <lennart@poettering.net>
Mon, 11 Dec 2017 21:22:04 +0000 (22:22 +0100)
committerLennart Poettering <lennart@poettering.net>
Mon, 11 Dec 2017 22:19:45 +0000 (23:19 +0100)
The previous commit fixed a NULL parameter issue, let's check for such,
to make it easier to find issues like this.

src/shared/bootspec.c

index acd9711839b9faf5c260d0d03bb413fd4cc98214..c0a10417d8b11905c8b56474bbea41487662b1fb 100644 (file)
@@ -36,8 +36,9 @@
 #include "virt.h"
 
 void boot_entry_free(BootEntry *entry) {
-        free(entry->filename);
+        assert(entry);
 
+        free(entry->filename);
         free(entry->title);
         free(entry->show_title);
         free(entry->version);
@@ -56,6 +57,9 @@ int boot_entry_load(const char *path, BootEntry *entry) {
         _cleanup_(boot_entry_free) BootEntry tmp = {};
         int r;
 
+        assert(path);
+        assert(entry);
+
         f = fopen(path, "re");
         if (!f)
                 return log_error_errno(errno, "Failed to open \"%s\": %m", path);
@@ -123,6 +127,8 @@ int boot_entry_load(const char *path, BootEntry *entry) {
 void boot_config_free(BootConfig *config) {
         unsigned i;
 
+        assert(config);
+
         free(config->default_pattern);
         free(config->timeout);
         free(config->editor);
@@ -140,6 +146,9 @@ int boot_loader_read_conf(const char *path, BootConfig *config) {
         unsigned line = 1;
         int r;
 
+        assert(path);
+        assert(config);
+
         f = fopen(path, "re");
         if (!f)
                 return log_error_errno(errno, "Failed to open \"%s\": %m", path);
@@ -251,14 +260,17 @@ static int boot_entry_compare(const void *a, const void *b) {
         return str_verscmp(aa->filename, bb->filename);
 }
 
-int boot_entries_find(const char *dir, BootEntry **entries, size_t *n_entries) {
+int boot_entries_find(const char *dir, BootEntry **ret_entries, size_t *ret_n_entries) {
         _cleanup_strv_free_ char **files = NULL;
         char **f;
         int r;
-
         BootEntry *array = NULL;
         size_t n_allocated = 0, n = 0;
 
+        assert(dir);
+        assert(ret_entries);
+        assert(ret_n_entries);
+
         r = conf_files_list(&files, ".conf", NULL, 0, dir, NULL);
         if (r < 0)
                 return log_error_errno(r, "Failed to list files in \"%s\": %m", dir);
@@ -276,8 +288,9 @@ int boot_entries_find(const char *dir, BootEntry **entries, size_t *n_entries) {
 
         qsort_safe(array, n, sizeof(BootEntry), boot_entry_compare);
 
-        *entries = array;
-        *n_entries = n;
+        *ret_entries = array;
+        *ret_n_entries = n;
+
         return 0;
 }
 
@@ -285,6 +298,9 @@ static bool find_nonunique(BootEntry *entries, size_t n_entries, bool *arr) {
         unsigned i, j;
         bool non_unique = false;
 
+        assert(entries || n_entries == 0);
+        assert(arr || n_entries == 0);
+
         for (i = 0; i < n_entries; i++)
                 arr[i] = false;
 
@@ -303,6 +319,8 @@ static int boot_entries_uniquify(BootEntry *entries, size_t n_entries) {
         int r;
         bool arr[n_entries];
 
+        assert(entries || n_entries == 0);
+
         /* Find _all_ non-unique titles */
         if (!find_nonunique(entries, n_entries, arr))
                 return 0;
@@ -349,6 +367,8 @@ static int boot_entries_uniquify(BootEntry *entries, size_t n_entries) {
 static int boot_entries_select_default(const BootConfig *config) {
         int i;
 
+        assert(config);
+
         if (config->entry_oneshot)
                 for (i = config->n_entries - 1; i >= 0; i--)
                         if (streq(config->entry_oneshot, config->entries[i].filename)) {
@@ -377,6 +397,7 @@ static int boot_entries_select_default(const BootConfig *config) {
                 log_debug("Found default: last entry \"%s\"", config->entries[config->n_entries - 1].filename);
         else
                 log_debug("Found no default boot entry :(");
+
         return config->n_entries - 1; /* -1 means "no default" */
 }
 
@@ -384,6 +405,9 @@ int boot_entries_load_config(const char *esp_path, BootConfig *config) {
         const char *p;
         int r;
 
+        assert(esp_path);
+        assert(config);
+
         p = strjoina(esp_path, "/loader/loader.conf");
         r = boot_loader_read_conf(p, config);
         if (r < 0)