From 818f8e8ad58f51074b356580c18d451f5dea7d5a Mon Sep 17 00:00:00 2001 From: Lucas De Marchi Date: Thu, 15 Dec 2011 13:35:40 -0200 Subject: [PATCH] Add safety NULL checks in exported functions --- libkmod/libkmod-module.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/libkmod/libkmod-module.c b/libkmod/libkmod-module.c index 55ac1d2..79a49c3 100644 --- a/libkmod/libkmod-module.c +++ b/libkmod/libkmod-module.c @@ -221,7 +221,7 @@ KMOD_EXPORT int kmod_module_new_from_name(struct kmod_ctx *ctx, char name_norm[NAME_MAX]; char *namesep; - if (ctx == NULL || name == NULL) + if (ctx == NULL || name == NULL || mod == NULL) return -ENOENT; alias_normalize(name, name_norm, &namelen); @@ -317,7 +317,7 @@ KMOD_EXPORT int kmod_module_new_from_path(struct kmod_ctx *ctx, char *abspath; size_t namelen; - if (ctx == NULL || path == NULL) + if (ctx == NULL || path == NULL || mod == NULL) return -ENOENT; abspath = path_make_absolute_cwd(path); @@ -599,6 +599,9 @@ KMOD_EXPORT struct kmod_module *kmod_module_get_module(const struct kmod_list *e */ KMOD_EXPORT const char *kmod_module_get_name(const struct kmod_module *mod) { + if (mod == NULL) + return NULL; + return mod->name; } @@ -617,6 +620,9 @@ KMOD_EXPORT const char *kmod_module_get_path(const struct kmod_module *mod) { char *line; + if (mod == NULL) + return NULL; + DBG(mod->ctx, "name='%s' path='%s'\n", mod->name, mod->path); if (mod->path != NULL) @@ -1051,6 +1057,9 @@ KMOD_EXPORT int kmod_module_get_initstate(const struct kmod_module *mod) char path[PATH_MAX], buf[32]; int fd, err, pathlen; + if (mod == NULL) + return -ENOENT; + pathlen = snprintf(path, sizeof(path), "/sys/module/%s/initstate", mod->name); fd = open(path, O_RDONLY); @@ -1160,6 +1169,9 @@ KMOD_EXPORT int kmod_module_get_refcnt(const struct kmod_module *mod) long refcnt; int fd, err; + if (mod == NULL) + return -ENOENT; + snprintf(path, sizeof(path), "/sys/module/%s/refcnt", mod->name); fd = open(path, O_RDONLY); if (fd < 0) { @@ -1197,6 +1209,7 @@ KMOD_EXPORT struct kmod_list *kmod_module_get_holders(const struct kmod_module * if (mod == NULL) return NULL; + snprintf(dname, sizeof(dname), "/sys/module/%s/holders", mod->name); d = opendir(dname); -- 2.7.4