Linux SCSI subsystem development
 help / color / mirror / Atom feed
* [PATCH] scsi: target: avoid strlen() in target_core_call_addhbatotarget()
@ 2026-07-22  6:35 Dmitry Antipov
  2026-07-22  6:48 ` sashiko-bot
  0 siblings, 1 reply; 3+ messages in thread
From: Dmitry Antipov @ 2026-07-22  6:35 UTC (permalink / raw)
  To: Martin K . Petersen; +Cc: linux-scsi, target-devel, Dmitry Antipov

Since 'snprintf()' is capable to detect output truncation, an extra call
to 'strlen()' in 'target_core_call_addhbatotarget()' may be dropped.

Signed-off-by: Dmitry Antipov <dmantipov@yandex.ru>
---
 drivers/target/target_core_configfs.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/target/target_core_configfs.c b/drivers/target/target_core_configfs.c
index 2b19a956007b..3214047031b8 100644
--- a/drivers/target/target_core_configfs.c
+++ b/drivers/target/target_core_configfs.c
@@ -3635,13 +3635,13 @@ static struct config_group *target_core_call_addhbatotarget(
 	unsigned long plugin_dep_id = 0;
 	int ret;
 
-	if (strlen(name) >= TARGET_CORE_NAME_MAX_LEN) {
+	ret = snprintf(buf, TARGET_CORE_NAME_MAX_LEN, "%s", name);
+	if (ret >= TARGET_CORE_NAME_MAX_LEN) {
 		pr_err("Passed *name strlen(): %d exceeds"
 			" TARGET_CORE_NAME_MAX_LEN: %d\n", (int)strlen(name),
 			TARGET_CORE_NAME_MAX_LEN);
 		return ERR_PTR(-ENAMETOOLONG);
 	}
-	snprintf(buf, TARGET_CORE_NAME_MAX_LEN, "%s", name);
 
 	str = strstr(buf, "_");
 	if (!str) {
-- 
2.55.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] scsi: target: avoid strlen() in target_core_call_addhbatotarget()
  2026-07-22  6:35 [PATCH] scsi: target: avoid strlen() in target_core_call_addhbatotarget() Dmitry Antipov
@ 2026-07-22  6:48 ` sashiko-bot
  2026-07-22  8:14   ` [PATCH v2] " Dmitry Antipov
  0 siblings, 1 reply; 3+ messages in thread
From: sashiko-bot @ 2026-07-22  6:48 UTC (permalink / raw)
  To: Dmitry Antipov; +Cc: linux-scsi

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] Incomplete replacement of strlen(): a redundant call to strlen(name) remains in the error handling path despite the length already being captured.
--

--- Patch [1]: [PATCH] scsi: target: avoid strlen() in target_core_call_addhbatotarget() ---
commit 03360414acae57f08ec406c8bedc559ab4a7788c
Author: Dmitry Antipov <dmantipov@yandex.ru>

scsi: target: avoid strlen() in target_core_call_addhbatotarget()

This commit avoids calling strlen() by leveraging the return value of
snprintf() to detect if the string exceeds the maximum length.

> diff --git a/drivers/target/target_core_configfs.c b/drivers/target/target_core_configfs.c
> index 2b19a956007b7..3214047031b86 100644
> --- a/drivers/target/target_core_configfs.c
> +++ b/drivers/target/target_core_configfs.c
> @@ -3635,13 +3635,13 @@ static struct config_group *target_core_call_addhbatotarget(
>  	unsigned long plugin_dep_id = 0;
>  	int ret;
>  
> -	if (strlen(name) >= TARGET_CORE_NAME_MAX_LEN) {
> +	ret = snprintf(buf, TARGET_CORE_NAME_MAX_LEN, "%s", name);
> +	if (ret >= TARGET_CORE_NAME_MAX_LEN) {
>  		pr_err("Passed *name strlen(): %d exceeds"
>  			" TARGET_CORE_NAME_MAX_LEN: %d\n", (int)strlen(name),

[Severity: Low]
Since ret now holds the exact length of the input string, could this remaining
call to strlen(name) in target_core_call_addhbatotarget() be replaced with ret
to completely avoid strlen() on this path?

>  			TARGET_CORE_NAME_MAX_LEN);
>  		return ERR_PTR(-ENAMETOOLONG);
>  	}
> -	snprintf(buf, TARGET_CORE_NAME_MAX_LEN, "%s", name);

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260722063523.669361-1-dmantipov@yandex.ru?part=1

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH v2] scsi: target: avoid strlen() in target_core_call_addhbatotarget()
  2026-07-22  6:48 ` sashiko-bot
@ 2026-07-22  8:14   ` Dmitry Antipov
  0 siblings, 0 replies; 3+ messages in thread
From: Dmitry Antipov @ 2026-07-22  8:14 UTC (permalink / raw)
  To: Martin K . Petersen; +Cc: linux-scsi, target-devel, Dmitry Antipov

Since 'snprintf()' is capable to calculate output length
and detect output truncation, both calls to 'strlen()'
in 'target_core_call_addhbatotarget()' may be dropped.

Assisted-by: sashiko:gemini-3.1-pro-preview
Signed-off-by: Dmitry Antipov <dmantipov@yandex.ru>
---
v2: adjust error message and drop strlen() on error path as well (Sashiko)
---
 drivers/target/target_core_configfs.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/drivers/target/target_core_configfs.c b/drivers/target/target_core_configfs.c
index 2b19a956007b..8a41139e6594 100644
--- a/drivers/target/target_core_configfs.c
+++ b/drivers/target/target_core_configfs.c
@@ -3635,13 +3635,13 @@ static struct config_group *target_core_call_addhbatotarget(
 	unsigned long plugin_dep_id = 0;
 	int ret;
 
-	if (strlen(name) >= TARGET_CORE_NAME_MAX_LEN) {
-		pr_err("Passed *name strlen(): %d exceeds"
-			" TARGET_CORE_NAME_MAX_LEN: %d\n", (int)strlen(name),
-			TARGET_CORE_NAME_MAX_LEN);
+	ret = snprintf(buf, TARGET_CORE_NAME_MAX_LEN, "%s", name);
+	if (ret >= TARGET_CORE_NAME_MAX_LEN) {
+		pr_err("Passed *name length: %d exceeds"
+		        " TARGET_CORE_NAME_MAX_LEN: %d\n",
+		        ret, TARGET_CORE_NAME_MAX_LEN);
 		return ERR_PTR(-ENAMETOOLONG);
 	}
-	snprintf(buf, TARGET_CORE_NAME_MAX_LEN, "%s", name);
 
 	str = strstr(buf, "_");
 	if (!str) {
-- 
2.55.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-07-22  8:16 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-22  6:35 [PATCH] scsi: target: avoid strlen() in target_core_call_addhbatotarget() Dmitry Antipov
2026-07-22  6:48 ` sashiko-bot
2026-07-22  8:14   ` [PATCH v2] " Dmitry Antipov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox