[llvm] r304353 - Avoid a UB pointer overflow in the ArrayRef unit test
Vedant Kumar via llvm-commits
llvm-commits at lists.llvm.org
Wed May 31 14:47:52 PDT 2017
Author: vedantk
Date: Wed May 31 16:47:52 2017
New Revision: 304353
URL: http://llvm.org/viewvc/llvm-project?rev=304353&view=rev
Log:
Avoid a UB pointer overflow in the ArrayRef unit test
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.
Differential Revision: https://reviews.llvm.org/D33149
Modified:
llvm/trunk/unittests/ADT/ArrayRefTest.cpp
Modified: llvm/trunk/unittests/ADT/ArrayRefTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/ArrayRefTest.cpp?rev=304353&r1=304352&r2=304353&view=diff
==============================================================================
--- llvm/trunk/unittests/ADT/ArrayRefTest.cpp (original)
+++ llvm/trunk/unittests/ADT/ArrayRefTest.cpp Wed May 31 16:47:52 2017
@@ -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,15 +81,25 @@ TEST(ArrayRefTest, AllocatorCopy) {
EXPECT_NE(makeArrayRef(Array3Src).data(), Array3Copy.data());
}
+TEST(ArrayRefTest, SizeTSizedOperations) {
+ ArrayRef<char> AR(nullptr, std::numeric_limits<ptrdiff_t>::max());
+
+ // 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());
+}
+
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) {
@@ -96,10 +107,6 @@ TEST(ArrayRefTest, DropFront) {
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 +194,6 @@ TEST(ArrayRefTest, EmptyEquals) {
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)
More information about the llvm-commits
mailing list