public inbox for linux-hardening@vger.kernel.org
 help / color / mirror / Atom feed
From: Josh Law <objecting@objecting.org>
To: kees@kernel.org, akpm@linux-foundation.org
Cc: andy@kernel.org, linux-hardening@vger.kernel.org,
	linux-kernel@vger.kernel.org, Josh Law <objecting@objecting.org>
Subject: [PATCH] lib/string: replace BUG_ON with WARN_ON_ONCE in strlcat
Date: Mon, 16 Mar 2026 16:17:28 +0000	[thread overview]
Message-ID: <20260316161728.84485-1-objecting@objecting.org> (raw)

BUG_ON() in a library function is too harsh -- it panics the kernel
when a caller passes a dest string whose length already meets or
exceeds count. This is a caller bug, not a reason to bring down the
entire system.

Replace with WARN_ON_ONCE() and a safe early return. The return value
of count signals truncation to the caller, consistent with strlcat
semantics (return >= count means the output was truncated).

This follows the guidance in include/asm-generic/bug.h which
explicitly discourages BUG_ON: "Don't use BUG() or BUG_ON() unless
there's really no way out."

Signed-off-by: Josh Law <objecting@objecting.org>
---
 lib/string.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/lib/string.c b/lib/string.c
index b632c71df1a5..ae3eb192534d 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -255,8 +255,9 @@ size_t strlcat(char *dest, const char *src, size_t count)
 	size_t len = strlen(src);
 	size_t res = dsize + len;
 
-	/* This would be a bug */
-	BUG_ON(dsize >= count);
+	/* This would be a caller bug */
+	if (WARN_ON_ONCE(dsize >= count))
+		return count;
 
 	dest += dsize;
 	count -= dsize;
-- 
2.34.1


             reply	other threads:[~2026-03-16 16:17 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-16 16:17 Josh Law [this message]
2026-03-16 16:29 ` [PATCH] lib/string: replace BUG_ON with WARN_ON_ONCE in strlcat Greg KH
2026-03-16 16:30 ` Andy Shevchenko
2026-03-16 16:36   ` Josh Law
2026-03-16 16:43     ` Andy Shevchenko
2026-03-16 16:46       ` Josh Law
2026-03-16 16:51         ` Andy Shevchenko
2026-03-16 16:52           ` Josh Law

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260316161728.84485-1-objecting@objecting.org \
    --to=objecting@objecting.org \
    --cc=akpm@linux-foundation.org \
    --cc=andy@kernel.org \
    --cc=kees@kernel.org \
    --cc=linux-hardening@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox