From nobody Sun Dec 28 22:54:18 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2A2D5C4167B for ; Mon, 4 Dec 2023 14:07:56 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344914AbjLDOHr (ORCPT ); Mon, 4 Dec 2023 09:07:47 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51424 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233342AbjLDOHq (ORCPT ); Mon, 4 Dec 2023 09:07:46 -0500 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 08C12D8 for ; Mon, 4 Dec 2023 06:07:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1701698871; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=Cx/FH2glJNRXLr0W/z9kDPTOMXxVSYaSs/fbwHr60CU=; b=GVz6AdDWT8xLhIm+sP2xf3G6udN5KzR3I7WpaUFW08vveq8fo0E1a8wjR+1uMHpu4DFKpf b8wYmsuXz37JkKwW8tkZStZsIF3sUUcx5swlO4FlIv4k1SaDz76w3u1om8XE6fRpywYV6d ybVzqOsYmQ8F7BZda/LZOD/+jtOafcU= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-111-5-9Omnj8Ommtyb9aUDUfLw-1; Mon, 04 Dec 2023 09:07:46 -0500 X-MC-Unique: 5-9Omnj8Ommtyb9aUDUfLw-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 9D5CD862CA7; Mon, 4 Dec 2023 14:07:45 +0000 (UTC) Received: from localhost (unknown [10.39.192.49]) by smtp.corp.redhat.com (Postfix) with ESMTP id E21311C060AE; Mon, 4 Dec 2023 14:07:44 +0000 (UTC) From: Stefan Hajnoczi To: "Michael S. Tsirkin" Cc: Xuan Zhuo , virtualization@lists.linux.dev, Jason Wang , linux-kernel@vger.kernel.org, Jens Axboe , linux-block@vger.kernel.org, Paolo Bonzini , Stefan Hajnoczi , Suwan Kim , kernel test robot Subject: [PATCH] virtio_blk: fix snprintf truncation compiler warning Date: Mon, 4 Dec 2023 09:07:43 -0500 Message-ID: <20231204140743.1487843-1-stefanha@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.7 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Commit 4e0400525691 ("virtio-blk: support polling I/O") triggers the following gcc 13 W=3D1 warnings: drivers/block/virtio_blk.c: In function =E2=80=98init_vq=E2=80=99: drivers/block/virtio_blk.c:1077:68: warning: =E2=80=98%d=E2=80=99 directive= output may be truncated writing between 1 and 11 bytes into a region of si= ze 7 [-Wformat-truncation=3D] 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: =E2=80=98snprintf=E2=80=99 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 Reported-by: kernel test robot Closes: https://lore.kernel.org/oe-kbuild-all/202312041509.DIyvEt9h-lkp@int= el.com/ Signed-off-by: Stefan Hajnoczi Reviewed-by: Chaitanya Kulkarni --- drivers/block/virtio_blk.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/drivers/block/virtio_blk.c b/drivers/block/virtio_blk.c index d53d6aa8ee69..47556d8ccc32 100644 --- a/drivers/block/virtio_blk.c +++ b/drivers/block/virtio_blk.c @@ -1019,12 +1019,12 @@ static void virtblk_config_changed(struct virtio_de= vice *vdev) static int init_vq(struct virtio_blk *vblk) { int err; - int i; + unsigned short i; vq_callback_t **callbacks; const char **names; struct virtqueue **vqs; unsigned short num_vqs; - unsigned int num_poll_vqs; + unsigned short num_poll_vqs; struct virtio_device *vdev =3D vblk->vdev; struct irq_affinity desc =3D { 0, }; =20 @@ -1068,13 +1068,13 @@ static int init_vq(struct virtio_blk *vblk) =20 for (i =3D 0; i < num_vqs - num_poll_vqs; i++) { callbacks[i] =3D 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] =3D vblk->vqs[i].name; } =20 for (; i < num_vqs; i++) { callbacks[i] =3D 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] =3D vblk->vqs[i].name; } =20 --=20 2.43.0