1
0
mirror of git://sourceware.org/git/lvm2.git synced 2025-03-10 16:58:47 +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:
Zdenek Kabelac 2021-07-27 22:52:08 +02:00
parent 8ebcbe9ace
commit 01081b2100

View File

@ -343,14 +343,15 @@ static uint64_t mda2_size_from_offset(struct device *dev, uint64_t mda2_offset)
} }
struct devicefile { struct devicefile {
char path[PATH_MAX];
int fd; 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 stat sb;
struct devicefile *def; struct devicefile *def;
size_t len;
if (stat(path, &sb)) if (stat(path, &sb))
return_NULL; return_NULL;
@ -358,18 +359,14 @@ static struct devicefile *get_devicefile(const char *path)
if ((sb.st_mode & S_IFMT) != S_IFREG) if ((sb.st_mode & S_IFMT) != S_IFREG)
return_NULL; 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; return_NULL;
if (dm_snprintf(def->path, PATH_MAX, "%s", path) < 0) { memcpy(def->path, path, len);
free(def);
return_NULL;
}
if ((def->fd = open(path, O_RDONLY)) < 0) { if ((def->fd = open(path, O_RDONLY)) < 0)
free(def);
return_NULL; return_NULL;
}
return def; 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"))) { if (!strncmp(key, "backup_file", strlen("backup_file"))) {
free(set->backup_file); if ((set->backup_file = dm_pool_strdup(cmd->mem, val)))
if ((set->backup_file = strdup(val)))
return 1; return 1;
return 0; return 0;
} }
@ -3063,7 +3059,7 @@ int pvck(struct cmd_context *cmd, int argc, char **argv)
return ECMD_FAILED; return ECMD_FAILED;
} }
if (S_ISREG(sb.st_mode)) if (S_ISREG(sb.st_mode))
def = get_devicefile(pv_name); def = get_devicefile(cmd, pv_name);
else if (S_ISBLK(sb.st_mode)) { else if (S_ISBLK(sb.st_mode)) {
if (!setup_device(cmd, pv_name)) { if (!setup_device(cmd, pv_name)) {
log_error("Failed to set up device %s.", 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 } else
log_error("Unknown dump value."); log_error("Unknown dump value.");
free(def);
if (!ret) if (!ret)
return ECMD_FAILED; return ECMD_FAILED;
return ECMD_PROCESSED; return ECMD_PROCESSED;