diff --git a/src/processor/range_map-inl.h b/src/processor/range_map-inl.h index e7bf4512..3aa2603a 100644 --- a/src/processor/range_map-inl.h +++ b/src/processor/range_map-inl.h @@ -184,7 +184,7 @@ bool RangeMap::RetrieveRangeAtIndex( *entry = iterator->second.entry(); if (entry_base) - *entry_base = iterator->first; + *entry_base = iterator->second.base(); if (entry_size) *entry_size = iterator->first - iterator->second.base() + 1; diff --git a/src/processor/range_map_unittest.cc b/src/processor/range_map_unittest.cc index dd713fba..88e93be4 100644 --- a/src/processor/range_map_unittest.cc +++ b/src/processor/range_map_unittest.cc @@ -325,6 +325,43 @@ static bool RetrieveIndexTest(TestMap *range_map, int set) { return true; } +// Additional RetriveAtIndex test to expose the bug in RetrieveRangeAtIndex(). +// Bug info: RetrieveRangeAtIndex() previously retrieves the high address of +// entry, however, it is supposed to retrieve the base address of entry as +// stated in the comment in range_map.h. +static bool RetriveAtIndexTest2() { + scoped_ptr range_map(new TestMap()); + + // Store ranges with base address = 2 * object_id: + const int range_size = 2; + for (int object_id = 0; object_id < 100; ++object_id) { + linked_ptr object(new CountedObject(object_id)); + int base_address = 2 * object_id; + range_map->StoreRange(base_address, range_size, object); + } + + linked_ptr object; + int object_count = range_map->GetCount(); + for (int object_index = 0; object_index < object_count; ++object_index) { + AddressType base; + if (!range_map->RetrieveRangeAtIndex(object_index, &object, &base, NULL)) { + fprintf(stderr, "FAILED: RetrieveAtIndexTest2 index %d, " + "expected success, observed failure\n", object_index); + return false; + } + + int expected_base = 2 * object->id(); + if (base != expected_base) { + fprintf(stderr, "FAILED: RetriveAtIndexTest2 index %d, " + "expected base %d, observed base %d", + object_index, expected_base, base); + return false; + } + } + + return true; +} + // RunTests runs a series of test sets. static bool RunTests() { @@ -497,6 +534,11 @@ static bool RunTests() { } } + if (!RetriveAtIndexTest2()) { + fprintf(stderr, "FAILED: did not pass RetrieveAtIndexTest2()\n"); + return false; + } + return true; }