Skip to content

Commit

Permalink
C++: Fix crash for invalid code
Browse files Browse the repository at this point in the history
...due to indirect recursion:

  ...
  CPlusPlus::ClassOrNamespace::lookupType        LookupContext.cpp 833  0x7fffd6c954cc
  CPlusPlus::ClassOrNamespace::nestedType        LookupContext.cpp 1364 0x7fffd6c94bc6
  CPlusPlus::ClassOrNamespace::lookupType_helper LookupContext.cpp 955  0x7fffd6c9517f
  CPlusPlus::ClassOrNamespace::lookupType_helper LookupContext.cpp 983  0x7fffd6c952ad
  CPlusPlus::ClassOrNamespace::lookupType        LookupContext.cpp 833  0x7fffd6c954cc
  CPlusPlus::ClassOrNamespace::nestedType        LookupContext.cpp 1364 0x7fffd6c94bc6
  CPlusPlus::ClassOrNamespace::lookupType_helper LookupContext.cpp 955  0x7fffd6c9517f
  CPlusPlus::ClassOrNamespace::lookupType_helper LookupContext.cpp 983  0x7fffd6c952ad
  CPlusPlus::ClassOrNamespace::lookupType        LookupContext.cpp 833  0x7fffd6c954cc
  ...

ClassOrNamespace::lookupType(const Name *) already guards with a list of
entries already processed, but some calls deeper the list is not passed
on and lookupType() starts again with an empty list. Handle that case,
too.

Task-number: QTCREATORBUG-18499
Change-Id: Iab8978f6ac1d0aea16f49b3547415f43de887b07
Reviewed-by: Marco Bubke <[email protected]>
  • Loading branch information
nikolaivk committed Jul 6, 2017
1 parent cfb5f61 commit f51d7a2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
20 changes: 12 additions & 8 deletions src/libs/cplusplus/LookupContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -952,7 +952,7 @@ ClassOrNamespace *ClassOrNamespace::lookupType_helper(const Name *name,
return this;
}

if (ClassOrNamespace *e = nestedType(name, origin))
if (ClassOrNamespace *e = nestedType(name, processed, origin))
return e;

if (_templateId) {
Expand Down Expand Up @@ -1074,7 +1074,9 @@ ClassOrNamespace *ClassOrNamespace::findOrCreateNestedAnonymousType(
}
}

ClassOrNamespace *ClassOrNamespace::nestedType(const Name *name, ClassOrNamespace *origin)
ClassOrNamespace *ClassOrNamespace::nestedType(const Name *name,
QSet<ClassOrNamespace *> *processed,
ClassOrNamespace *origin)
{
Q_ASSERT(name != 0);
Q_ASSERT(name->isNameId() || name->isTemplateNameId() || name->isAnonymousNameId());
Expand Down Expand Up @@ -1184,11 +1186,11 @@ ClassOrNamespace *ClassOrNamespace::nestedType(const Name *name, ClassOrNamespac
instantiation->_name = templId;
instantiation->_templateId = templId;

QSet<ClassOrNamespace *> processed;
QSet<ClassOrNamespace *> otherProcessed;
while (!origin->_symbols.isEmpty() && origin->_symbols[0]->isBlock()) {
if (processed.contains(origin))
if (otherProcessed.contains(origin))
break;
processed.insert(origin);
otherProcessed.insert(origin);
origin = origin->parent();
}

Expand Down Expand Up @@ -1310,7 +1312,7 @@ ClassOrNamespace *ClassOrNamespace::nestedType(const Name *name, ClassOrNamespac
// Another template that uses the dependent name.
// Ex.: template <class T> class A : public B<T> {};
if (baseTemplId->identifier() != templId->identifier())
baseBinding = nestedType(baseName, origin);
baseBinding = nestedType(baseName, processed, origin);
} else if (const QualifiedNameId *qBaseName = baseName->asQualifiedNameId()) {
// Qualified names in general.
// Ex.: template <class T> class A : public B<T>::Type {};
Expand Down Expand Up @@ -1361,7 +1363,8 @@ ClassOrNamespace *ClassOrNamespace::nestedType(const Name *name, ClassOrNamespac
}

if (binding) {
ClassOrNamespace * baseBinding = binding->lookupType(baseName);
ClassOrNamespace * baseBinding
= binding->lookupType_helper(baseName, processed, true, this);
if (baseBinding && !knownUsings.contains(baseBinding))
reference->addUsing(baseBinding);
}
Expand Down Expand Up @@ -1518,7 +1521,8 @@ ClassOrNamespace *ClassOrNamespace::findOrCreateType(const Name *name, ClassOrNa
return findOrCreateType(q->base(), origin)->findOrCreateType(q->name(), origin, clazz);

} else if (name->isNameId() || name->isTemplateNameId() || name->isAnonymousNameId()) {
ClassOrNamespace *e = nestedType(name, origin);
QSet<ClassOrNamespace *> processed;
ClassOrNamespace *e = nestedType(name, &processed, origin);

if (! e) {
e = _factory->allocClassOrNamespace(this);
Expand Down
3 changes: 2 additions & 1 deletion src/libs/cplusplus/LookupContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ class CPLUSPLUS_EXPORT ClassOrNamespace
ClassOrNamespace *findBlock_helper(Block *block, QSet<ClassOrNamespace *> *processed,
bool searchInEnclosingScope);

ClassOrNamespace *nestedType(const Name *name, ClassOrNamespace *origin);
ClassOrNamespace *nestedType(const Name *name, QSet<ClassOrNamespace *> *processed,
ClassOrNamespace *origin);

void instantiateNestedClasses(ClassOrNamespace *enclosingTemplateClass,
Clone &cloner,
Expand Down

0 comments on commit f51d7a2

Please sign in to comment.