Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jduck/6c217beed59d23381385e770c4e1f37f to your computer and use it in GitHub Desktop.
Save jduck/6c217beed59d23381385e770c4e1f37f to your computer and use it in GitHub Desktop.
From d45ffefae10a9a0fba279fb9ab249a70bd52e060 Mon Sep 17 00:00:00 2001
From: "Joshua J. Drake" <android-open-source@qoop.org>
Date: Sat, 15 Aug 2015 07:37:55 -0500
Subject: [PATCH] Correct the length calculation
In some cases the utf16_to_utf8_length incorrectly increments the src pointer.
This results in the length of the utf8 string being incorrect and can lead to
buffer problems in calling code.
Change-Id: Id1170658aa5b1d56acfd3d882e788632ca42b7eb
---
libutils/Unicode.cpp | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/libutils/Unicode.cpp b/libutils/Unicode.cpp
index 6f4b721..626456f 100644
--- a/libutils/Unicode.cpp
+++ b/libutils/Unicode.cpp
@@ -408,10 +408,10 @@ ssize_t utf16_to_utf8_length(const char16_t *src, size_t src_len)
const char16_t* const end = src + src_len;
while (src < end) {
if ((*src & 0xFC00) == 0xD800 && (src + 1) < end
- && (*++src & 0xFC00) == 0xDC00) {
+ && (*(src + 1) & 0xFC00) == 0xDC00) {
// surrogate pairs are always 4 bytes.
ret += 4;
- src++;
+ src += 2;
} else {
ret += utf32_codepoint_utf8_length((char32_t) *src++);
}
--
1.9.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment