[kdevelop/4.7] languages/cpp/cppduchain: Fix places with undefined behavior

Kevin Funk kfunk at kde.org
Mon Mar 21 23:40:11 UTC 2016


Git commit ca8d751a20b5d1e7b028af39e78547fc0e9d696b by Kevin Funk.
Committed on 21/03/2016 at 23:39.
Pushed by kfunk into branch '4.7'.

Fix places with undefined behavior

New API: IndexedType::IndexedType(const AbstractType::Ptr& type)

Places where we may call AbstractPtr::indexed with this being a nullptr.

Similar patches probably need to be applied to other language plugins

CCMAIL: kdevelop-devel at kde.org
CCBUG: 360707

M  +3    -1    languages/cpp/cppduchain/cppducontext.h
M  +3    -3    languages/cpp/cppduchain/declarationbuilder.cpp
M  +1    -1    languages/cpp/cppduchain/expressionparser.cpp
M  +2    -2    languages/cpp/cppduchain/name_visitor.cpp
M  +2    -2    languages/cpp/cppduchain/templatedeclaration.cpp
M  +3    -3    languages/cpp/cppduchain/typeconversion.cpp
M  +2    -2    languages/cpp/cppduchain/typeutils.cpp

http://commits.kde.org/kdevelop/ca8d751a20b5d1e7b028af39e78547fc0e9d696b

