]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
libkmod: swap alloca usage for a few assert_cc
authorEmil Velikov <emil.l.velikov@gmail.com>
Mon, 12 Feb 2024 17:23:10 +0000 (17:23 +0000)
committerLucas De Marchi <lucas.de.marchi@gmail.com>
Tue, 30 Apr 2024 17:33:52 +0000 (12:33 -0500)
Since all the compression magic is always available now, we don't need
to loop at runtime nor use alloca - latter of which comes with a handful
of caveats.

Simply throw in a few assert_cc(), which will trigger at build-time.

Signed-off-by: Emil Velikov <emil.l.velikov@gmail.com>
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 b69f1ef66a7769a2fed5a8891f4b9af2595af475..5b88d6c9647dbd47cacaab15d680e37a8eadf00d 100644 (file)
@@ -410,7 +410,6 @@ struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx,
 {
        struct kmod_file *file = calloc(1, sizeof(struct kmod_file));
        const struct comp_type *itr;
-       size_t magic_size_max = 0;
        int err = 0;
 
        if (file == NULL)
@@ -422,22 +421,17 @@ struct kmod_file *kmod_file_open(const struct kmod_ctx *ctx,
                goto error;
        }
 
-       for (itr = comp_types; itr->load != NULL; itr++) {
-               if (magic_size_max < itr->magic_size)
-                       magic_size_max = itr->magic_size;
-       }
-
-       if (magic_size_max > 0) {
-               char *buf = alloca(magic_size_max + 1);
+       {
+               char buf[7];
                ssize_t sz;
 
-               if (buf == NULL) {
-                       err = -errno;
-                       goto error;
-               }
-               sz = read_str_safe(file->fd, buf, magic_size_max + 1);
+               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 != (ssize_t)magic_size_max) {
+               if (sz != (sizeof(buf) - 1)) {
                        if (sz < 0)
                                err = sz;
                        else