examples/vhost_blk: fix not null-terminated buffer
authorJin Yu <jin.yu@intel.com>
Fri, 15 May 2020 14:45:02 +0000 (22:45 +0800)
committerFerruh Yigit <ferruh.yigit@intel.com>
Mon, 18 May 2020 18:35:57 +0000 (20:35 +0200)
In vhost_blk_bdev_construct: The string buffer may not have
a null terminator if the source string's length is equal to
the buffer size.

Fixes: 91d3e2d42997 ("examples/vhost_blk: refactor to increase readability")

Signed-off-by: Jin Yu <jin.yu@intel.com>
Reviewed-by: Xiaolong Ye <xiaolong.ye@intel.com>
examples/vhost_blk/vhost_blk.c

index 95a0508..f4c5943 100644 (file)
@@ -750,8 +750,9 @@ vhost_blk_bdev_construct(const char *bdev_name,
        if (!bdev)
                return NULL;
 
-       strncpy(bdev->name, bdev_name, sizeof(bdev->name));
-       strncpy(bdev->product_name, bdev_serial, sizeof(bdev->product_name));
+       snprintf(bdev->name, sizeof(bdev->name), "%s", bdev_name);
+       snprintf(bdev->product_name, sizeof(bdev->product_name), "%s",
+                bdev_serial);
        bdev->blocklen = blk_size;
        bdev->blockcnt = blk_cnt;
        bdev->write_cache = wce_enable;