Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[c2cpg] Guard instanceOf casts #5154

Merged
merged 1 commit into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ trait AstForExpressionsCreator(implicit withSchemaValidation: ValidationMode) {
val functionNameExpr = call.getFunctionNameExpression
val typ = functionNameExpr.getExpressionType
typ match {
case pointerType: IPointerType =>
case _: IPointerType =>
createPointerCallAst(call, cleanType(safeGetType(call.getExpressionType)))
case functionType: ICPPFunctionType =>
functionNameExpr match {
case idExpr: CPPASTIdExpression if idExpr.getName.getBinding.isInstanceOf[ICPPFunction] =>
case idExpr: CPPASTIdExpression if idExpr.getName.resolveBinding().isInstanceOf[ICPPFunction] =>
val function = idExpr.getName.getBinding.asInstanceOf[ICPPFunction]
maltek marked this conversation as resolved.
Show resolved Hide resolved
val name = idExpr.getName.getLastName.toString
val signature =
Expand Down Expand Up @@ -115,7 +115,8 @@ trait AstForExpressionsCreator(implicit withSchemaValidation: ValidationMode) {
val args = call.getArguments.toList.map(a => astForNode(a))

createCallAst(callCpgNode, args)
case fieldRefExpr: ICPPASTFieldReference =>
case fieldRefExpr: ICPPASTFieldReference
if fieldRefExpr.getFieldName.resolveBinding().isInstanceOf[ICPPMethod] =>
val instanceAst = astForExpression(fieldRefExpr.getFieldOwner)
val args = call.getArguments.toList.map(a => astForNode(a))

Expand All @@ -126,7 +127,6 @@ trait AstForExpressionsCreator(implicit withSchemaValidation: ValidationMode) {
val classFullName = cleanType(safeGetType(fieldRefExpr.getFieldOwnerType))
val fullName = s"$classFullName.$name:$signature"

fieldRefExpr.getFieldName.resolveBinding()
val method = fieldRefExpr.getFieldName.getBinding.asInstanceOf[ICPPMethod]
val (dispatchType, receiver) =
if (method.isVirtual || method.isPureVirtual) {
Expand All @@ -144,18 +144,18 @@ trait AstForExpressionsCreator(implicit withSchemaValidation: ValidationMode) {
Some(registerType(cleanType(safeGetType(call.getExpressionType))))
)
createCallAst(callCpgNode, args, base = Some(instanceAst), receiver)
case other =>
case _ =>
astForCppCallExpressionUntyped(call)
}
case classType: ICPPClassType =>
case classType: ICPPClassType if call.getEvaluation.isInstanceOf[EvalFunctionCall] =>
val evaluation = call.getEvaluation.asInstanceOf[EvalFunctionCall]

val functionType = Try(evaluation.getOverload.getType).toOption
val signature = functionType.map(functionTypeToSignature).getOrElse(X2CpgDefines.UnresolvedSignature)
val name = Defines.OperatorCall

classType match {
case closureType: CPPClosureType =>
case _: CPPClosureType =>
val fullName = s"$name:$signature"
val dispatchType = DispatchTypes.DYNAMIC_DISPATCH

Expand Down Expand Up @@ -205,15 +205,13 @@ trait AstForExpressionsCreator(implicit withSchemaValidation: ValidationMode) {
astForCppCallExpressionUntyped(call)
case _: IProblemBinding =>
astForCppCallExpressionUntyped(call)
case other =>
case _ =>
astForCppCallExpressionUntyped(call)
}
}

private def astForCppCallExpressionUntyped(call: ICPPASTFunctionCallExpression): Ast = {
val functionNameExpr = call.getFunctionNameExpression

functionNameExpr match {
call.getFunctionNameExpression match {
case fieldRefExpr: ICPPASTFieldReference =>
val instanceAst = astForExpression(fieldRefExpr.getFieldOwner)
val args = call.getArguments.toList.map(a => astForNode(a))
Expand Down Expand Up @@ -249,9 +247,9 @@ trait AstForExpressionsCreator(implicit withSchemaValidation: ValidationMode) {
Some(X2CpgDefines.Any)
)
createCallAst(callCpgNode, args)
case other =>
case otherExpr =>
// This could either be a pointer or an operator() call we do not know at this point
// but since it is CPP we opt for the later.
// but since it is CPP we opt for the latter.
val args = call.getArguments.toList.map(a => astForNode(a))

val name = Defines.OperatorCall
Expand All @@ -267,7 +265,7 @@ trait AstForExpressionsCreator(implicit withSchemaValidation: ValidationMode) {
Some(signature),
Some(X2CpgDefines.Any)
)
val instanceAst = astForExpression(functionNameExpr)
val instanceAst = astForExpression(otherExpr)
createCallAst(callCpgNode, args, base = Some(instanceAst), receiver = Some(instanceAst))
}
}
Expand All @@ -276,9 +274,9 @@ trait AstForExpressionsCreator(implicit withSchemaValidation: ValidationMode) {
val functionNameExpr = call.getFunctionNameExpression
val typ = functionNameExpr.getExpressionType
typ match {
case pointerType: CPointerType =>
case _: CPointerType =>
createPointerCallAst(call, cleanType(safeGetType(call.getExpressionType)))
case functionType: CFunctionType =>
case _: CFunctionType =>
functionNameExpr match {
case idExpr: CASTIdExpression =>
createCFunctionCallAst(call, idExpr, cleanType(safeGetType(call.getExpressionType)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ trait AstForPrimitivesCreator(implicit withSchemaValidation: ValidationMode) { t
val variableOption = scope.lookupVariable(identifierName)
variableOption match {
case Some((_, variableTypeName)) => variableTypeName
case None if ident.isInstanceOf[IASTName] && ident.asInstanceOf[IASTName].getBinding != null =>
case None if ident.isInstanceOf[IASTName] && ident.asInstanceOf[IASTName].resolveBinding() != null =>
val id = ident.asInstanceOf[IASTName]
id.getBinding match {
case v: IVariable =>
Expand Down
Loading