[PATCH] D33149: Avoid a UB pointer overflow in the ArrayRef unit test
Vedant Kumar via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Fri May 12 13:08:51 PDT 2017
vsk created this revision.
The intent of the test is to check that array lengths greater than
UINT_MAX work properly. Change the test to stress that scenario, without
triggering pointer overflow UB.
Caught by a WIP pointer overflow checker in clang.
https://reviews.llvm.org/D33149
Files:
unittests/ADT/ArrayRefTest.cpp
Index: unittests/ADT/ArrayRefTest.cpp
===================================================================
--- unittests/ADT/ArrayRefTest.cpp
+++ unittests/ADT/ArrayRefTest.cpp
@@ -11,6 +11,7 @@
#include "llvm/Support/Allocator.h"
#include "llvm/Support/raw_ostream.h"
#include "gtest/gtest.h"
+#include <limits>
#include <vector>
using namespace llvm;
@@ -80,26 +81,34 @@
EXPECT_NE(makeArrayRef(Array3Src).data(), Array3Copy.data());
}
+#if defined(__LP64__) && __LP64__
+TEST(ArrayRefTest, SizeTSizedOperations) {
+ ArrayRef<char> AR(nullptr, std::numeric_limits<uint32_t>::max() + 2);
+
+ // Check that drop_back accepts size_t-sized numbers.
+ EXPECT_EQ(1U, AR.drop_back(AR.size() - 1).size());
+
+ // Check that drop_front accepts size_t-sized numbers.
+ EXPECT_EQ(1U, AR.drop_front(AR.size() - 1).size());
+
+ // Check that slice accepts size_t-sized numbers.
+ EXPECT_EQ(1U, AR.slice(AR.size() - 1).size());
+ EXPECT_EQ(AR.size() - 1, AR.slice(1, AR.size() - 1).size());
+}
+#endif
+
TEST(ArrayRefTest, DropBack) {
static const int TheNumbers[] = {4, 8, 15, 16, 23, 42};
ArrayRef<int> AR1(TheNumbers);
ArrayRef<int> AR2(TheNumbers, AR1.size() - 1);
EXPECT_TRUE(AR1.drop_back().equals(AR2));
-
- // Check that drop_back accepts size_t-sized numbers.
- ArrayRef<char> AR3((const char *)0x10000, SIZE_MAX - 0x10000);
- EXPECT_EQ(1U, AR3.drop_back(AR3.size() - 1).size());
}
TEST(ArrayRefTest, DropFront) {
static const int TheNumbers[] = {4, 8, 15, 16, 23, 42};
ArrayRef<int> AR1(TheNumbers);
ArrayRef<int> AR2(&TheNumbers[2], AR1.size() - 2);
EXPECT_TRUE(AR1.drop_front(2).equals(AR2));
-
- // Check that drop_front accepts size_t-sized numbers.
- ArrayRef<char> AR3((const char *)0x10000, SIZE_MAX - 0x10000);
- EXPECT_EQ(1U, AR3.drop_front(AR3.size() - 1).size());
}
TEST(ArrayRefTest, DropWhile) {
@@ -187,13 +196,6 @@
EXPECT_TRUE(ArrayRef<unsigned>() == ArrayRef<unsigned>());
}
-TEST(ArrayRefTest, Slice) {
- // Check that slice accepts size_t-sized numbers.
- ArrayRef<char> AR((const char *)0x10000, SIZE_MAX - 0x10000);
- EXPECT_EQ(1U, AR.slice(AR.size() - 1).size());
- EXPECT_EQ(AR.size() - 1, AR.slice(1, AR.size() - 1).size());
-}
-
TEST(ArrayRefTest, ConstConvert) {
int buf[4];
for (int i = 0; i < 4; ++i)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D33149.98824.patch
Type: text/x-patch
Size: 2301 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170512/e7bab12f/attachment.bin>
More information about the llvm-commits
mailing list