From: David Laight <david.laight.linux@gmail.com>
To: linux-kernel@vger.kernel.org, Andrew Morton <akpm@linux-foundation.org>
Cc: David Laight <david.laight.linux@gmail.com>,
Arnd Bergmann <arnd@arndb.de>,
Linus Torvalds <torvalds@linux-foundation.org>,
Christophe Leroy <christophe.leroy@c-s.fr>,
Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
Rasmus Villemoes <linux@rasmusvillemoes.dk>,
nnac123@linux.ibm.com, horms@kernel.org
Subject: [PATCH next 4/8] test_hexdump: Check for buffer overrun of sample output buffer
Date: Sat, 8 Mar 2025 09:34:48 +0000 [thread overview]
Message-ID: <20250308093452.3742-5-david.laight.linux@gmail.com> (raw)
In-Reply-To: <20250308093452.3742-1-david.laight.linux@gmail.com>
While the output generated by test_hexdump_prepare_test() shouldn't
be longer than the size of the buffer passed, for safety verify that
the buffer is long enough.
If too short fill the buffer with an error message - output on
test failure.
Signed-off-by: David Laight <david.laight.linux@gmail.com>
---
lib/test_hexdump.c | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/lib/test_hexdump.c b/lib/test_hexdump.c
index 743ea5c78f9e..ed6f0b0a1bb3 100644
--- a/lib/test_hexdump.c
+++ b/lib/test_hexdump.c
@@ -39,6 +39,14 @@ static size_t __init test_hexdump_prepare_test(size_t len, size_t rowsize,
groupsize = 1;
byteswap = IS_ENABLED(CONFIG_CPU_BIG_ENDIAN) ? 0 : groupsize - 1;
+ /* Check test passed a big enough output buffer */
+ if (ascii)
+ i = rowsize * 2 + rowsize / groupsize + 1 + len + 1;
+ else
+ i = len * 2 + len / groupsize - 1 + 1;
+ if (i > testlen)
+ return scnprintf(test, testlen, "buffer too short %zu < %zu", testlen, i);
+
/* hex dump */
p = test;
for (i = 0, j = 0; i < len; i++) {
--
2.39.5
next prev parent reply other threads:[~2025-03-08 9:35 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-03-08 9:34 [PATCH next 0/8] test_hexdump: Improve test coverage David Laight
2025-03-08 9:34 ` [PATCH next 1/8] test_hexdump: Change rowsize and groupsize to size_t David Laight
2025-03-10 8:28 ` Andy Shevchenko
2025-03-08 9:34 ` [PATCH next 2/8] test_hexdump: Create test output data from the binary input data buffer David Laight
2025-03-10 8:31 ` Andy Shevchenko
2025-03-12 19:28 ` David Laight
2025-03-12 19:36 ` Andy Shevchenko
2025-03-13 9:17 ` David Laight
2025-03-08 9:34 ` [PATCH next 3/8] test_hexdump: Use longer variable names David Laight
2025-03-08 9:34 ` David Laight [this message]
2025-03-10 9:01 ` [PATCH next 4/8] test_hexdump: Check for buffer overrun of sample output buffer Andy Shevchenko
2025-03-12 19:37 ` David Laight
2025-03-08 9:34 ` [PATCH next 5/8] test_hexdump: Fix sample output if zero bytes requested David Laight
2025-03-10 9:03 ` Andy Shevchenko
2025-03-12 19:40 ` David Laight
2025-03-08 9:34 ` [PATCH next 6/8] test_hexdump: If a test fails print all the parameters David Laight
2025-03-08 9:34 ` [PATCH next 7/8] test_hexdump: Run fixed not random tests David Laight
2025-03-08 9:34 ` [PATCH next 8/8] test_hexdump: Test all 256 byte values David Laight
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=20250308093452.3742-5-david.laight.linux@gmail.com \
--to=david.laight.linux@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=andriy.shevchenko@linux.intel.com \
--cc=arnd@arndb.de \
--cc=christophe.leroy@c-s.fr \
--cc=horms@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@rasmusvillemoes.dk \
--cc=nnac123@linux.ibm.com \
--cc=torvalds@linux-foundation.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