]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
libkmod: tidy-up kmod_file_open()
authorEmil Velikov <emil.l.velikov@gmail.com>
Mon, 12 Feb 2024 17:23:11 +0000 (17:23 +0000)
committerLucas De Marchi <lucas.de.marchi@gmail.com>
Tue, 30 Apr 2024 20:33:55 +0000 (15:33 -0500)
This commit cleans up the indentation and the error path of the
function. It bears no functional changes.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
[ Move assert to avoid warning with -Wdeclaration-after-statement ]
Reviewed-by: Lucas De Marchi <lucas.demarchi@intel.com>
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
libkmod/libkmod-file.c

index 5b88d6c9647dbd47cacaab15d680e37a8eadf00d..20f9d62680999fcbbc0f541c96853e0f05e5f7b1 100644 (file)
@@ -408,43 +408,43 @@ struct kmod_elf *kmod_file_get_elf(struct kmod_file *file)
 struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx,
                                                const char *filename)
 {
-       struct kmod_file *file = calloc(1, sizeof(struct kmod_file));
+       struct kmod_file *file;
        const struct comp_type *itr;
-       int err = 0;
+       char buf[7];
+       ssize_t sz;
+
+       assert_cc(sizeof(magic_zstd) < sizeof(buf));
+       assert_cc(sizeof(magic_xz) < sizeof(buf));
+       assert_cc(sizeof(magic_zlib) < sizeof(buf));
 
+       file = calloc(1, sizeof(struct kmod_file));
        if (file == NULL)
                return NULL;
 
        file->fd = open(filename, O_RDONLY|O_CLOEXEC);
        if (file->fd < 0) {
-               err = -errno;
-               goto error;
+               free(file);
+               return NULL;
        }
 
-       {
-               char buf[7];
-               ssize_t sz;
-
-               assert_cc(sizeof(magic_zstd) < sizeof(buf));
-               assert_cc(sizeof(magic_xz) < sizeof(buf));
-               assert_cc(sizeof(magic_zlib) < sizeof(buf));
-
-               sz = read_str_safe(file->fd, buf, sizeof(buf));
-               lseek(file->fd, 0, SEEK_SET);
-               if (sz != (sizeof(buf) - 1)) {
-                       if (sz < 0)
-                               err = sz;
-                       else
-                               err = -EINVAL;
-                       goto error;
-               }
+       sz = read_str_safe(file->fd, buf, sizeof(buf));
+       lseek(file->fd, 0, SEEK_SET);
+       if (sz != (sizeof(buf) - 1)) {
+               if (sz < 0)
+                       errno = -sz;
+               else
+                       errno = EINVAL;
 
-               for (itr = comp_types; itr->load != NULL; itr++) {
-                       if (memcmp(buf, itr->magic_bytes, itr->magic_size) == 0) {
-                               file->load = itr->load;
-                               file->compression = itr->compression;
-                               break;
-                       }
+               close(file->fd);
+               free(file);
+               return NULL;
+       }
+
+       for (itr = comp_types; itr->load != NULL; itr++) {
+               if (memcmp(buf, itr->magic_bytes, itr->magic_size) == 0) {
+                       file->load = itr->load;
+                       file->compression = itr->compression;
+                       break;
                }
        }
 
@@ -455,15 +455,6 @@ struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx,
 
        file->ctx = ctx;
 
-error:
-       if (err < 0) {
-               if (file->fd >= 0)
-                       close(file->fd);
-               free(file);
-               errno = -err;
-               return NULL;
-       }
-
        return file;
 }