[v2] example/vhost_blk: fix buffer not null terminated

Message ID 20200515144502.14043-1-jin.yu@intel.com (mailing list archive)
State Accepted, archived
Delegated to: Maxime Coquelin
Headers
Series [v2] example/vhost_blk: fix buffer not null terminated |

Checks

Context Check Description
ci/checkpatch success coding style OK
ci/iol-intel-Performance success Performance Testing PASS
ci/iol-nxp-Performance success Performance Testing PASS
ci/travis-robot success Travis build: passed
ci/iol-mellanox-Performance success Performance Testing PASS
ci/Intel-compilation success Compilation OK
ci/iol-testing fail Testing issues

Commit Message

Jin Yu May 15, 2020, 2:45 p.m. UTC
  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")
Cc: jin.yu@intel.com

Signed-off-by: Jin Yu <jin.yu@intel.com>
---
V2 - update the commit message
---
 examples/vhost_blk/vhost_blk.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
  

Comments

Xiaolong Ye May 15, 2020, 7:02 a.m. UTC | #1
On 05/15, Jin Yu wrote:
>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")
>Cc: jin.yu@intel.com
>
>Signed-off-by: Jin Yu <jin.yu@intel.com>
>---
>V2 - update the commit message
>---
> examples/vhost_blk/vhost_blk.c | 5 +++--
> 1 file changed, 3 insertions(+), 2 deletions(-)
>
>diff --git a/examples/vhost_blk/vhost_blk.c b/examples/vhost_blk/vhost_blk.c
>index 95a050855..f4c59437a 100644
>--- a/examples/vhost_blk/vhost_blk.c
>+++ b/examples/vhost_blk/vhost_blk.c
>@@ -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;
>-- 
>2.17.2
>

Reviewed-by: Xiaolong Ye <xiaolong.ye@intel.com>
  
Maxime Coquelin May 15, 2020, 8:39 a.m. UTC | #2
On 5/15/20 4:45 PM, Jin Yu wrote:
> 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")
> Cc: jin.yu@intel.com
> 
> Signed-off-by: Jin Yu <jin.yu@intel.com>
> ---
> 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
  

Patch

diff --git a/examples/vhost_blk/vhost_blk.c b/examples/vhost_blk/vhost_blk.c
index 95a050855..f4c59437a 100644
--- a/examples/vhost_blk/vhost_blk.c
+++ b/examples/vhost_blk/vhost_blk.c
@@ -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;