[PATCH] D21629: [libcxx] [test] Add assertions to quiet analysis warnings about array bounds.

Stephan T. Lavavej via cfe-commits cfe-commits at lists.llvm.org
Wed Jun 22 16:57:28 PDT 2016


STL_MSFT created this revision.
STL_MSFT added reviewers: EricWF, mclow.lists.
STL_MSFT added a subscriber: cfe-commits.

Add assertions to quiet analysis warnings about array bounds.

In the partial_sort tests, the N >= M assertion is just a good sanity check, to detect bogus testcases. MSVC's /analyze needs to see the i < N assertion explicitly, otherwise it worries that array[i] might be out-of-bounds. I gave those ones comments because they are technically redundant.

In eval.pass.cpp, the assertions simply say that we aren't going to try to access arrays out-of-bounds, which I assume is guaranteed by the logic of the test, but this is far from obvious, so the assertions are valuable in their own right.

Fixes MSVC warnings of the form:
warning C6385: Reading invalid data from 'array':  the readable size is 'N*4' bytes, but '8' bytes may be read.
These warnings are valuable, so I'd prefer to suppress them locally instead of globally, especially given that so few places are affected.

http://reviews.llvm.org/D21629

Files:
  test/std/algorithms/alg.sorting/alg.sort/partial.sort/partial_sort.pass.cpp
  test/std/algorithms/alg.sorting/alg.sort/partial.sort/partial_sort_comp.pass.cpp
  test/std/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/eval.pass.cpp

Index: test/std/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/eval.pass.cpp
===================================================================
--- test/std/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/eval.pass.cpp
+++ test/std/numerics/rand/rand.dis/rand.dist.samp/rand.dist.samp.plinear/eval.pass.cpp
@@ -243,6 +243,7 @@
             a = 0;
             for (int j = 0; j < k; ++j)
                 a += areas[j];
+            assert(k < Np);
             m = (p[k+1] - p[k]) / (b[k+1] - b[k]);
             bk = b[k];
             c = (b[k+1]*p[k] - b[k]*p[k+1]) / (b[k+1] - b[k]);
@@ -281,6 +282,7 @@
     double S = 0;
     for (int i = 0; i < areas.size(); ++i)
     {
+        assert(i < Np);
         areas[i] = (p[i]+p[i+1])*(b[i+1]-b[i])/2;
         S += areas[i];
     }
@@ -296,6 +298,7 @@
             a = 0;
             for (int j = 0; j < k; ++j)
                 a += areas[j];
+            assert(k < Np);
             m = (p[k+1] - p[k]) / (b[k+1] - b[k]);
             bk = b[k];
             c = (b[k+1]*p[k] - b[k]*p[k+1]) / (b[k+1] - b[k]);
Index: test/std/algorithms/alg.sorting/alg.sort/partial.sort/partial_sort_comp.pass.cpp
===================================================================
--- test/std/algorithms/alg.sorting/alg.sort/partial.sort/partial_sort_comp.pass.cpp
+++ test/std/algorithms/alg.sorting/alg.sort/partial.sort/partial_sort_comp.pass.cpp
@@ -35,13 +35,17 @@
 test_larger_sorts(unsigned N, unsigned M)
 {
     assert(N != 0);
+    assert(N >= M);
     int* array = new int[N];
     for (int i = 0; i < N; ++i)
         array[i] = i;
     std::random_shuffle(array, array+N);
     std::partial_sort(array, array+M, array+N, std::greater<int>());
     for (int i = 0; i < M; ++i)
+    {
+        assert(i < N); // quiet analysis warnings
         assert(array[i] == N-i-1);
+    }
     delete [] array;
 }
 
Index: test/std/algorithms/alg.sorting/alg.sort/partial.sort/partial_sort.pass.cpp
===================================================================
--- test/std/algorithms/alg.sorting/alg.sort/partial.sort/partial_sort.pass.cpp
+++ test/std/algorithms/alg.sorting/alg.sort/partial.sort/partial_sort.pass.cpp
@@ -22,13 +22,17 @@
 test_larger_sorts(unsigned N, unsigned M)
 {
     assert(N != 0);
+    assert(N >= M);
     int* array = new int[N];
     for (int i = 0; i < N; ++i)
         array[i] = i;
     std::random_shuffle(array, array+N);
     std::partial_sort(array, array+M, array+N);
     for (int i = 0; i < M; ++i)
+    {
+        assert(i < N); // quiet analysis warnings
         assert(array[i] == i);
+    }
     delete [] array;
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D21629.61627.patch
Type: text/x-patch
Size: 2642 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160622/1aac114d/attachment.bin>


More information about the cfe-commits mailing list