summaryrefslogtreecommitdiffstats
path: root/libutils/tests/Unicode_test.cpp
diff options
context:
space:
mode:
authorSergio Giro <sgiro@google.com>2016-07-20 20:01:33 +0100
committerSergio Giro <sgiro@google.com>2016-08-02 18:47:53 +0000
commit1dcc0c82394ec9cd6887c7ca39f9b5024db01ac9 (patch)
tree5e07e40ae8bc52895b05b42e9d627c4f3057a0e0 /libutils/tests/Unicode_test.cpp
parentde717d55c7a3f7d8051222f26ef12eb38b3777f2 (diff)
downloadsystem_core-1dcc0c82394ec9cd6887c7ca39f9b5024db01ac9.tar.gz
system_core-1dcc0c82394ec9cd6887c7ca39f9b5024db01ac9.tar.bz2
system_core-1dcc0c82394ec9cd6887c7ca39f9b5024db01ac9.zip
Unicode: specify destination length in utf8_to_utf16 methods
String16(const char *utf8) now returns the empty string in case a string ends halfway throw a utf8 character. Bug: 29267949 Change-Id: I5223caa7d42f4582a982609a898a02043265c6d3
Diffstat (limited to 'libutils/tests/Unicode_test.cpp')
-rw-r--r--libutils/tests/Unicode_test.cpp13
1 files changed, 12 insertions, 1 deletions
diff --git a/libutils/tests/Unicode_test.cpp b/libutils/tests/Unicode_test.cpp
index c263f75e2..d23e43a71 100644
--- a/libutils/tests/Unicode_test.cpp
+++ b/libutils/tests/Unicode_test.cpp
@@ -98,7 +98,7 @@ TEST_F(UnicodeTest, UTF8toUTF16Normal) {
char16_t output[1 + 1 + 1 + 2 + 1]; // Room for NULL
- utf8_to_utf16(str, sizeof(str), output);
+ utf8_to_utf16(str, sizeof(str), output, sizeof(output) / sizeof(output[0]));
EXPECT_EQ(0x0030, output[0])
<< "should be U+0030";
@@ -147,4 +147,15 @@ TEST_F(UnicodeTest, strstr16TargetNotPresent) {
EXPECT_EQ(nullptr, result);
}
+// http://b/29267949
+// Test that overreading in utf8_to_utf16_length is detected
+TEST_F(UnicodeTest, InvalidUtf8OverreadDetected) {
+ // An utf8 char starting with \xc4 is two bytes long.
+ // Add extra zeros so no extra memory is read in case the code doesn't
+ // work as expected.
+ static char utf8[] = "\xc4\x00\x00\x00";
+ ASSERT_DEATH(utf8_to_utf16_length((uint8_t *) utf8, strlen(utf8),
+ true /* overreadIsFatal */), "" /* regex for ASSERT_DEATH */);
+}
+
}