btrfs-progs: copy entire label buffer to target buffers

The label is of a fixed size 256 bytes and expects the zero terminator.
Using __strncpy_null is correct as it makes sure there's always the zero
termination but the argument passed in skips the last character.

Signed-off-by: David Sterba <dsterba@suse.com>
This commit is contained in:
David Sterba 2024-06-17 20:28:13 +02:00 committed by Qu Wenruo
parent 9166b470fc
commit 84d88689c1
4 changed files with 8 additions and 8 deletions

View file

@ -102,7 +102,7 @@ static int set_label_unmounted(const char *dev, const char *label)
error_msg(ERROR_MSG_START_TRANS, "set label");
return PTR_ERR(trans);
}
__strncpy_null(root->fs_info->super_copy->label, label, BTRFS_LABEL_SIZE - 1);
__strncpy_null(root->fs_info->super_copy->label, label, BTRFS_LABEL_SIZE);
btrfs_commit_transaction(trans, root);
@ -123,7 +123,7 @@ static int set_label_mounted(const char *mount_path, const char *labelp)
}
memset(label, 0, sizeof(label));
__strncpy_null(label, labelp, BTRFS_LABEL_SIZE - 1);
__strncpy_null(label, labelp, BTRFS_LABEL_SIZE);
if (ioctl(fd, BTRFS_IOC_SET_FSLABEL, label) < 0) {
error("unable to set label of %s: %m", mount_path);
close(fd);
@ -153,7 +153,7 @@ int get_label_unmounted(const char *dev, char *label)
return -1;
__strncpy_null(label, root->fs_info->super_copy->label,
BTRFS_LABEL_SIZE - 1);
BTRFS_LABEL_SIZE);
/* Now we close it since we are done. */
close_ctree(root);
@ -187,7 +187,7 @@ int get_label_mounted(const char *mount_path, char *labelp)
return ret;
}
__strncpy_null(labelp, label, BTRFS_LABEL_SIZE - 1);
__strncpy_null(labelp, label, BTRFS_LABEL_SIZE);
close(fd);
return 0;
}

View file

@ -148,7 +148,7 @@ static int setup_temp_super(int fd, struct btrfs_mkfs_config *cfg,
btrfs_set_super_cache_generation(&super, -1);
btrfs_set_super_incompat_flags(&super, cfg->features.incompat_flags);
if (cfg->label)
__strncpy_null(super.label, cfg->label, BTRFS_LABEL_SIZE - 1);
__strncpy_null(super.label, cfg->label, BTRFS_LABEL_SIZE);
/* Sys chunk array will be re-initialized at chunk tree init time */
super.sys_chunk_array_size = 0;

View file

@ -1323,7 +1323,7 @@ static int do_convert(const char *devname, u32 convert_flags, u32 nodesize,
memset(root->fs_info->super_copy->label, 0, BTRFS_LABEL_SIZE);
if (convert_flags & CONVERT_FLAG_COPY_LABEL) {
__strncpy_null(root->fs_info->super_copy->label,
cctx.label, BTRFS_LABEL_SIZE - 1);
cctx.label, BTRFS_LABEL_SIZE);
printf("Copy label '%s'\n", root->fs_info->super_copy->label);
} else if (convert_flags & CONVERT_FLAG_SET_LABEL) {
strcpy(root->fs_info->super_copy->label, fslabel);
@ -1938,7 +1938,7 @@ int BOX_MAIN(convert)(int argc, char *argv[])
"label too long, trimmed to %d bytes",
BTRFS_LABEL_SIZE - 1);
}
__strncpy_null(fslabel, optarg, BTRFS_LABEL_SIZE - 1);
__strncpy_null(fslabel, optarg, BTRFS_LABEL_SIZE);
break;
case 'L':
copylabel = CONVERT_FLAG_COPY_LABEL;

View file

@ -476,7 +476,7 @@ int make_btrfs(int fd, struct btrfs_mkfs_config *cfg)
btrfs_set_super_nr_global_roots(&super, 1);
if (cfg->label)
__strncpy_null(super.label, cfg->label, BTRFS_LABEL_SIZE - 1);
__strncpy_null(super.label, cfg->label, BTRFS_LABEL_SIZE);
/* create the tree of root objects */
memset(buf->data, 0, cfg->nodesize);