diff --git a/languages/cpp/cppduchain/cppducontext.h b/languages/cpp/cppduchain/cppducontext.h
index 81d0f5b..d87b883 100644
--- a/languages/cpp/cppduchain/cppducontext.h
+++ b/languages/cpp/cppduchain/cppducontext.h
@@ -340,7 +340,9 @@ class CppDUContext : public BaseContext {
               DelayedType::Ptr delayed( new DelayedType() );
               delayed->setIdentifier( i );
               
-              res.type = Cpp::resolveDelayedTypes( delayed.cast<AbstractType>(), this, source, basicFlags & KDevelop::DUContext::NoUndefinedTemplateParams ? DUContext::NoUndefinedTemplateParams : DUContext::NoSearchFlags )->indexed();
+              auto resolved = Cpp::resolveDelayedTypes( delayed.cast<AbstractType>(), this,
+                source, basicFlags & DUContext::NoUndefinedTemplateParams ? DUContext::NoUndefinedTemplateParams : DUContext::NoSearchFlags );
+              res.type = IndexedType(resolved);
               
               if( basicFlags & KDevelop::DUContext::NoUndefinedTemplateParams) {
                 AbstractType::Ptr targetTypePtr = TypeUtils::unAliasedType(TypeUtils::targetType(res.type.abstractType(), 0));
diff --git a/languages/cpp/cppduchain/declarationbuilder.cpp b/languages/cpp/cppduchain/declarationbuilder.cpp
index 538f78f..4ad7d3b 100644
--- a/languages/cpp/cppduchain/declarationbuilder.cpp
+++ b/languages/cpp/cppduchain/declarationbuilder.cpp
@@ -371,10 +371,10 @@ void DeclarationBuilder::findDeclarationForDefinition(const QualifiedIdentifier
   foreach (Declaration* dec, declarations) {
     if (dec->isForwardDeclaration() || dec->isDefinition())
       continue;
-    if (dec->abstractType()->indexed() == lastType()->indexed()) {
+    if (IndexedType(dec->abstractType()) == IndexedType(lastType())) {
       //If this declaration is already assigned to a partial match, unassign it
       if (FunctionDefinition* oldDef = FunctionDefinition::definition(dec)) {
-        if (oldDef->abstractType()->indexed() != dec->abstractType()->indexed())
+        if (IndexedType(oldDef->abstractType()) != IndexedType(dec->abstractType()))
           oldDef->setDeclaration(0);
       }
       funDef->setDeclaration(dec);
@@ -1173,7 +1173,7 @@ void DeclarationBuilder::visitBaseSpecifier(BaseSpecifierAST *node) {
       instance.virtualInheritance = (bool)node->virt;
 
       //TypeUtils::unAliasedType(
-      instance.baseClass = TypeUtils::unAliasedType(lastType())->indexed();
+      instance.baseClass = IndexedType(TypeUtils::unAliasedType(lastType()));
       if(currentClass->classType() == ClassDeclarationData::Struct)
         instance.access = KDevelop::Declaration::Public;
       else
diff --git a/languages/cpp/cppduchain/expressionparser.cpp b/languages/cpp/cppduchain/expressionparser.cpp
index be28328..6b925bc 100644
--- a/languages/cpp/cppduchain/expressionparser.cpp
+++ b/languages/cpp/cppduchain/expressionparser.cpp
@@ -204,7 +204,7 @@ ExpressionEvaluationResult ExpressionParser::evaluateType( AST* ast, ParseSessio
 
   DUChainReadLocker lock(DUChain::lock());
 
-  ret.type = v.lastType()->indexed();
+  ret.type = v.lastType() ? v.lastType()->indexed() : IndexedType();
   ret.isInstance = v.lastInstance().isInstance;
 
   if(v.lastInstance().declaration)
diff --git a/languages/cpp/cppduchain/name_visitor.cpp b/languages/cpp/cppduchain/name_visitor.cpp
index 15ae877..8c9bfba 100644
--- a/languages/cpp/cppduchain/name_visitor.cpp
+++ b/languages/cpp/cppduchain/name_visitor.cpp
@@ -166,7 +166,7 @@ ExpressionEvaluationResult NameASTVisitor::processTemplateArgument(TemplateArgum
     if( m_visitor->lastType() ) {
       LOCKDUCHAIN;
       
-      res.type = m_visitor->lastType()->indexed();
+      res.type = IndexedType(m_visitor->lastType());
       foreach(const DeclarationPointer &decl, m_visitor->lastDeclarations())
         if(decl)
           res.allDeclarationsList().append(decl->id());
@@ -197,7 +197,7 @@ ExpressionEvaluationResult NameASTVisitor::processTemplateArgument(TemplateArgum
       return ExpressionEvaluationResult();
     }
     
-    res.type = v.type()->indexed();
+    res.type = IndexedType(v.type());
     
     if( res.type ) {
       LOCKDUCHAIN;
diff --git a/languages/cpp/cppduchain/templatedeclaration.cpp b/languages/cpp/cppduchain/templatedeclaration.cpp
index d5ea573..aa1ea2f 100644
--- a/languages/cpp/cppduchain/templatedeclaration.cpp
+++ b/languages/cpp/cppduchain/templatedeclaration.cpp
@@ -924,7 +924,7 @@ void applyDefaultParameters(const DUContext* templateContext, const TopDUContext
       Q_ASSERT(!templateDecl->defaultParameter().isEmpty());
       DelayedType::Ptr delayed( new DelayedType() );
       delayed->setIdentifier( IndexedTypeIdentifier(templateDecl->defaultParameter()) );
-      type = resolveDelayedTypes( delayed.cast<AbstractType>(), surroundingContext, source)->indexed();
+      type = IndexedType(resolveDelayedTypes(delayed.cast<AbstractType>(), surroundingContext, source));
     } // else the parameter is missing
 
     //TODO: why is this neccessary?
@@ -1049,7 +1049,7 @@ Declaration* TemplateDeclaration::instantiate( const InstantiationInformation& _
       UnAliasExchanger exchanger(source);
       
       for(uint a = 0; a < templateArguments.templateParametersSize(); ++a)
-        newTemplateArguments.templateParametersList().append(exchanger.exchange(templateArguments.templateParameters()[a].abstractType())->indexed());
+        newTemplateArguments.templateParametersList().append(IndexedType(exchanger.exchange(templateArguments.templateParameters()[a].abstractType())));
       
       templateArguments = newTemplateArguments;
     }
diff --git a/languages/cpp/cppduchain/typeconversion.cpp b/languages/cpp/cppduchain/typeconversion.cpp
index 9eeb127..a944717 100644
--- a/languages/cpp/cppduchain/typeconversion.cpp
+++ b/languages/cpp/cppduchain/typeconversion.cpp
@@ -206,7 +206,7 @@ uint TypeConversion::implicitConversion( const IndexedType& _from, const Indexed
 
       if( realTo->modifiers() & AbstractType::ConstModifier ) {
         //For constant references, the compiler can create a temporary object holding the converted value. So just forget whether the types are references.
-        conv = implicitConversion( realType(from, m_topContext)->indexed(), realType(to, m_topContext)->indexed(), fromLValue, noUserDefinedConversion );
+        conv = implicitConversion( IndexedType(realType(from, m_topContext)), IndexedType(realType(to, m_topContext)), fromLValue, noUserDefinedConversion );
         goto ready;
       }
     }
@@ -361,7 +361,7 @@ ConversionRank TypeConversion::standardConversion( AbstractType::Ptr from, Abstr
     if( isReferenceType(from) ) {
       ///Transform lvalue to rvalue. Iso c++ draft 4.1 modeled roughly
       
-      AbstractType::Ptr fromNonConstant = realType(from, m_topContext)->indexed().abstractType();
+      AbstractType::Ptr fromNonConstant = IndexedType(realType(from, m_topContext)).abstractType();
 
       //When copying, the type becomes non-constant
       if(fromNonConstant && fromNonConstant->modifiers() & AbstractType::ConstModifier)
@@ -388,7 +388,7 @@ ConversionRank TypeConversion::standardConversion( AbstractType::Ptr from, Abstr
       maximizeRank( bestRank, worseRank(rank, ExactMatch ) );
     }else if(from->modifiers() & AbstractType::ConstModifier) {
       ///We can transform a constant lvalue to a non-constant rvalue
-      AbstractType::Ptr fromNonConstant = from->indexed().abstractType();
+      AbstractType::Ptr fromNonConstant = IndexedType(from).abstractType();
       fromNonConstant->setModifiers(fromNonConstant->modifiers() & ~(AbstractType::ConstModifier));
       ConversionRank ret = standardConversion( fromNonConstant, to, removeCategories(categories,LValueTransformationCategory), maxCategories-1 );
       maximizeRank( bestRank, ret );
diff --git a/languages/cpp/cppduchain/typeutils.cpp b/languages/cpp/cppduchain/typeutils.cpp
index bee1c2e..02be6da 100644
--- a/languages/cpp/cppduchain/typeutils.cpp
+++ b/languages/cpp/cppduchain/typeutils.cpp
@@ -234,7 +234,7 @@ KDevelop::AbstractType::Ptr matchingClassPointer(const KDevelop::AbstractType::P
       foreach(Declaration* decl, internal->findDeclarations(Cpp::castIdentifier().identifier(), CursorInRevision::invalid(), topContext, (DUContext::SearchFlags)(DUContext::DontSearchInParent | DUContext::NoFiltering))) {
         FunctionType::Ptr funType = decl->type<FunctionType>();
         if(funType && funType->returnType()) {
-          if(conversion.implicitConversion(funType->returnType()->indexed(), matchTo->indexed(), true)) {
+          if(conversion.implicitConversion(IndexedType(funType->returnType()), IndexedType(matchTo), true)) {
             return funType->returnType();
           }
         }
@@ -292,7 +292,7 @@ IndexedType removeConstModifier(const IndexedType& indexedType)
 {
     AbstractType::Ptr type = indexedType.abstractType();
     removeConstModifier(type);
-    return type->indexed();
+    return IndexedType(type);
 }
 
 void removeConstModifier(AbstractType::Ptr& type)


More information about the KDevelop-devel mailing list