mirror of
git://sourceware.org/git/lvm2.git
synced 2024-12-21 13:34:40 +03:00
cov: pvck fix memleak
Fix memory leaks on error paths for allocated path and backup_file name by converting allocation to dm_pool_alloc and also change devicefile structure to contain embeded path as last struct member - so we could allocate only needed string size instead of PATH_MAX from pool. TODO: still to be fixed 'mf' struct.
This commit is contained in:
parent
8ebcbe9ace
commit
01081b2100
23
tools/pvck.c
23
tools/pvck.c
@ -343,14 +343,15 @@ static uint64_t mda2_size_from_offset(struct device *dev, uint64_t mda2_offset)
|
||||
}
|
||||
|
||||
struct devicefile {
|
||||
char path[PATH_MAX];
|
||||
int fd;
|
||||
char path[0];
|
||||
};
|
||||
|
||||
static struct devicefile *get_devicefile(const char *path)
|
||||
static struct devicefile *get_devicefile(struct cmd_context *cmd, const char *path)
|
||||
{
|
||||
struct stat sb;
|
||||
struct devicefile *def;
|
||||
size_t len;
|
||||
|
||||
if (stat(path, &sb))
|
||||
return_NULL;
|
||||
@ -358,18 +359,14 @@ static struct devicefile *get_devicefile(const char *path)
|
||||
if ((sb.st_mode & S_IFMT) != S_IFREG)
|
||||
return_NULL;
|
||||
|
||||
if (!(def = malloc(sizeof(struct devicefile))))
|
||||
len = strlen(path) + 1;
|
||||
if (!(def = dm_pool_alloc(cmd->mem, sizeof(struct devicefile) + len)))
|
||||
return_NULL;
|
||||
|
||||
if (dm_snprintf(def->path, PATH_MAX, "%s", path) < 0) {
|
||||
free(def);
|
||||
return_NULL;
|
||||
}
|
||||
memcpy(def->path, path, len);
|
||||
|
||||
if ((def->fd = open(path, O_RDONLY)) < 0) {
|
||||
free(def);
|
||||
if ((def->fd = open(path, O_RDONLY)) < 0)
|
||||
return_NULL;
|
||||
}
|
||||
|
||||
return def;
|
||||
}
|
||||
@ -1774,8 +1771,7 @@ static int _get_one_setting(struct cmd_context *cmd, struct settings *set, char
|
||||
}
|
||||
|
||||
if (!strncmp(key, "backup_file", strlen("backup_file"))) {
|
||||
free(set->backup_file);
|
||||
if ((set->backup_file = strdup(val)))
|
||||
if ((set->backup_file = dm_pool_strdup(cmd->mem, val)))
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
@ -3063,7 +3059,7 @@ int pvck(struct cmd_context *cmd, int argc, char **argv)
|
||||
return ECMD_FAILED;
|
||||
}
|
||||
if (S_ISREG(sb.st_mode))
|
||||
def = get_devicefile(pv_name);
|
||||
def = get_devicefile(cmd, pv_name);
|
||||
else if (S_ISBLK(sb.st_mode)) {
|
||||
if (!setup_device(cmd, pv_name)) {
|
||||
log_error("Failed to set up device %s.", pv_name);
|
||||
@ -3145,7 +3141,6 @@ int pvck(struct cmd_context *cmd, int argc, char **argv)
|
||||
} else
|
||||
log_error("Unknown dump value.");
|
||||
|
||||
free(def);
|
||||
if (!ret)
|
||||
return ECMD_FAILED;
|
||||
return ECMD_PROCESSED;
|
||||
|
Loading…
Reference in New Issue
Block a user