Skip to content

Commit b8e0792

Browse files
stefanhaRHmstsirkin
authored andcommitted
virtio_blk: fix snprintf truncation compiler warning
Commit 4e04005 ("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. Use unsigned short and "%u" consistently to solve the compiler warning. Cc: Suwan Kim <[email protected]> Reported-by: kernel test robot <[email protected]> Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/ Signed-off-by: Stefan Hajnoczi <[email protected]> Message-Id: <[email protected]> Signed-off-by: Michael S. Tsirkin <[email protected]>
1 parent 1f475cd commit b8e0792

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

drivers/block/virtio_blk.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,12 +1019,12 @@ static void virtblk_config_changed(struct virtio_device *vdev)
10191019
static int init_vq(struct virtio_blk *vblk)
10201020
{
10211021
int err;
1022-
int i;
1022+
unsigned short i;
10231023
vq_callback_t **callbacks;
10241024
const char **names;
10251025
struct virtqueue **vqs;
10261026
unsigned short num_vqs;
1027-
unsigned int num_poll_vqs;
1027+
unsigned short num_poll_vqs;
10281028
struct virtio_device *vdev = vblk->vdev;
10291029
struct irq_affinity desc = { 0, };
10301030

@@ -1068,13 +1068,13 @@ static int init_vq(struct virtio_blk *vblk)
10681068

10691069
for (i = 0; i < num_vqs - num_poll_vqs; i++) {
10701070
callbacks[i] = virtblk_done;
1071-
snprintf(vblk->vqs[i].name, VQ_NAME_LEN, "req.%d", i);
1071+
snprintf(vblk->vqs[i].name, VQ_NAME_LEN, "req.%u", i);
10721072
names[i] = vblk->vqs[i].name;
10731073
}
10741074

10751075
for (; i < num_vqs; i++) {
10761076
callbacks[i] = NULL;
1077-
snprintf(vblk->vqs[i].name, VQ_NAME_LEN, "req_poll.%d", i);
1077+
snprintf(vblk->vqs[i].name, VQ_NAME_LEN, "req_poll.%u", i);
10781078
names[i] = vblk->vqs[i].name;
10791079
}
10801080

0 commit comments

Comments
 (0)