mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Jonas Rebmann <jre@pengutronix.de>
To: Kees Cook <kees@kernel.org>, Andy Shevchenko <andy@kernel.org>,
	 Andrew Morton <akpm@linux-foundation.org>
Cc: linux-hardening@vger.kernel.org, linux-kernel@vger.kernel.org,
	 kernel@pengutronix.de, Jonas Rebmann <jre@pengutronix.de>
Subject: [PATCH 5/5] lib/string_helpers: fix counting of remaining bytes in string_unescape()
Date: Wed, 16 Sep 2026 19:38:10 +0200	[thread overview]
Message-ID: <20260916-string_unescape-v1-5-7f8bd986fa33@pengutronix.de> (raw)
In-Reply-To: <20260916-string_unescape-v1-0-7f8bd986fa33@pengutronix.de>

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.

Fixes: 16c7fa05829e ("lib/string_helpers: introduce generic string_unescape")
Signed-off-by: Jonas Rebmann <jre@pengutronix.de>
---
 lib/string_helpers.c             | 5 +++--
 lib/tests/string_helpers_kunit.c | 2 ++
 2 files changed, 5 insertions(+), 2 deletions(-)

diff --git a/lib/string_helpers.c b/lib/string_helpers.c
index cb41ef9d8c5b..4a621f884bde 100644
--- a/lib/string_helpers.c
+++ b/lib/string_helpers.c
@@ -329,7 +329,7 @@ int string_unescape(char *src, char *dst, size_t size, unsigned int flags)
 		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, size_t size, unsigned int flags)
 				continue;
 
 			*out++ = '\\';
-			size--;
+			if (!--size)
+				break;
 		}
 		*out++ = *src++;
 	}
diff --git a/lib/tests/string_helpers_kunit.c b/lib/tests/string_helpers_kunit.c
index 2e02c680cbb2..10763a01be83 100644
--- a/lib/tests/string_helpers_kunit.c
+++ b/lib/tests/string_helpers_kunit.c
@@ -626,6 +626,8 @@ static void test_unescape(struct kunit *test)
 
 	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)

-- 
2.56.0.rc0.108.gf0ef1b96a0


  parent reply	other threads:[~2026-09-16 17:38 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-16 17:38 [PATCH 0/5] lib/string_helpers: fixes and test cases for string_unescape() Jonas Rebmann
2026-09-16 17:38 ` [PATCH 1/5] lib/tests: string_helpers: check null terminator too Jonas Rebmann
2026-09-17  7:14   ` Andy Shevchenko
2026-09-17  8:58     ` Jonas Rebmann
2026-09-17  9:07       ` Andy Shevchenko
2026-09-16 17:38 ` [PATCH 2/5] lib/tests: string_helpers: drop unused parameters Jonas Rebmann
2026-09-17  7:18   ` Andy Shevchenko
2026-09-17  8:58     ` Jonas Rebmann
2026-09-16 17:38 ` [PATCH 3/5] lib/tests: string_helpers: introduce test_string_unescape_one Jonas Rebmann
2026-09-17  7:21   ` Andy Shevchenko
2026-09-17  8:58     ` Jonas Rebmann
2026-09-17  9:08       ` Andy Shevchenko
2026-09-17  9:56         ` Jonas Rebmann
2026-09-17 12:17           ` Andy Shevchenko
2026-09-16 17:38 ` [PATCH 4/5] lib/string_helpers: use full destination buffer in string_unescape() Jonas Rebmann
2026-09-17  7:45   ` Andy Shevchenko
2026-09-17  9:02     ` Jonas Rebmann
2026-09-17 12:32       ` Andy Shevchenko
2026-09-17 15:47         ` Jonas Rebmann
2026-09-16 17:38 ` Jonas Rebmann [this message]
2026-09-17  8:04   ` [PATCH 5/5] lib/string_helpers: fix counting of remaining bytes " Andy Shevchenko
2026-09-16 21:36 ` [PATCH 0/5] lib/string_helpers: fixes and test cases for string_unescape() Andrew Morton
2026-09-16 23:13   ` Eric Biggers
2026-09-17  0:04     ` Andrew Morton

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=20260916-string_unescape-v1-5-7f8bd986fa33@pengutronix.de \
    --to=jre@pengutronix.de \
    --cc=akpm@linux-foundation.org \
    --cc=andy@kernel.org \
    --cc=kees@kernel.org \
    --cc=kernel@pengutronix.de \
    --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

all inboxes | Powered by JetHome®