RE: [PATCH] scsi: myrs: Replacing snprintf with scnprintf

From: David Laight
Date: Fri Jun 16 2023 - 04:01:52 EST


From: wuyonggang001@xxxxxxxxxx
> Sent: 14 June 2023 06:56
>
> Fix the following coccicheck warning:
>
> drivers/scsi/myrs.c:1411:8-16: WARNING: use scnprintf or sprintf

That is nothing like the world best commit message.

Also if any of the snprintf() actually overflow the terminating
'\n' is lost - that will have unexpected effects over and above
the truncation.

In fact all the buffer sizes are bogus - did you even look at the code.
I think they should all be using sysfs_emit().

There is also this one:
char serial[17];

memcpy(serial, cs->ctlr_info->serial_number, 16);
serial[16] = '\0';
return snprintf(buf, 16, "%s\n", serial);
A "%.16s\n" format will have the desired effect.
But completely untested with long names because the 16 isn't big enough.

David


>
> Signed-off-by: Yonggang Wu <wuyonggang001@xxxxxxxxxx>
> ---
> drivers/scsi/myrs.c | 22 +++++++++++-----------
> 1 file changed, 11 insertions(+), 11 deletions(-)
>
> diff --git a/drivers/scsi/myrs.c b/drivers/scsi/myrs.c
> index a1eec65a9713..ced1d2fbd862 100644
> --- a/drivers/scsi/myrs.c
> +++ b/drivers/scsi/myrs.c
> @@ -939,7 +939,7 @@ static ssize_t raid_state_show(struct device *dev,
> int ret;
>
> if (!sdev->hostdata)
> - return snprintf(buf, 16, "Unknown\n");
> + return scnprintf(buf, 16, "Unknown\n");
>
> if (sdev->channel >= cs->ctlr_info->physchan_present) {
> struct myrs_ldev_info *ldev_info = sdev->hostdata;
> @@ -1058,7 +1058,7 @@ static ssize_t raid_level_show(struct device *dev,
> const char *name = NULL;
>
> if (!sdev->hostdata)
> - return snprintf(buf, 16, "Unknown\n");
> + return scnprintf(buf, 16, "Unknown\n");
>
> if (sdev->channel >= cs->ctlr_info->physchan_present) {
> struct myrs_ldev_info *ldev_info;
> @@ -1086,7 +1086,7 @@ static ssize_t rebuild_show(struct device *dev,
> unsigned char status;
>
> if (sdev->channel < cs->ctlr_info->physchan_present)
> - return snprintf(buf, 32, "physical device - not rebuilding\n");
> + return scnprintf(buf, 32, "physical device - not
> rebuilding\n");
>
> ldev_info = sdev->hostdata;
> ldev_num = ldev_info->ldev_num;
> @@ -1190,7 +1190,7 @@ static ssize_t consistency_check_show(struct
> device *dev,
> unsigned short ldev_num;
>
> if (sdev->channel < cs->ctlr_info->physchan_present)
> - return snprintf(buf, 32, "physical device - not checking\n");
> + return scnprintf(buf, 32, "physical device - not checking\n");
>
> ldev_info = sdev->hostdata;
> if (!ldev_info)
> @@ -1303,7 +1303,7 @@ static ssize_t serial_show(struct device *dev,
>
> memcpy(serial, cs->ctlr_info->serial_number, 16);
> serial[16] = '\0';
> - return snprintf(buf, 16, "%s\n", serial);
> + return scnprintf(buf, 16, "%s\n", serial);
> }
> static DEVICE_ATTR_RO(serial);
>
> @@ -1313,7 +1313,7 @@ static ssize_t ctlr_num_show(struct device *dev,
> struct Scsi_Host *shost = class_to_shost(dev);
> struct myrs_hba *cs = shost_priv(shost);
>
> - return snprintf(buf, 20, "%d\n", cs->host->host_no);
> + return scnprintf(buf, 20, "%d\n", cs->host->host_no);
> }
> static DEVICE_ATTR_RO(ctlr_num);
>
> @@ -1388,7 +1388,7 @@ static ssize_t model_show(struct device *dev,
> struct Scsi_Host *shost = class_to_shost(dev);
> struct myrs_hba *cs = shost_priv(shost);
>
> - return snprintf(buf, 28, "%s\n", cs->model_name);
> + return scnprintf(buf, 28, "%s\n", cs->model_name);
> }
> static DEVICE_ATTR_RO(model);
>
> @@ -1398,7 +1398,7 @@ static ssize_t ctlr_type_show(struct device *dev,
> struct Scsi_Host *shost = class_to_shost(dev);
> struct myrs_hba *cs = shost_priv(shost);
>
> - return snprintf(buf, 4, "%d\n", cs->ctlr_info->ctlr_type);
> + return scnprintf(buf, 4, "%d\n", cs->ctlr_info->ctlr_type);
> }
> static DEVICE_ATTR_RO(ctlr_type);
>
> @@ -1408,7 +1408,7 @@ static ssize_t cache_size_show(struct device *dev,
> struct Scsi_Host *shost = class_to_shost(dev);
> struct myrs_hba *cs = shost_priv(shost);
>
> - return snprintf(buf, 8, "%d MB\n", cs->ctlr_info->cache_size_mb);
> + return scnprintf(buf, 8, "%d MB\n", cs->ctlr_info->cache_size_mb);
> }
> static DEVICE_ATTR_RO(cache_size);
>
> @@ -1418,7 +1418,7 @@ static ssize_t firmware_show(struct device *dev,
> struct Scsi_Host *shost = class_to_shost(dev);
> struct myrs_hba *cs = shost_priv(shost);
>
> - return snprintf(buf, 16, "%d.%02d-%02d\n",
> + return scnprintf(buf, 16, "%d.%02d-%02d\n",
> cs->ctlr_info->fw_major_version,
> cs->ctlr_info->fw_minor_version,
> cs->ctlr_info->fw_turn_number);
> @@ -1488,7 +1488,7 @@ static ssize_t
> disable_enclosure_messages_show(struct device *dev,
> struct Scsi_Host *shost = class_to_shost(dev);
> struct myrs_hba *cs = shost_priv(shost);
>
> - return snprintf(buf, 3, "%d\n", cs->disable_enc_msg);
> + return scnprintf(buf, 3, "%d\n", cs->disable_enc_msg);
> }
>
> static ssize_t disable_enclosure_messages_store(struct device *dev,

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)