Fix Module::FindTypes to return the correct number of matches.

In r331719, I changed Module::FindTypes not to limit the amount
of types returned by the Symbol provider, because we want all
possible matches to be able to filter them. In one code path,
the filtering was applied to the TypeList without changing the
number of types that gets returned. This is turn could cause
consumers to access beyond the end of the TypeList.

This patch fixes this case and also adds an assertion to
TypeList::GetTypeAtIndex to catch those obvious programming
mistakes.

Triggering the condition in which we performed the incorrect
access was not easy. It happened a lot in mixed Swift/ObjectiveC
code, but I was able to trigger it in pure Objective C++ although
in a contrieved way.

rdar://problem/40254997

llvm-svn: 333786
This commit is contained in:
Frederic Riss 2018-06-01 20:14:21 +00:00
parent 1943b49c99
commit 9cd4def1c6
6 changed files with 43 additions and 0 deletions

View File

@ -0,0 +1,7 @@
LEVEL = ../../../make
OBJCXX_SOURCES := main.mm myobject.mm
include $(LEVEL)/Makefile.rules
# myobject.o needs to be built without debug info
myobject.o: myobject.mm
$(CXX) -c -o $@ $<

View File

@ -0,0 +1,6 @@
from lldbsuite.test import decorators
from lldbsuite.test import lldbinline
lldbinline.MakeInlineTest(
__file__, globals(), [
decorators.skipIfFreeBSD, decorators.skipIfLinux, decorators.skipIfWindows])

View File

@ -0,0 +1,21 @@
#import <Foundation/Foundation.h>
namespace NS {
class MyObject { int i = 42; };
NS::MyObject globalObject;
}
@interface MyObject: NSObject
@end
int main ()
{
@autoreleasepool
{
MyObject *o = [MyObject alloc];
return 0; //% self.expect("fr var o", DATA_TYPES_DISPLAYED_CORRECTLY, substrs = ["(MyObject"]);
//% self.expect("fr var globalObject", DATA_TYPES_DISPLAYED_CORRECTLY, substrs = ["42"]);
}
}

View File

@ -0,0 +1,7 @@
#import <Foundation/Foundation.h>
@interface MyObject : NSObject
@end
@implementation MyObject
@end

View File

@ -1030,6 +1030,7 @@ size_t Module::FindTypes(
std::string name_str(name.AsCString(""));
typesmap.RemoveMismatchedTypes(type_scope, name_str, type_class,
exact_match);
num_matches = typesmap.GetSize();
}
}
}

View File

@ -77,6 +77,7 @@ uint32_t TypeList::GetSize() const { return m_types.size(); }
TypeSP TypeList::GetTypeAtIndex(uint32_t idx) {
iterator pos, end;
uint32_t i = idx;
assert(i < GetSize() && "Accessing past the end of a TypeList");
for (pos = m_types.begin(), end = m_types.end(); pos != end; ++pos) {
if (i == 0)
return *pos;