btrfs-progs: convert: rework checks of label size
authorDavid Sterba <dsterba@suse.com>
Wed, 2 Sep 2015 16:02:23 +0000 (18:02 +0200)
committerDavid Sterba <dsterba@suse.com>
Wed, 2 Sep 2015 16:56:54 +0000 (18:56 +0200)
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 <dsterba@suse.com>
btrfs-convert.c

index 2e6f4d4..f4fc650 100644 (file)
@@ -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;