From a426f18baf519f6694ef923bd7d046041d190dee Mon Sep 17 00:00:00 2001 From: David Sterba Date: Wed, 2 Sep 2015 18:02:23 +0200 Subject: [PATCH] btrfs-progs: convert: rework checks of label size Coverity complains that the fslabel might be longer than the superblock buffer, down in do_convert. The label is at most 255 bytes, terminated by zero. Use buffers of the right size. Resolves-coverity-id: 1320911 Signed-off-by: David Sterba --- btrfs-convert.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/btrfs-convert.c b/btrfs-convert.c index 2e6f4d4..f4fc650 100644 --- a/btrfs-convert.c +++ b/btrfs-convert.c @@ -2868,7 +2868,7 @@ int main(int argc, char *argv[]) int usage_error = 0; int progress = 1; char *file; - char fslabel[BTRFS_LABEL_SIZE + 1]; + char fslabel[BTRFS_LABEL_SIZE]; u64 features = BTRFS_MKFS_DEFAULT_FEATURES; while(1) { @@ -2910,14 +2910,13 @@ int main(int argc, char *argv[]) break; case 'l': copylabel = -1; - fslabel[BTRFS_LABEL_SIZE] = 0; - strncpy(fslabel, optarg, sizeof(fslabel)); - if (fslabel[BTRFS_LABEL_SIZE]) { + if (strlen(optarg) >= BTRFS_LABEL_SIZE) { fprintf(stderr, - "warning: label too long, trimmed to %d bytes\n", - BTRFS_LABEL_SIZE); - fslabel[BTRFS_LABEL_SIZE] = 0; + "WARNING: label too long, trimmed to %d bytes\n", + BTRFS_LABEL_SIZE - 1); } + strncpy(fslabel, optarg, BTRFS_LABEL_SIZE - 1); + fslabel[BTRFS_LABEL_SIZE - 1] = 0; break; case 'L': copylabel = 1; -- 2.7.4