summaryrefslogtreecommitdiff
path: root/core/fxcrt/cfx_widestring_unittest.cpp
diff options
context:
space:
mode:
authorRyan Harrison <rharrison@chromium.org>2017-08-11 16:20:32 -0400
committerChromium commit bot <commit-bot@chromium.org>2017-08-11 21:03:14 +0000
commitddb9b7cdd19b63a81c4a094239e85f84acefaa17 (patch)
tree8657940fb10d76a96ffe996cf17d70a1c65ca6de /core/fxcrt/cfx_widestring_unittest.cpp
parentd27998f6526272a5b8732106aa9b75f724434aca (diff)
downloadpdfium-ddb9b7cdd19b63a81c4a094239e85f84acefaa17.tar.xz
Add checks of index operations on string classes
Specifically the index parameter passed in to GetAt(), SetAt() and operator[] are now being tested to be in bounds. BUG=chromium:752480, pdfium:828 Change-Id: I9e94d58c98a8eaaaae53cd0e3ffe2123ea17d8c4 Reviewed-on: https://pdfium-review.googlesource.com/10651 Commit-Queue: Ryan Harrison <rharrison@chromium.org> Reviewed-by: Tom Sepez <tsepez@chromium.org>
Diffstat (limited to 'core/fxcrt/cfx_widestring_unittest.cpp')
-rw-r--r--core/fxcrt/cfx_widestring_unittest.cpp74
1 files changed, 67 insertions, 7 deletions
diff --git a/core/fxcrt/cfx_widestring_unittest.cpp b/core/fxcrt/cfx_widestring_unittest.cpp
index 6e7b63c5fa..a23763218d 100644
--- a/core/fxcrt/cfx_widestring_unittest.cpp
+++ b/core/fxcrt/cfx_widestring_unittest.cpp
@@ -10,13 +10,51 @@
#include "testing/gtest/include/gtest/gtest.h"
+TEST(fxcrt, WideStringGetAt) {
+ CFX_WideString short_string(L"a");
+ CFX_WideString longer_string(L"abc");
+ CFX_WideString embedded_nul_string(L"ab\0c", 4);
+
+#ifndef NDEBUG
+ EXPECT_DEATH({ short_string.GetAt(-1); }, ".*");
+#endif
+ EXPECT_EQ(L'a', short_string.GetAt(0));
+#ifndef NDEBUG
+ EXPECT_DEATH({ short_string.GetAt(1); }, ".*");
+#endif
+ EXPECT_EQ(L'c', longer_string.GetAt(2));
+ EXPECT_EQ(L'b', embedded_nul_string.GetAt(1));
+ EXPECT_EQ(L'\0', embedded_nul_string.GetAt(2));
+ EXPECT_EQ(L'c', embedded_nul_string.GetAt(3));
+}
+
TEST(fxcrt, WideStringOperatorSubscript) {
- // CFX_WideString includes the NUL terminator for non-empty strings.
CFX_WideString abc(L"abc");
+#ifndef NDEBUG
+ EXPECT_DEATH({ abc[-1]; }, ".*");
+#endif
EXPECT_EQ(L'a', abc[0]);
EXPECT_EQ(L'b', abc[1]);
EXPECT_EQ(L'c', abc[2]);
- EXPECT_EQ(L'\0', abc[3]);
+#ifndef NDEBUG
+ EXPECT_DEATH({ abc[4]; }, ".*");
+#endif
+}
+
+TEST(fxcrt, WideStringSetAt) {
+ CFX_WideString abc(L"abc");
+#ifndef NDEBUG
+ EXPECT_DEATH({ abc.SetAt(-1, L'd'); }, ".*");
+#endif
+ abc.SetAt(0, L'd');
+ EXPECT_EQ(L"dbc", abc);
+ abc.SetAt(1, L'e');
+ EXPECT_EQ(L"dec", abc);
+ abc.SetAt(2, L'f');
+ EXPECT_EQ(L"def", abc);
+#ifndef NDEBUG
+ EXPECT_DEATH({ abc.SetAt(3, L'g'); }, ".*");
+#endif
}
TEST(fxcrt, WideStringOperatorLT) {
@@ -742,13 +780,35 @@ TEST(fxcrt, WideStringCFromVector) {
EXPECT_EQ(nullptr, cleared_string.raw_str());
}
+TEST(fxcrt, WideStringCGetAt) {
+ CFX_WideStringC short_string(L"a");
+ CFX_WideStringC longer_string(L"abc");
+ CFX_WideStringC embedded_nul_string(L"ab\0c", 4);
+
+#ifndef NDEBUG
+ EXPECT_DEATH({ short_string.GetAt(-1); }, ".*");
+#endif
+ EXPECT_EQ(L'a', static_cast<wchar_t>(short_string.GetAt(0)));
+#ifndef NDEBUG
+ EXPECT_DEATH({ short_string.GetAt(1); }, ".*");
+#endif
+ EXPECT_EQ(L'c', static_cast<wchar_t>(longer_string.GetAt(2)));
+ EXPECT_EQ(L'b', static_cast<wchar_t>(embedded_nul_string.GetAt(1)));
+ EXPECT_EQ(L'\0', static_cast<wchar_t>(embedded_nul_string.GetAt(2)));
+ EXPECT_EQ(L'c', static_cast<wchar_t>(embedded_nul_string.GetAt(3)));
+}
+
TEST(fxcrt, WideStringCOperatorSubscript) {
- // CFX_WideStringC includes the NUL terminator for non-empty strings.
CFX_WideStringC abc(L"abc");
- EXPECT_EQ(L'a', abc.CharAt(0));
- EXPECT_EQ(L'b', abc.CharAt(1));
- EXPECT_EQ(L'c', abc.CharAt(2));
- EXPECT_EQ(L'\0', abc.CharAt(3));
+#ifndef NDEBUG
+ EXPECT_DEATH({ abc[-1]; }, ".*");
+#endif
+ EXPECT_EQ(L'a', static_cast<wchar_t>(abc[0]));
+ EXPECT_EQ(L'b', static_cast<wchar_t>(abc[1]));
+ EXPECT_EQ(L'c', static_cast<wchar_t>(abc[2]));
+#ifndef NDEBUG
+ EXPECT_DEATH({ abc[4]; }, ".*");
+#endif
}
TEST(fxcrt, WideStringCOperatorLT) {