diff options
author | Christopher Ferris <cferris@google.com> | 2017-04-03 12:39:47 -0700 |
---|---|---|
committer | Christopher Ferris <cferris@google.com> | 2017-04-03 18:36:33 -0700 |
commit | f447c8eb205d899085968a0a8dfae861ef56a589 (patch) | |
tree | b7cbe68673663760db5330d33c2fd75d2651fcbe /libunwindstack/tests/MemoryRangeTest.cpp | |
parent | 8aae1b158ad45bd3a7eeace9558ee7390b12fa94 (diff) |
Add overflow checks in Memory objects.
Also change one of the reads to be explicitly ReadField instead of an
overloaded Read function.
Bug: 23762183
Test: Passes new unit tests.
Change-Id: Id848f7b632f67df0c5b7318d9e588942cfd2099a
Diffstat (limited to 'libunwindstack/tests/MemoryRangeTest.cpp')
-rw-r--r-- | libunwindstack/tests/MemoryRangeTest.cpp | 34 |
1 files changed, 7 insertions, 27 deletions
diff --git a/libunwindstack/tests/MemoryRangeTest.cpp b/libunwindstack/tests/MemoryRangeTest.cpp index d636ec497..ee5ba018b 100644 --- a/libunwindstack/tests/MemoryRangeTest.cpp +++ b/libunwindstack/tests/MemoryRangeTest.cpp @@ -17,6 +17,7 @@ #include <stdint.h> #include <string.h> +#include <memory> #include <vector> #include <gtest/gtest.h> @@ -65,35 +66,14 @@ TEST_F(MemoryRangeTest, read_near_limit) { ASSERT_FALSE(range.Read(1020, dst.data(), 5)); ASSERT_FALSE(range.Read(1024, dst.data(), 1)); ASSERT_FALSE(range.Read(1024, dst.data(), 1024)); -} - -TEST_F(MemoryRangeTest, read_string_past_end) { - std::string name("0123456789"); - memory_->SetMemory(0, name); - - // Verify a read past the range fails. - MemoryRange range(memory_, 0, 5); - std::string dst_name; - ASSERT_FALSE(range.ReadString(0, &dst_name)); -} - -TEST_F(MemoryRangeTest, read_string_to_end) { - std::string name("0123456789"); - memory_->SetMemory(30, name); - // Verify the range going to the end of the string works. - MemoryRange range(memory_, 30, 30 + name.size() + 1); - std::string dst_name; - ASSERT_TRUE(range.ReadString(0, &dst_name)); - ASSERT_EQ("0123456789", dst_name); + // Verify that reading up to the end works. + ASSERT_TRUE(range.Read(1020, dst.data(), 4)); } -TEST_F(MemoryRangeTest, read_string_fencepost) { - std::string name("0123456789"); - memory_->SetMemory(10, name); +TEST_F(MemoryRangeTest, read_overflow) { + std::vector<uint8_t> buffer(100); - // Verify the range set to one byte less than the end of the string fails. - MemoryRange range(memory_, 10, 10 + name.size()); - std::string dst_name; - ASSERT_FALSE(range.ReadString(0, &dst_name)); + std::unique_ptr<MemoryRange> overflow(new MemoryRange(new MemoryFakeAlwaysReadZero, 100, 200)); + ASSERT_FALSE(overflow->Read(UINT64_MAX - 10, buffer.data(), 100)); } |