String16::remove - avoid overflow
Bug: 156999009
Test: libutils_test (cases added)
Change-Id: Iad46d95d9848928ba81000090b2fe9aec1e5eaac
diff --git a/libutils/String16.cpp b/libutils/String16.cpp
index d514f29..70bf5a0 100644
--- a/libutils/String16.cpp
+++ b/libutils/String16.cpp
@@ -441,7 +441,7 @@
mString = getEmptyString();
return OK;
}
- if ((begin+len) > N) len = N-begin;
+ if (len > N || len > N - begin) len = N - begin;
if (begin == 0 && len == N) {
return OK;
}
diff --git a/libutils/String16_test.cpp b/libutils/String16_test.cpp
index f1f24c3..ae9ba89 100644
--- a/libutils/String16_test.cpp
+++ b/libutils/String16_test.cpp
@@ -90,6 +90,13 @@
EXPECT_STR16EQ(u"VerifyInsert me", tmp);
}
+TEST(String16Test, RemoveDefault) {
+ String16 tmp("Verify me");
+ tmp.remove(4);
+ EXPECT_EQ(4U, tmp.size());
+ EXPECT_STR16EQ(u"Veri", tmp);
+}
+
TEST(String16Test, Remove) {
String16 tmp("Verify me");
tmp.remove(2, 6);
@@ -97,6 +104,13 @@
EXPECT_STR16EQ(u" m", tmp);
}
+TEST(String16Test, RemoveOutOfBounds) {
+ String16 tmp("Verify me");
+ tmp.remove(100, 6);
+ EXPECT_EQ(3U, tmp.size());
+ EXPECT_STR16EQ(u" me", tmp);
+}
+
TEST(String16Test, MakeLower) {
String16 tmp("Verify Me!");
tmp.makeLower();