[PATCH 2/2] scsi: myrs: fix -Wfortify-source

From: Nick Desaulniers
Date: Mon Aug 28 2023 - 18:26:56 EST


clang-18 has improved its support for detecting operations that will
truncate values at runtime via -Wfortify-source.

Fixes the warning:
drivers/scsi/myrs.c:1089:10: warning: 'snprintf' will always be
truncated; specified size is 32, but format string expands to at least
34 [-Wfortify-source]

In particular, the string literal "physical device - not rebuilding\n"
is indeed 34B by my count.

When we have a string literal that does not contain any format flags,
rather than use snprintf (sometimes with a size that's too small), let's
use sprintf.

This is pattern is cleaned up throughout the file.

Reported-by: Nathan Chancellor <nathan@xxxxxxxxxx>
Closes: https://github.com/ClangBuiltLinux/linux/issues/1923
Signed-off-by: Nick Desaulniers <ndesaulniers@xxxxxxxxxx>
---
drivers/scsi/myrs.c | 14 +++++++-------
1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/drivers/scsi/myrs.c b/drivers/scsi/myrs.c
index a1eec65a9713..729f08379bd0 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 sprintf(buf, "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 sprintf(buf, "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 sprintf(buf, "physical device - not rebuilding\n");

ldev_info = sdev->hostdata;
ldev_num = ldev_info->ldev_num;
@@ -1102,7 +1102,7 @@ static ssize_t rebuild_show(struct device *dev,
(size_t)ldev_info->rbld_lba,
(size_t)ldev_info->cfg_devsize);
} else
- return snprintf(buf, 32, "not rebuilding\n");
+ return sprintf(buf, "not rebuilding\n");
}

static ssize_t rebuild_store(struct device *dev,
@@ -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 sprintf(buf, "physical device - not checking\n");

ldev_info = sdev->hostdata;
if (!ldev_info)
@@ -1202,7 +1202,7 @@ static ssize_t consistency_check_show(struct device *dev,
(size_t)ldev_info->cc_lba,
(size_t)ldev_info->cfg_devsize);
else
- return snprintf(buf, 32, "not checking\n");
+ return sprintf(buf, "not checking\n");
}

static ssize_t consistency_check_store(struct device *dev,
@@ -1376,7 +1376,7 @@ static ssize_t processor_show(struct device *dev,
info->cpu[1].cpu_name,
second_processor, info->cpu[1].cpu_count);
else
- ret = snprintf(buf, 64, "1: absent\n2: absent\n");
+ ret = sprintf(buf, "1: absent\n2: absent\n");

return ret;
}

--
2.42.0.rc2.253.gd59a3bf2b4-goog