[PATCH] D33149: Avoid a UB pointer overflow in the ArrayRef unit test

Phabricator via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed May 31 14:48:14 PDT 2017


This revision was automatically updated to reflect the committed changes.
Closed by commit rL304353: Avoid a UB pointer overflow in the ArrayRef unit test (authored by vedantk).

Changed prior to commit:
  https://reviews.llvm.org/D33149?vs=98824&id=100923#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D33149

Files:
  llvm/trunk/unittests/ADT/ArrayRefTest.cpp


Index: llvm/trunk/unittests/ADT/ArrayRefTest.cpp
===================================================================
--- llvm/trunk/unittests/ADT/ArrayRefTest.cpp
+++ llvm/trunk/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,32 @@
   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) {
@@ -187,13 +194,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.100923.patch
Type: text/x-patch
Size: 2288 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170531/04523421/attachment.bin>


More information about the llvm-commits mailing list