1
0
mirror of git://sourceware.org/git/lvm2.git synced 2025-01-18 10:04:20 +03:00

cleanup: use NAME_LEN stack buffer

Using NAME_LEN we get at least easy max LV name size validation.
Also code gets more simple.
This commit is contained in:
Zdenek Kabelac 2016-12-11 12:19:38 +01:00
parent 6cd68c55dd
commit dc7d3dd5ed

View File

@ -661,6 +661,7 @@ static int _split_mirror_images(struct logical_volume *lv,
struct dm_list split_images; struct dm_list split_images;
struct lv_list *lvl; struct lv_list *lvl;
struct cmd_context *cmd = lv->vg->cmd; struct cmd_context *cmd = lv->vg->cmd;
char layer_name[NAME_LEN], format[NAME_LEN];
if (!lv_is_mirrored(lv)) { if (!lv_is_mirrored(lv)) {
log_error("Unable to split non-mirrored LV %s.", log_error("Unable to split non-mirrored LV %s.",
@ -728,9 +729,6 @@ static int _split_mirror_images(struct logical_volume *lv,
} }
if (!dm_list_empty(&split_images)) { if (!dm_list_empty(&split_images)) {
size_t len = strlen(new_lv->name) + 32;
char *layer_name, format[len];
/* /*
* A number of images have been split and * A number of images have been split and
* a new mirror layer must be formed * a new mirror layer must be formed
@ -747,23 +745,21 @@ static int _split_mirror_images(struct logical_volume *lv,
dm_list_iterate_items(lvl, &split_images) { dm_list_iterate_items(lvl, &split_images) {
sub_lv = lvl->lv; sub_lv = lvl->lv;
if (dm_snprintf(format, len, "%s_mimage_%%d", if (dm_snprintf(format, sizeof(format), "%s_mimage_%%d",
new_lv->name) < 0) { new_lv->name) < 0) {
log_error("Failed to build new image name for %s.", log_error("Failed to build new image name for %s.",
display_lvname(new_lv)); display_lvname(new_lv));
return 0; return 0;
} }
if (!(layer_name = dm_pool_alloc(lv->vg->vgmem, len))) { if (!generate_lv_name(lv->vg, format, layer_name, sizeof(layer_name))) {
log_error("Unable to allocate memory.");
return 0;
}
if (!generate_lv_name(lv->vg, format, layer_name, len)||
sscanf(layer_name, format, &i) != 1) {
log_error("Failed to generate new image names for %s.", log_error("Failed to generate new image names for %s.",
display_lvname(new_lv)); display_lvname(new_lv));
return 0; return 0;
} }
sub_lv->name = layer_name; if (!(sub_lv->name = dm_pool_strdup(lv->vg->vgmem, layer_name))) {
log_error("Unable to allocate memory.");
return 0;
}
} }
if (!_merge_mirror_images(new_lv, &split_images)) { if (!_merge_mirror_images(new_lv, &split_images)) {
@ -1403,15 +1399,11 @@ static int _create_mimage_lvs(struct alloc_handle *ah,
int log) int log)
{ {
uint32_t m, first_area; uint32_t m, first_area;
char *img_name; char img_name[NAME_LEN];
size_t len;
len = strlen(lv->name) + 32;
img_name = alloca(len);
if (dm_snprintf(img_name, len, "%s_mimage_%%d", lv->name) < 0) { if (dm_snprintf(img_name, sizeof(img_name), "%s_mimage_%%d", lv->name) < 0) {
log_error("img_name allocation failed. " log_error("Failed to build new mirror image name for %s.",
"Remove new LV and retry."); display_lvname(lv));
return 0; return 0;
} }
@ -1784,14 +1776,10 @@ static struct logical_volume *_create_mirror_log(struct logical_volume *lv,
const char *suffix) const char *suffix)
{ {
struct logical_volume *log_lv; struct logical_volume *log_lv;
char *log_name; char log_name[NAME_LEN];
size_t len;
len = strlen(lv_name) + 32; if (dm_snprintf(log_name, sizeof(log_name), "%s%s", lv_name, suffix) < 0) {
log_name = alloca(len); /* alloca never fails */ log_error("Failed to build new mirror log name for %s.", lv_name);
if (dm_snprintf(log_name, len, "%s%s", lv_name, suffix) < 0) {
log_error("log_name allocation failed.");
return NULL; return NULL;
} }