Fix out-of-bounds in an [unused] utility.

This was caught by ASAN.

Our algorithm header has many one-liners that compute the "next power of
two length / 2" but they all have the caveat that if the input is
already POT, then the "/ 2" part does not occur.

Usually we deal with this by testing the difference against zero.
However in `partition_point` we were skipping the test, thus causing a
potential out-of-bounds access.

I fixed `partition_point` and added a few more tests for non-POT cases.
This commit is contained in:
Philip Rideout
2019-11-05 14:56:10 -08:00
parent 4f540fe7fd
commit 87a05f057a
2 changed files with 10 additions and 1 deletions

View File

@@ -234,4 +234,12 @@ TEST(AlgorithmTest, Partition) {
r = utils::partition_point(std::begin(array), std::end(array), [](int i) { return i < 2; });
EXPECT_EQ(std::begin(array), r);
int array7[7] = { 2, 5, 4, 8, 9, 9, 9 };
r = utils::partition_point(std::begin(array7), std::end(array7), [](int i) { return i < 9; });
EXPECT_EQ(4, r - std::begin(array7));
int array9[9] = { 2, 5, 4, 8, 9, 9, 9, 9, 9 };
r = utils::partition_point(std::begin(array9), std::end(array9), [](int i) { return i < 9; });
EXPECT_EQ(4, r - std::begin(array9));
}