diff mbox series

[U-Boot,v2,1/1] lib: vsprintf: avoid overflow printing UTF16 strings

Message ID 20190210101126.11324-1-xypron.glpk@gmx.de
State Accepted
Commit 0e66c10a7d808062898f9a19f0c95f04d8dd85ed
Delegated to: Alexander Graf
Headers show
Series [U-Boot,v2,1/1] lib: vsprintf: avoid overflow printing UTF16 strings | expand

Commit Message

Heinrich Schuchardt Feb. 10, 2019, 10:11 a.m. UTC
We have to ensure while printing UTF16 strings that we do not exceed the
end of the print buffer.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
---
v2
	Use the exact UTF8 length of the converted UTF16 character when
	checking if sufficient space is remaining in the buffer. This
	avoids missing up to 2 characters.
---
 lib/vsprintf.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

Comments

Simon Glass Feb. 10, 2019, 10:48 a.m. UTC | #1
On Sun, 10 Feb 2019 at 04:11, Heinrich Schuchardt <xypron.glpk@gmx.de> wrote:
>
> We have to ensure while printing UTF16 strings that we do not exceed the
> end of the print buffer.
>
> Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
> ---
> v2
>         Use the exact UTF8 length of the converted UTF16 character when
>         checking if sufficient space is remaining in the buffer. This
>         avoids missing up to 2 characters.
> ---
>  lib/vsprintf.c | 10 +++++++---
>  1 file changed, 7 insertions(+), 3 deletions(-)

Reviewed-by: Simon Glass <sjg@chromium.org>
diff mbox series

Patch

diff --git a/lib/vsprintf.c b/lib/vsprintf.c
index 4213441fbf..de5db1aa5c 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -279,13 +279,17 @@  static char *string(char *buf, char *end, char *s, int field_width,
 static char *string16(char *buf, char *end, u16 *s, int field_width,
 		int precision, int flags)
 {
-	u16 *str = s ? s : L"<NULL>";
-	ssize_t len = utf16_strnlen(str, precision);
+	const u16 *str = s ? s : L"<NULL>";
+	ssize_t i, len = utf16_strnlen(str, precision);
 
 	if (!(flags & LEFT))
 		for (; len < field_width; --field_width)
 			ADDCH(buf, ' ');
-	utf16_utf8_strncpy(&buf, str, len);
+	for (i = 0; i < len && buf + utf16_utf8_strnlen(str, 1) <= end; ++i) {
+		s32 s = utf16_get(&str);
+
+		utf8_put(s, &buf);
+	}
 	for (; len < field_width; --field_width)
 		ADDCH(buf, ' ');
 	return buf;