[PATCH v2] scsi: target: avoid strlen() in target_core_call_addhbatotarget()

Dmitry Antipov <[email protected]> Wed, 22 Jul 2026 11:14:10 +0300
Newsgroups org.kernel.vger.target-devel,org.kernel.vger.linux-scsi
Message-ID <[email protected]>
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 <[email protected]>
---
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