Skip to content

Commit

Permalink
Avoid a UB pointer overflow in the ArrayRef unit test
Browse files Browse the repository at this point in the history
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

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@304353 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
vedantk committed May 31, 2017
1 parent 3412991 commit b527f09
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions unittests/ADT/ArrayRefTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -80,26 +81,32 @@ 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) {
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) {
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit b527f09

Please sign in to comment.