Skip to contentSkip to conversation

example/vhost_blk: fix buffer not null terminated

PATCH Superseded Jin Yu 3 participants 5 messages May 8, 2020 – May 15, 2020

lore
Share & tools
lore

Jin Yu

author
#0 lore raw

PATCH 1/1 example/vhost_blk: fix buffer not null terminated

Fix the potential bug.

---
 examples/vhost_blk/vhost_blk.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
examples/vhost_blk/vhost_blk.c +3 -2
index f08473f58..b5ce3332a 100644
@@ -757,8 +757,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;
Signature · 1 line
2.17.2
#1 lore raw
Quoted 1 line · Jin Yu

Fix the potential bug.

Could you describe more about the potential bug?

Fixes tag is needed here.

Thanks,
Xiaolong

Quoted 25 lines

---
examples/vhost_blk/vhost_blk.c | 5 +++--

1 file changed, 3 insertions(+), 2 deletions(-)
examples/vhost_blk/vhost_blk.c +3 -2
index f08473f58..b5ce3332a 100644
@@ -757,8 +757,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;
Signature · 1 line
2.17.2
author
#2 lore raw

PATCH v2 1/1 example/vhost_blk: fix buffer not null terminated

In reply to Jin Yu

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")

---
V2 - update the commit message

---
 examples/vhost_blk/vhost_blk.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
examples/vhost_blk/vhost_blk.c +3 -2
index 95a050855..f4c59437a 100644
@@ -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;
Signature · 1 line
2.17.2
Reviewed-by on v2 1/1
#3 lore raw
Quoted 33 lines · Jin Yu

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")

---
V2 - update the commit message
---
examples/vhost_blk/vhost_blk.c | 5 +++--

1 file changed, 3 insertions(+), 2 deletions(-)
examples/vhost_blk/vhost_blk.c +3 -2
index 95a050855..f4c59437a 100644
@@ -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;
Signature · 1 line
2.17.2
#4 lore raw
In reply to Jin Yu
Quoted 7 lines · Jin Yu

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")
Quoted 6 lines

---
V2 - update the commit message

---
 examples/vhost_blk/vhost_blk.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

Applied to dpdk-next-virtio/master.

Thanks,
Maxime