summaryrefslogtreecommitdiffstats
path: root/libutils/tests
diff options
context:
space:
mode:
authorSergio Giro <sgiro@google.com>2016-07-20 20:01:33 +0100
committerMartijn Coenen <maco@google.com>2016-08-25 19:57:06 +0000
commit9de6776321b80d387e6108683547bc043f868925 (patch)
tree7a35f99e63fe7a332202194d845964458d1fd7fb /libutils/tests
parente461b37965ff609ea36c1fbb6e4e8df1fb7b7d7f (diff)
downloadsystem_core-9de6776321b80d387e6108683547bc043f868925.tar.gz
system_core-9de6776321b80d387e6108683547bc043f868925.tar.bz2
system_core-9de6776321b80d387e6108683547bc043f868925.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 Clean cherry-pick from 1dcc0c82394ec9cd6887c7ca39f9b5024db01ac9 Change-Id: I5223caa7d42f4582a982609a898a02043265c6d3
Diffstat (limited to 'libutils/tests')
-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 */);
+}
+
}