* + lib-string_helpers-fix-counting-of-remaining-bytes-in-string_unescape.patch added to mm-nonmm-unstable branch
@ 2026-09-16 21:39 Andrew Morton
0 siblings, 0 replies; only message in thread
From: Andrew Morton @ 2026-09-16 21:39 UTC (permalink / raw)
To: mm-commits, kernel, kees, andy, jre, akpm
The patch titled
Subject: lib/string_helpers: fix counting of remaining bytes in string_unescape()
has been added to the -mm mm-nonmm-unstable branch. Its filename is
lib-string_helpers-fix-counting-of-remaining-bytes-in-string_unescape.patch
This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/lib-string_helpers-fix-counting-of-remaining-bytes-in-string_unescape.patch
This patch will later appear in the mm-nonmm-unstable branch at
git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
Before you just go and hit "reply", please:
a) Consider who else should be cc'ed
b) Prefer to cc a suitable mailing list as well
c) Ideally: find the original patch on the mailing list and do a
reply-to-all to that, adding suitable additional cc's
*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***
The -mm tree is included into linux-next via various
branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there most days
------------------------------------------------------
From: Jonas Rebmann <jre@pengutronix.de>
Subject: lib/string_helpers: fix counting of remaining bytes in string_unescape()
Date: Wed, 16 Sep 2026 19:38:10 +0200
All of the available sequences expand to exactly one byte, the size check
in the loop condition is sufficient for the case of an escaped character
too.
Otherwise, an escape sequence that should be unescaped to the last
character before terminating with null in the destination buffer will be
output as backslash instead of the escaped character.
The only exception is when encountering a backslash that turns out to not
start a valid escape sequence and both the backslash and the character
following are handled in one iteration. Move the check there.
Add a kunit regression-test that unescapes a character to right in front
of the null terminator of the destination buffer.
Link: https://lore.kernel.org/20260916-string_unescape-v1-5-7f8bd986fa33@pengutronix.de
Fixes: 16c7fa05829e ("lib/string_helpers: introduce generic string_unescape")
Signed-off-by: Jonas Rebmann <jre@pengutronix.de>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Cc: Andy Shevchenko <andy@kernel.org>
Cc: Kees Cook <kees@kernel.org>
Cc: Sascha Hauer <kernel@pengutronix.de>
---
lib/string_helpers.c | 5 +++--
lib/tests/string_helpers_kunit.c | 2 ++
2 files changed, 5 insertions(+), 2 deletions(-)
--- a/lib/string_helpers.c~lib-string_helpers-fix-counting-of-remaining-bytes-in-string_unescape
+++ a/lib/string_helpers.c
@@ -329,7 +329,7 @@ int string_unescape(char *src, char *dst
size = SIZE_MAX;
while (*src && --size) {
- if (src[0] == '\\' && src[1] != '\0' && size > 1) {
+ if (src[0] == '\\' && src[1] != '\0') {
src++;
if (flags & UNESCAPE_SPACE &&
@@ -349,7 +349,8 @@ int string_unescape(char *src, char *dst
continue;
*out++ = '\\';
- size--;
+ if (!--size)
+ break;
}
*out++ = *src++;
}
--- a/lib/tests/string_helpers_kunit.c~lib-string_helpers-fix-counting-of-remaining-bytes-in-string_unescape
+++ a/lib/tests/string_helpers_kunit.c
@@ -626,6 +626,8 @@ static void test_unescape(struct kunit *
test_string_unescape_one(test, "short buffer", UNESCAPE_HEX, "\\x41\\x41B", 4, "AAB", 3);
test_string_unescape_one(test, "unrecognized escape at end", UNESCAPE_HEX, "B\\qX", 4, "B\\q", 3);
+
+ test_string_unescape_one(test, "end of buffer", UNESCAPE_HEX, "B\\x41", 3, "BA", 2);
}
static void test_escape(struct kunit *test)
_
Patches currently in -mm which might be from jre@pengutronix.de are
lib-tests-string_helpers-check-null-terminator-too.patch
lib-tests-string_helpers-drop-unused-parameters.patch
lib-tests-string_helpers-introduce-test_string_unescape_one.patch
lib-string_helpers-use-full-destination-buffer-in-string_unescape.patch
lib-string_helpers-fix-counting-of-remaining-bytes-in-string_unescape.patch
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2026-09-16 21:39 UTC | newest]
Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-09-16 21:39 + lib-string_helpers-fix-counting-of-remaining-bytes-in-string_unescape.patch added to mm-nonmm-unstable branch Andrew Morton
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.