From: Tobias Stoeckmann Date: Fri, 15 Nov 2024 17:43:12 +0000 (+0100) Subject: libkmod: Propagate hash_add errors X-Git-Tag: v34~60 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=808eb4b8e9a17ea5e36d5c2865a3446077d499e8;p=thirdparty%2Fkmod.git libkmod: Propagate hash_add errors 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 Reviewed-by: Emil Velikov Link: https://github.com/kmod-project/kmod/pull/248 Signed-off-by: Lucas De Marchi --- diff --git a/libkmod/libkmod-internal.h b/libkmod/libkmod-internal.h index 31979b5f..4706071d 100644 --- a/libkmod/libkmod-internal.h +++ b/libkmod/libkmod-internal.h @@ -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); diff --git a/libkmod/libkmod-module.c b/libkmod/libkmod-module.c index 17d75cc0..07be771c 100644 --- a/libkmod/libkmod-module.c +++ b/libkmod/libkmod-module.c @@ -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; diff --git a/libkmod/libkmod.c b/libkmod/libkmod.c index f9d63a94..f4510d0e 100644 --- a/libkmod/libkmod.c +++ b/libkmod/libkmod.c @@ -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)