From 4dd1086805e98f3b4808405631aab08045e628a8 Mon Sep 17 00:00:00 2001 From: Alasdair Kergon Date: Wed, 10 May 2006 17:49:25 +0000 Subject: [PATCH] more coverity fixes --- lib/cache/lvmcache.c | 5 ++++- lib/commands/toolcontext.c | 2 +- lib/config/config.c | 10 ++++++++-- lib/format1/import-export.c | 14 ++++++-------- lib/format_text/import.c | 8 +++----- lib/locking/cluster_locking.c | 3 ++- lib/locking/file_locking.c | 4 +++- lib/metadata/metadata.c | 5 ++++- tools/reporter.c | 7 +++++-- tools/toollib.c | 12 ++++++++++-- tools/vgcfgbackup.c | 6 +++++- 11 files changed, 51 insertions(+), 25 deletions(-) diff --git a/lib/cache/lvmcache.c b/lib/cache/lvmcache.c index f3bc24fdc..61209ede1 100644 --- a/lib/cache/lvmcache.c +++ b/lib/cache/lvmcache.c @@ -123,7 +123,10 @@ const struct format_type *fmt_from_vgname(const char *vgname, const char *vgid) * we check cached labels here. Unfortunately vginfo is volatile. */ list_init(&devs); list_iterate_items(info, &vginfo->infos) { - devl = dm_malloc(sizeof(*devl)); + if (!(devl = dm_malloc(sizeof(*devl)))) { + log_error("device_list element allocation failed"); + return NULL; + } devl->dev = info->dev; list_add(&devs, &devl->list); } diff --git a/lib/commands/toolcontext.c b/lib/commands/toolcontext.c index 1932b412c..ad84a1d18 100644 --- a/lib/commands/toolcontext.c +++ b/lib/commands/toolcontext.c @@ -952,7 +952,7 @@ struct cmd_context *create_toolcontext(struct arg *the_args) if (!(cmd->mem = dm_pool_create("command", 4 * 1024))) { log_error("Command memory pool creation failed"); - return 0; + goto error; } memlock_init(cmd); diff --git a/lib/config/config.c b/lib/config/config.c index 43226d018..33ba929ff 100644 --- a/lib/config/config.c +++ b/lib/config/config.c @@ -688,14 +688,20 @@ static void _eat_space(struct parser *p) static struct config_value *_create_value(struct parser *p) { struct config_value *v = dm_pool_alloc(p->mem, sizeof(*v)); - memset(v, 0, sizeof(*v)); + + if (v) + memset(v, 0, sizeof(*v)); + return v; } static struct config_node *_create_node(struct parser *p) { struct config_node *n = dm_pool_alloc(p->mem, sizeof(*n)); - memset(n, 0, sizeof(*n)); + + if (n) + memset(n, 0, sizeof(*n)); + return n; } diff --git a/lib/format1/import-export.c b/lib/format1/import-export.c index a9da3815d..abb5e751e 100644 --- a/lib/format1/import-export.c +++ b/lib/format1/import-export.c @@ -124,15 +124,13 @@ int export_pv(struct cmd_context *cmd, struct dm_pool *mem, memcpy(pvd->pv_uuid, pv->id.uuid, ID_LEN); - if (!_check_vg_name(pv->vg_name)) { - stack; - return 0; - } - - memset(pvd->vg_name, 0, sizeof(pvd->vg_name)); - - if (pv->vg_name) + if (pv->vg_name) { + if (!_check_vg_name(pv->vg_name)) { + stack; + return 0; + } strncpy((char *)pvd->vg_name, pv->vg_name, sizeof(pvd->vg_name)); + } /* Preserve existing system_id if it exists */ if (vg && *vg->system_id) diff --git a/lib/format_text/import.c b/lib/format_text/import.c index b5f3d9c02..c2ac3f1da 100644 --- a/lib/format_text/import.c +++ b/lib/format_text/import.c @@ -44,7 +44,7 @@ const char *text_vgname_import(const struct format_type *fmt, } if (!(cft = create_config_tree(NULL))) - goto_out; + return_NULL; if ((!dev && !read_config_file(cft)) || (dev && !read_config_fd(cft, dev, offset, size, @@ -94,10 +94,8 @@ struct volume_group *text_vg_import_fd(struct format_instance *fid, *desc = NULL; *when = 0; - if (!(cft = create_config_tree(file))) { - stack; - goto out; - } + if (!(cft = create_config_tree(file))) + return_NULL if ((!dev && !read_config_file(cft)) || (dev && !read_config_fd(cft, dev, offset, size, diff --git a/lib/locking/cluster_locking.c b/lib/locking/cluster_locking.c index 15271b937..6781c9f9a 100644 --- a/lib/locking/cluster_locking.c +++ b/lib/locking/cluster_locking.c @@ -386,11 +386,12 @@ int lock_resource(struct cmd_context *cmd, const char *resource, int flags) int cluster_cmd = 0; assert(strlen(resource) < sizeof(lockname)); + assert(resource); switch (flags & LCK_SCOPE_MASK) { case LCK_VG: /* If the VG name is empty then lock the unused PVs */ - if (!resource || !*resource) + if (!*resource) lvm_snprintf(lockname, sizeof(lockname), "P_orphans"); else lvm_snprintf(lockname, sizeof(lockname), "V_%s", diff --git a/lib/locking/file_locking.c b/lib/locking/file_locking.c index c2c47ec1a..41b90f08e 100644 --- a/lib/locking/file_locking.c +++ b/lib/locking/file_locking.c @@ -207,9 +207,11 @@ static int _file_lock_resource(struct cmd_context *cmd, const char *resource, { char lockfile[PATH_MAX]; + assert(resource); + switch (flags & LCK_SCOPE_MASK) { case LCK_VG: - if (!resource || !*resource) + if (!*resource) lvm_snprintf(lockfile, sizeof(lockfile), "%s/P_orphans", _lock_dir); else diff --git a/lib/metadata/metadata.c b/lib/metadata/metadata.c index 8a4b9b8b7..cbd303118 100644 --- a/lib/metadata/metadata.c +++ b/lib/metadata/metadata.c @@ -266,7 +266,10 @@ struct volume_group *vg_create(struct cmd_context *cmd, const char *vg_name, vg->seqno = 0; vg->status = (RESIZEABLE_VG | LVM_READ | LVM_WRITE); - vg->system_id = dm_pool_alloc(mem, NAME_LEN); + if (!(vg->system_id = dm_pool_alloc(mem, NAME_LEN))) { + stack; + goto bad; + } *vg->system_id = '\0'; vg->extent_size = extent_size; diff --git a/tools/reporter.c b/tools/reporter.c index 4991f5a22..9d734440d 100644 --- a/tools/reporter.c +++ b/tools/reporter.c @@ -214,8 +214,11 @@ static int _report(struct cmd_context *cmd, int argc, char **argv, return 0; } if (*opts == '+') { - str = dm_pool_alloc(cmd->mem, - strlen(options) + strlen(opts) + 1); + if (!(str = dm_pool_alloc(cmd->mem, + strlen(options) + strlen(opts) + 1))) { + log_error("options string allocation failed"); + return 0; + } strcpy(str, options); strcat(str, ","); strcat(str, opts + 1); diff --git a/tools/toollib.c b/tools/toollib.c index ab81e3779..7afbd0c4b 100644 --- a/tools/toollib.c +++ b/tools/toollib.c @@ -405,7 +405,11 @@ static int _process_one_vg(struct cmd_context *cmd, const char *vg_name, } log_verbose("Finding volume group \"%s\"", vg_name); - vg = vg_read(cmd, vg_name, vgid, &consistent); + if (!(vg = vg_read(cmd, vg_name, vgid, &consistent))) { + log_error("Volume group \"%s\" not found", vg_name); + unlock_vg(cmd, vg_name); + return ret_max; + } if (!list_empty(tags)) { /* Only process if a tag matches or it's on arg_vgnames */ @@ -649,7 +653,11 @@ int process_each_pv(struct cmd_context *cmd, int argc, char **argv, if (!list_empty(&tags) && (vgnames = get_vgs(cmd, 0)) && !list_empty(vgnames)) { list_iterate_items(sll, vgnames) { - vg = vg_read(cmd, sll->str, NULL, &consistent); + if (!(vg = vg_read(cmd, sll->str, NULL, &consistent))) { + log_error("Volume group \"%s\" not found", sll->str); + ret_max = ECMD_FAILED; + continue; + } if (!consistent) continue; ret = process_each_pv_in_vg(cmd, vg, &tags, diff --git a/tools/vgcfgbackup.c b/tools/vgcfgbackup.c index ae15dbe71..b1af733ca 100644 --- a/tools/vgcfgbackup.c +++ b/tools/vgcfgbackup.c @@ -23,7 +23,11 @@ static char *_expand_filename(const char *template, const char *vg_name, if (security_level()) return dm_strdup(template); - filename = dm_malloc(PATH_MAX); + if (!(filename = dm_malloc(PATH_MAX))) { + log_error("Failed to allocate filename."); + return NULL; + } + if (snprintf(filename, PATH_MAX, template, vg_name) < 0) { log_error("Error processing filename template %s", template);