From: Arnd Bergmann <arnd@kernel.org>
To: linux-kernel@vger.kernel.org,
"James E.J. Bottomley" <jejb@linux.ibm.com>,
"Martin K. Petersen" <martin.petersen@oracle.com>
Cc: Arnd Bergmann <arnd@arndb.de>, Chris Down <chris@chrisdown.name>,
Petr Mladek <pmladek@suse.com>,
Bart Van Assche <bvanassche@acm.org>,
linux-scsi@vger.kernel.org
Subject: [PATCH 02/11] scsi: devinfo: rework scsi_strcpy_devinfo()
Date: Thu, 28 Mar 2024 15:04:46 +0100 [thread overview]
Message-ID: <20240328140512.4148825-3-arnd@kernel.org> (raw)
In-Reply-To: <20240328140512.4148825-1-arnd@kernel.org>
From: Arnd Bergmann <arnd@arndb.de>
scsi_strcpy_devinfo() appears to work as intended but its semantics are
so confusing that gcc warns about it when -Wstringop-truncation is enabled:
In function 'scsi_strcpy_devinfo',
inlined from 'scsi_dev_info_list_add_keyed' at drivers/scsi/scsi_devinfo.c:370:2:
drivers/scsi/scsi_devinfo.c:297:9: error: 'strncpy' specified bound 16 equals destination size [-Werror=stringop-truncation]
297 | strncpy(to, from, to_length);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
Reorganize the function to completely separate the nul-terminated from
the space-padded/non-terminated case. The former is just strscpy_pad(),
while the latter does not have a standard function.
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
---
drivers/scsi/scsi_devinfo.c | 30 ++++++++++++++++++++----------
1 file changed, 20 insertions(+), 10 deletions(-)
diff --git a/drivers/scsi/scsi_devinfo.c b/drivers/scsi/scsi_devinfo.c
index ba7237e83863..58726c15ebac 100644
--- a/drivers/scsi/scsi_devinfo.c
+++ b/drivers/scsi/scsi_devinfo.c
@@ -290,18 +290,28 @@ static struct scsi_dev_info_list_table *scsi_devinfo_lookup_by_key(int key)
static void scsi_strcpy_devinfo(char *name, char *to, size_t to_length,
char *from, int compatible)
{
- size_t from_length;
+ int ret;
- from_length = strlen(from);
- /* This zero-pads the destination */
- strncpy(to, from, to_length);
- if (from_length < to_length && !compatible) {
- /*
- * space pad the string if it is short.
- */
- memset(&to[from_length], ' ', to_length - from_length);
+ if (compatible) {
+ /* This zero-pads and nul-terminates the destination */
+ ret = strscpy_pad(to, from, to_length);
+ } else {
+ /* no nul-termination but space-padding for short strings */
+ size_t from_length = strlen(from);
+ ret = from_length;
+
+ if (from_length > to_length) {
+ from_length = to_length;
+ ret = -E2BIG;
+ }
+
+ memcpy(to, from, from_length);
+
+ if (from_length < to_length)
+ memset(&to[from_length], ' ', to_length - from_length);
}
- if (from_length > to_length)
+
+ if (ret < 0)
printk(KERN_WARNING "%s: %s string '%s' is too long\n",
__func__, name, from);
}
--
2.39.2
next prev parent reply other threads:[~2024-03-28 14:05 UTC|newest]
Thread overview: 38+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-03-28 14:04 [PATCH 00/11] address remaining stringop-truncation warnings Arnd Bergmann
2024-03-28 14:04 ` [PATCH 01/11] staging: vc04_services: changen strncpy() to strscpy_pad() Arnd Bergmann
2024-03-28 14:42 ` Dan Carpenter
2024-03-28 16:15 ` Arnd Bergmann
2024-03-28 23:10 ` Justin Stitt
2024-03-28 14:04 ` Arnd Bergmann [this message]
2024-03-28 16:46 ` [PATCH 02/11] scsi: devinfo: rework scsi_strcpy_devinfo() Bart Van Assche
2024-03-28 23:14 ` Justin Stitt
2024-03-28 23:18 ` Arnd Bergmann
2024-03-28 14:04 ` [PATCH 03/11] staging: replace weird strncpy() with memcpy() Arnd Bergmann
2024-03-28 16:35 ` Dan Carpenter
2024-04-08 14:45 ` Arnd Bergmann
2024-04-08 15:59 ` Dan Carpenter
2024-04-08 19:20 ` Arnd Bergmann
2024-03-28 14:04 ` [PATCH 04/11] orangefs: convert strncpy() to strscpy() Arnd Bergmann
2024-03-28 23:17 ` Justin Stitt
2024-03-28 14:04 ` [PATCH 05/11] test_hexdump: avoid string truncation warning Arnd Bergmann
2024-03-28 23:54 ` Justin Stitt
2024-04-08 15:38 ` Arnd Bergmann
2024-04-08 19:53 ` Justin Stitt
2024-03-28 14:04 ` [PATCH 06/11] acpi: avoid warning for truncated string copy Arnd Bergmann
2024-03-28 23:20 ` Justin Stitt
2024-04-08 14:41 ` Rafael J. Wysocki
2024-03-28 14:04 ` [PATCH 07/11] block/partitions/ldm: convert strncpy() to strscpy() Arnd Bergmann
2024-03-28 23:24 ` Justin Stitt
2024-03-28 14:04 ` [PATCH 08/11] blktrace: convert strncpy() to strscpy_pad() Arnd Bergmann
2024-03-28 14:14 ` Steven Rostedt
2024-04-08 18:05 ` Arnd Bergmann
2024-03-28 14:04 ` [PATCH 09/11] staging: rtl8723bs: convert strncpy to strscpy Arnd Bergmann
2024-03-28 23:01 ` Justin Stitt
2024-04-08 18:15 ` Arnd Bergmann
2024-03-28 14:04 ` [PATCH 10/11] staging: greybus: change strncpy() to strscpy() Arnd Bergmann
2024-03-28 15:00 ` Dan Carpenter
2024-04-08 18:26 ` Arnd Bergmann
2024-04-09 7:09 ` Dan Carpenter
2024-03-28 23:28 ` Justin Stitt
2024-04-08 18:30 ` Arnd Bergmann
2024-03-28 14:04 ` [PATCH 11/11] kbuild: enable -Wstringop-truncation globally Arnd Bergmann
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240328140512.4148825-3-arnd@kernel.org \
--to=arnd@kernel.org \
--cc=arnd@arndb.de \
--cc=bvanassche@acm.org \
--cc=chris@chrisdown.name \
--cc=jejb@linux.ibm.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-scsi@vger.kernel.org \
--cc=martin.petersen@oracle.com \
--cc=pmladek@suse.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox