]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
libkmod: Propagate hash_add errors
authorTobias Stoeckmann <tobias@stoeckmann.org>
Fri, 15 Nov 2024 17:43:12 +0000 (18:43 +0100)
committerLucas De Marchi <lucas.de.marchi@gmail.com>
Thu, 21 Nov 2024 04:34:33 +0000 (22:34 -0600)
If newly created module in kmod_module_new could not be added to hash,
return an error. Otherwise it is possible to create multiple independent
structs with the same name, which the hash set is supposed to prevent.

An error only occurs in out of memory conditions.

Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/248
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
libkmod/libkmod-internal.h
libkmod/libkmod-module.c
libkmod/libkmod.c

index 31979b5f0df70df7c81471261ca66d1a0ed43936..4706071d71adb0ea7a4a115249c5aa5c5355e8a5 100644 (file)
@@ -82,7 +82,7 @@ _nonnull_(1) void kmod_set_modules_required(struct kmod_ctx *ctx, bool required)
 _nonnull_all_ char *kmod_search_moddep(struct kmod_ctx *ctx, const char *name);
 
 _nonnull_all_ struct kmod_module *kmod_pool_get_module(struct kmod_ctx *ctx, const char *key);
-_nonnull_all_ void kmod_pool_add_module(struct kmod_ctx *ctx, struct kmod_module *mod, const char *key);
+_nonnull_all_ int kmod_pool_add_module(struct kmod_ctx *ctx, struct kmod_module *mod, const char *key);
 _nonnull_all_ void kmod_pool_del_module(struct kmod_ctx *ctx, struct kmod_module *mod, const char *key);
 
 _nonnull_all_ const struct kmod_config *kmod_get_config(const struct kmod_ctx *ctx);
index 17d75cc09cc291ab873d0e95c6d5f27b9877466d..07be771c4569185a64c5e046a929b4b656c6f3a7 100644 (file)
@@ -237,6 +237,7 @@ static int kmod_module_new(struct kmod_ctx *ctx, const char *key, const char *na
 {
        struct kmod_module *m;
        size_t keylen;
+       int err;
 
        m = kmod_pool_get_module(ctx, key);
        if (m != NULL) {
@@ -269,7 +270,11 @@ static int kmod_module_new(struct kmod_ctx *ctx, const char *key, const char *na
        }
 
        m->refcount = 1;
-       kmod_pool_add_module(ctx, m, m->hashkey);
+       err = kmod_pool_add_module(ctx, m, m->hashkey);
+       if (err < 0) {
+               free(m);
+               return err;
+       }
        *mod = m;
 
        return 0;
index f9d63a94937cdbadbf9535c90f4e1e5c366be7c8..f4510d0e8e0af1f10a4d5a8ac513da72fa8cee09 100644 (file)
@@ -334,11 +334,11 @@ struct kmod_module *kmod_pool_get_module(struct kmod_ctx *ctx, const char *key)
        return mod;
 }
 
-void kmod_pool_add_module(struct kmod_ctx *ctx, struct kmod_module *mod, const char *key)
+int kmod_pool_add_module(struct kmod_ctx *ctx, struct kmod_module *mod, const char *key)
 {
        DBG(ctx, "add %p key='%s'\n", mod, key);
 
-       hash_add(ctx->modules_by_name, key, mod);
+       return hash_add(ctx->modules_by_name, key, mod);
 }
 
 void kmod_pool_del_module(struct kmod_ctx *ctx, struct kmod_module *mod, const char *key)