[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <73de2cd5-9770-453c-b002-7cd561bdbc7b@acm.org>
Date: Thu, 28 Mar 2024 09:46:58 -0700
From: Bart Van Assche <bvanassche@....org>
To: Arnd Bergmann <arnd@...nel.org>, linux-kernel@...r.kernel.org,
"James E.J. Bottomley" <jejb@...ux.ibm.com>,
"Martin K. Petersen" <martin.petersen@...cle.com>
Cc: Arnd Bergmann <arnd@...db.de>, Chris Down <chris@...isdown.name>,
Petr Mladek <pmladek@...e.com>, linux-scsi@...r.kernel.org
Subject: Re: [PATCH 02/11] scsi: devinfo: rework scsi_strcpy_devinfo()
On 3/28/24 07:04, Arnd Bergmann wrote:
> 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);
> }
Please eliminate the variable 'ret'. I think that will improve
readability of the new code.
Thanks,
Bart.
Powered by blists - more mailing lists