mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Andrey Zakharov <dedemorow@gmail.com>
To: simont@opensource.cirrus.com, ckeepax@opensource.cirrus.com,
	rf@opensource.cirrus.com
Cc: broonie@kernel.org, patches@opensource.cirrus.com,
	linux-kernel@vger.kernel.org
Subject: [PATCH] firmware: cs_dsp: Replace strlcat() with seq_buf in KUnit tests
Date: Tue, 24 Nov 2026 14:36:55 +0300	[thread overview]
Message-ID: <20261124113655.610791-1-dedemorow@gmail.com> (raw)

strlcat() is being removed from the kernel. It runs an unbounded
strlen() on the destination, and its size argument is not tied to
the real buffer, see the issue below.

The bin and wmfw tests fill a 512 byte buffer with a repeated string
by calling strlcat() in a loop, and use the return value to detect
that the buffer is full. Use seq_buf_init() and seq_buf_puts() and
stop when seq_buf_has_overflowed() is true. kunit_kzalloc() returns
a zeroed buffer, so seq_buf_init() is enough here.

Tested: built with gcc 16.2.1 and ran the cs_dsp KUnit suites under
QEMU (x86_64, kunit.py, REGMAP_BUILD): 5829 of 5829 tests pass, the
same as without the patch. The tests do not look at the contents of
the buffer, so I also checked it with a temporary assertion after the
loop: strlen(infobuf) is 511 with the old strlcat() loop and with the
new code, and the assertion fails if 510 is expected.

Not tested: other architectures and any real hardware.

Link: https://github.com/KSPP/linux/issues/370
Signed-off-by: Andrey Zakharov <dedemorow@gmail.com>
---
 drivers/firmware/cirrus/test/cs_dsp_test_bin.c  | 7 +++++--
 drivers/firmware/cirrus/test/cs_dsp_test_wmfw.c | 7 +++++--
 2 files changed, 10 insertions(+), 4 deletions(-)

diff --git a/drivers/firmware/cirrus/test/cs_dsp_test_bin.c b/drivers/firmware/cirrus/test/cs_dsp_test_bin.c
index 63416838f865..2f44b7a6e637 100644
--- a/drivers/firmware/cirrus/test/cs_dsp_test_bin.c
+++ b/drivers/firmware/cirrus/test/cs_dsp_test_bin.c
@@ -17,6 +17,7 @@
 #include <linux/math.h>
 #include <linux/random.h>
 #include <linux/regmap.h>
+#include <linux/seq_buf.h>
 
 #include "../cs_dsp.h"
 
@@ -2109,6 +2110,7 @@ static void bin_patch_name_and_info(struct kunit *test)
 	unsigned int reg_inc_per_word = cs_dsp_mock_reg_addr_inc_per_unpacked_word(priv);
 	u32 reg_val, payload_data;
 	char *infobuf;
+	struct seq_buf sb;
 	unsigned int alg_base_words, reg_addr;
 	struct firmware *fw;
 
@@ -2126,8 +2128,9 @@ static void bin_patch_name_and_info(struct kunit *test)
 	infobuf = kunit_kzalloc(test, 512, GFP_KERNEL);
 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, infobuf);
 
-	for (; strlcat(infobuf, "Waffle{Blah}\n", 512) < 512; )
-		;
+	seq_buf_init(&sb, infobuf, 512);
+	while (!seq_buf_has_overflowed(&sb))
+		seq_buf_puts(&sb, "Waffle{Blah}\n");
 
 	cs_dsp_mock_bin_add_info(priv->local->bin_builder, infobuf);
 
diff --git a/drivers/firmware/cirrus/test/cs_dsp_test_wmfw.c b/drivers/firmware/cirrus/test/cs_dsp_test_wmfw.c
index 1de70e81a868..464a0cc79415 100644
--- a/drivers/firmware/cirrus/test/cs_dsp_test_wmfw.c
+++ b/drivers/firmware/cirrus/test/cs_dsp_test_wmfw.c
@@ -16,6 +16,7 @@
 #include <linux/firmware/cirrus/wmfw.h>
 #include <linux/random.h>
 #include <linux/regmap.h>
+#include <linux/seq_buf.h>
 #include <linux/string.h>
 #include <linux/vmalloc.h>
 
@@ -1722,6 +1723,7 @@ static void wmfw_load_with_info(struct kunit *test)
 	unsigned int reg_addr;
 	u8 *payload_data, *readback;
 	char *infobuf;
+	struct seq_buf sb;
 	const unsigned int payload_size_bytes = 48;
 	int ret;
 
@@ -1745,8 +1747,9 @@ static void wmfw_load_with_info(struct kunit *test)
 	infobuf = kunit_kzalloc(test, 512, GFP_KERNEL);
 	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, infobuf);
 
-	for (; strlcat(infobuf, "Waffle{Blah}\n", 512) < 512;)
-		;
+	seq_buf_init(&sb, infobuf, 512);
+	while (!seq_buf_has_overflowed(&sb))
+		seq_buf_puts(&sb, "Waffle{Blah}\n");
 
 	cs_dsp_mock_wmfw_add_info(local->wmfw_builder, infobuf);
 	cs_dsp_mock_wmfw_add_info(local->wmfw_builder, "Another block of info");
-- 
2.56.1


                 reply	other threads:[~2026-11-24 11:37 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20261124113655.610791-1-dedemorow@gmail.com \
    --to=dedemorow@gmail.com \
    --cc=broonie@kernel.org \
    --cc=ckeepax@opensource.cirrus.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=patches@opensource.cirrus.com \
    --cc=rf@opensource.cirrus.com \
    --cc=simont@opensource.cirrus.com \
    /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

all inboxes | Powered by JetHome®