mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2] virtio_blk: fix snprintf truncation compiler warning
@ 2023-12-05 14:18 Stefan Hajnoczi
  2023-12-07  6:28 ` Chaitanya Kulkarni
  0 siblings, 1 reply; 2+ messages in thread
From: Stefan Hajnoczi @ 2023-12-05 14:18 UTC (permalink / raw)
  To: Michael S. Tsirkin
  Cc: David.Laight, Xuan Zhuo, linux-block, Paolo Bonzini, Jens Axboe,
	Jason Wang, virtualization, linux-kernel, Stefan Hajnoczi,
	Suwan Kim, kernel test robot

Commit 4e0400525691 ("virtio-blk: support polling I/O") triggers the
following gcc 13 W=1 warnings:

drivers/block/virtio_blk.c: In function ‘init_vq’:
drivers/block/virtio_blk.c:1077:68: warning: ‘%d’ directive output may be truncated writing between 1 and 11 bytes into a region of size 7 [-Wformat-truncation=]
 1077 |                 snprintf(vblk->vqs[i].name, VQ_NAME_LEN, "req_poll.%d", i);
      |                                                                    ^~
drivers/block/virtio_blk.c:1077:58: note: directive argument in the range [-2147483648, 65534]
 1077 |                 snprintf(vblk->vqs[i].name, VQ_NAME_LEN, "req_poll.%d", i);
      |                                                          ^~~~~~~~~~~~~
drivers/block/virtio_blk.c:1077:17: note: ‘snprintf’ output between 11 and 21 bytes into a destination of size 16
 1077 |                 snprintf(vblk->vqs[i].name, VQ_NAME_LEN, "req_poll.%d", i);
      |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

This is a false positive because the lower bound -2147483648 is
incorrect. The true range of i is [0, num_vqs - 1] where 0 < num_vqs <
65536.

The code mixes int, unsigned short, and unsigned int types in addition
to using "%d" for an unsigned value. The only place where a 16-bit value
is needed is during the config space access. Use unsigned int and "%u"
consistently to solve the compiler warning and clean up the code.

Cc: Suwan Kim <suwan.kim027@gmail.com>
Reported-by: kernel test robot <lkp@intel.com>
Closes: https://lore.kernel.org/oe-kbuild-all/202312041509.DIyvEt9h-lkp@intel.com/
Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
---
 drivers/block/virtio_blk.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

v2:
- Use unsigned int instead of unsigned short [David]

diff --git a/drivers/block/virtio_blk.c b/drivers/block/virtio_blk.c
index d53d6aa8ee69..dc69a40fbf08 100644
--- a/drivers/block/virtio_blk.c
+++ b/drivers/block/virtio_blk.c
@@ -1019,20 +1019,23 @@ static void virtblk_config_changed(struct virtio_device *vdev)
 static int init_vq(struct virtio_blk *vblk)
 {
 	int err;
-	int i;
+	unsigned int i;
 	vq_callback_t **callbacks;
 	const char **names;
 	struct virtqueue **vqs;
-	unsigned short num_vqs;
+	unsigned int num_vqs;
 	unsigned int num_poll_vqs;
 	struct virtio_device *vdev = vblk->vdev;
 	struct irq_affinity desc = { 0, };
+	u16 num_vqs_u16;
 
 	err = virtio_cread_feature(vdev, VIRTIO_BLK_F_MQ,
 				   struct virtio_blk_config, num_queues,
-				   &num_vqs);
+				   &num_vqs_u16);
 	if (err)
 		num_vqs = 1;
+	else
+		num_vqs = num_vqs_u16;
 
 	if (!err && !num_vqs) {
 		dev_err(&vdev->dev, "MQ advertised but zero queues reported\n");
@@ -1068,13 +1071,13 @@ static int init_vq(struct virtio_blk *vblk)
 
 	for (i = 0; i < num_vqs - num_poll_vqs; i++) {
 		callbacks[i] = virtblk_done;
-		snprintf(vblk->vqs[i].name, VQ_NAME_LEN, "req.%d", i);
+		snprintf(vblk->vqs[i].name, VQ_NAME_LEN, "req.%u", i);
 		names[i] = vblk->vqs[i].name;
 	}
 
 	for (; i < num_vqs; i++) {
 		callbacks[i] = NULL;
-		snprintf(vblk->vqs[i].name, VQ_NAME_LEN, "req_poll.%d", i);
+		snprintf(vblk->vqs[i].name, VQ_NAME_LEN, "req_poll.%u", i);
 		names[i] = vblk->vqs[i].name;
 	}
 
-- 
2.43.0


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH v2] virtio_blk: fix snprintf truncation compiler warning
  2023-12-05 14:18 [PATCH v2] virtio_blk: fix snprintf truncation compiler warning Stefan Hajnoczi
@ 2023-12-07  6:28 ` Chaitanya Kulkarni
  0 siblings, 0 replies; 2+ messages in thread
From: Chaitanya Kulkarni @ 2023-12-07  6:28 UTC (permalink / raw)
  To: Stefan Hajnoczi, Michael S. Tsirkin
  Cc: David.Laight, Xuan Zhuo, linux-block, Paolo Bonzini, Jens Axboe,
	Jason Wang, virtualization, linux-kernel, Suwan Kim,
	kernel test robot

On 12/5/23 06:18, Stefan Hajnoczi wrote:
> Commit 4e0400525691 ("virtio-blk: support polling I/O") triggers the
> following gcc 13 W=1 warnings:
>
> drivers/block/virtio_blk.c: In function ‘init_vq’:
> drivers/block/virtio_blk.c:1077:68: warning: ‘%d’ directive output may be truncated writing between 1 and 11 bytes into a region of size 7 [-Wformat-truncation=]
>   1077 |                 snprintf(vblk->vqs[i].name, VQ_NAME_LEN, "req_poll.%d", i);
>        |                                                                    ^~
> drivers/block/virtio_blk.c:1077:58: note: directive argument in the range [-2147483648, 65534]
>   1077 |                 snprintf(vblk->vqs[i].name, VQ_NAME_LEN, "req_poll.%d", i);
>        |                                                          ^~~~~~~~~~~~~
> drivers/block/virtio_blk.c:1077:17: note: ‘snprintf’ output between 11 and 21 bytes into a destination of size 16
>   1077 |                 snprintf(vblk->vqs[i].name, VQ_NAME_LEN, "req_poll.%d", i);
>        |                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
> This is a false positive because the lower bound -2147483648 is
> incorrect. The true range of i is [0, num_vqs - 1] where 0 < num_vqs <
> 65536.
>
> The code mixes int, unsigned short, and unsigned int types in addition
> to using "%d" for an unsigned value. The only place where a 16-bit value
> is needed is during the config space access. Use unsigned int and "%u"
> consistently to solve the compiler warning and clean up the code.
>
> Cc: Suwan Kim <suwan.kim027@gmail.com>
> Reported-by: kernel test robot <lkp@intel.com>
> Closes: https://lore.kernel.org/oe-kbuild-all/202312041509.DIyvEt9h-lkp@intel.com/
> Signed-off-by: Stefan Hajnoczi <stefanha@redhat.com>
> ---
>   drivers/block/virtio_blk.c | 13 ++++++++-----
>   1 file changed, 8 insertions(+), 5 deletions(-)
>
> v2:
> - Use unsigned int instead of unsigned short [David]
>
>

Looks good.

Reviewed-by: Chaitanya Kulkarni <kch@nvidia.com>

-ck



^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-12-07  6:28 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-12-05 14:18 [PATCH v2] virtio_blk: fix snprintf truncation compiler warning Stefan Hajnoczi
2023-12-07  6:28 ` Chaitanya Kulkarni

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®