Skip to contentSkip to conversation

lib/string: rewrite strlcat() to use sized_strscpy()

PATCH In review Josh Law 2 participants 2 messages Mar 16

Subsystems (2)
lore
Share & tools
lore

Josh Law

author
#0 lore raw

PATCH 1/1 lib/string: rewrite strlcat() to use sized_strscpy()

Replace the hand-rolled copy logic and BUG_ON() in strlcat() with a call to sized_strscpy(). This removes the kernel panic on caller misuse (dsize >= count) and instead returns a truncation indicator consistent with BSD strlcat semantics.

strlcat() is deprecated and callers should be converted to strscpy() or similar bounded interfaces.

---
 lib/string.c | 15 +++++----------
 1 file changed, 5 insertions(+), 10 deletions(-)
lib/string.c +5 -10
index b632c71df1a5..414016f8a6a6 100644
@@ -253,18 +253,13 @@ size_t strlcat(char *dest, const char *src, size_t count)
 {
 	size_t dsize = strlen(dest);
 	size_t len = strlen(src);
-	size_t res = dsize + len;
 
-	/* This would be a bug */
-	BUG_ON(dsize >= count);
+	if (dsize >= count)
+		return count + len;
 
-	dest += dsize;
-	count -= dsize;
-	if (len >= count)
-		len = count-1;
-	__builtin_memcpy(dest, src, len);
-	dest[len] = 0;
-	return res;
+	sized_strscpy(dest + dsize, src, count - dsize);
+
+	return dsize + len;
 }
 EXPORT_SYMBOL(strlcat);
 #endif
Signature · 1 line
2.34.1
#1 lore raw
Quoted 7 lines · Josh Law

Replace the hand-rolled copy logic and BUG_ON() in strlcat() with a call to sized_strscpy(). This removes the kernel panic on caller misuse (dsize >= count) and instead returns a truncation indicator consistent with BSD strlcat semantics.

strlcat() is deprecated and callers should be converted to strscpy() or similar bounded interfaces.

I mentioned something different, id est converting users. This function will go when ~130 or so users gone.

So, I don't see a need to touch this function (implementation) at all.

Signature · 2 lines
With Best Regards,
Andy Shevchenko