]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
Add TAKE_PTR()
authorLucas De Marchi <lucas.de.marchi@gmail.com>
Sun, 1 Dec 2024 00:49:00 +0000 (18:49 -0600)
committerLucas De Marchi <lucas.de.marchi@gmail.com>
Fri, 6 Dec 2024 20:59:10 +0000 (12:59 -0800)
Similar to macro in systemd codebase: add a macro that documents we are
"leaking" a pointer that would otherwise be cleaned up by the cleanup
attribute.

Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/264
libkmod/libkmod-config.c
libkmod/libkmod-file-zlib.c
libkmod/libkmod-module.c
shared/util.c
shared/util.h

index 648656e1e0ee6220ba85b12bf5254b46f3b4840b..77b93929881d2efd8efcd605999c0b0ed4afa2a5 100644 (file)
@@ -146,8 +146,9 @@ static int kmod_config_add_command(struct kmod_config *config, const char *modna
        if (!l)
                return -ENOMEM;
 
+       TAKE_PTR(cmd);
        *list = l;
-       cmd = NULL;
+
        return 0;
 }
 
@@ -175,8 +176,9 @@ static int kmod_config_add_options(struct kmod_config *config, const char *modna
        if (!list)
                return -ENOMEM;
 
-       opt = NULL;
+       TAKE_PTR(opt);
        config->options = list;
+
        return 0;
 }
 
@@ -202,8 +204,9 @@ static int kmod_config_add_alias(struct kmod_config *config, const char *name,
        if (!list)
                return -ENOMEM;
 
-       alias = NULL;
+       TAKE_PTR(alias);
        config->aliases = list;
+
        return 0;
 }
 
@@ -222,8 +225,9 @@ static int kmod_config_add_blacklist(struct kmod_config *config, const char *mod
        if (!list)
                return -ENOMEM;
 
-       p = NULL;
+       TAKE_PTR(p);
        config->blacklists = list;
+
        return 0;
 }
 
index 54b39a8eb28238cb58bb49bedcf4754d3e915021..478a8c80409c88e6f55391ff21c606ca91a26f9e 100644 (file)
@@ -22,9 +22,9 @@
 
 int kmod_file_load_zlib(struct kmod_file *file)
 {
+       _cleanup_free_ unsigned char *p = NULL;
        int err = 0;
        off_t did = 0, total = 0;
-       _cleanup_free_ unsigned char *p = NULL;
        gzFile gzf;
        int gzfd;
 
@@ -68,10 +68,10 @@ int kmod_file_load_zlib(struct kmod_file *file)
                did += r;
        }
 
-       file->memory = p;
+       file->memory = TAKE_PTR(p);
        file->size = did;
-       p = NULL;
        gzclose(gzf);
+
        return 0;
 
 error:
index b989aa8b2238bd1c049c316d36a49576368176ec..0d74100cb74cde86dd844228dd8cebc5522634d5 100644 (file)
@@ -648,9 +648,9 @@ static int do_finit_module(struct kmod_module *mod, unsigned int flags, const ch
 
 static int do_init_module(struct kmod_module *mod, unsigned int flags, const char *args)
 {
+       _cleanup_free_ const void *stripped = NULL;
        struct kmod_elf *elf;
        const void *mem;
-       _cleanup_free_ const void *stripped = NULL;
        off_t size;
        int err;
 
index 5d05c0144a3ec98d3455d39eb72df913a2ded3eb..81026ca33fb803bae23409df5d5eb86f262b04fb 100644 (file)
@@ -332,11 +332,12 @@ char *freadline_wrapped(FILE *fp, unsigned int *linenum)
                        n++;
 
                        {
-                               char *ret = buf;
+                               char *ret = TAKE_PTR(buf);
+
                                ret[i] = '\0';
-                               buf = NULL;
                                if (linenum)
                                        *linenum += n;
+
                                return ret;
                        }
 
@@ -390,7 +391,8 @@ char *path_make_absolute_cwd(const char *p)
        if (r == NULL)
                return NULL;
 
-       cwd = NULL;
+       TAKE_PTR(cwd);
+
        r[cwdlen] = '/';
        memcpy(&r[cwdlen + 1], p, plen + 1);
 
index 58431baa11c4e7cbd4f7083720fbc6d400910abf..7c8bb95b4acbfcdac8f880c4c8b68af123d9f1bb 100644 (file)
@@ -158,3 +158,10 @@ static inline bool umulsz_overflow(size_t a, size_t b, size_t *res)
 #error "Unknown sizeof(size_t)"
 #endif
 }
+
+#define TAKE_PTR(x)                \
+       ({                         \
+               typeof(x) x__ = x; \
+               (x) = NULL;        \
+               x__;               \
+       })