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] Fixed fullNames for function problem bindings #5280

Merged
merged 2 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -167,8 +167,11 @@ trait FullNameProvider { this: AstCreator =>
safeGetBinding(declarator.getName) match {
case Some(value: ICPPMethod) =>
cleanType(value.getType.getReturnType.toString)
case _ =>
case _ if declarator.getParent.isInstanceOf[IASTSimpleDeclaration] =>
cleanType(typeForDeclSpecifier(declarator.getParent.asInstanceOf[IASTSimpleDeclaration].getDeclSpecifier))
case _ if declarator.getParent.isInstanceOf[IASTFunctionDefinition] =>
cleanType(typeForDeclSpecifier(declarator.getParent.asInstanceOf[IASTFunctionDefinition].getDeclSpecifier))
case _ => Defines.Any
}
}

Expand Down Expand Up @@ -330,10 +333,15 @@ trait FullNameProvider { this: AstCreator =>
case Some(_: IProblemBinding) =>
val fullNameNoSig = replaceOperator(ASTStringUtil.getQualifiedName(declarator.getName))
val fixedFullName = fixQualifiedName(fullNameNoSig)
val returnTpe = declarator.getParent match {
case definition: ICPPASTFunctionDefinition if !isCppConstructor(definition) => returnType(definition)
case _ => returnType(declarator)
}
val signature_ = signature(returnTpe, declarator)
if (fixedFullName.isEmpty) {
Option(s"${X2CpgDefines.UnresolvedNamespace}:${X2CpgDefines.UnresolvedSignature}")
Option(s"${X2CpgDefines.UnresolvedNamespace}:$signature_")
} else {
Option(s"$fixedFullName:${X2CpgDefines.UnresolvedSignature}")
Option(s"$fixedFullName:$signature_")
}
case _ => None
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,31 @@ class AstCreationPassTests extends AstC2CpgSuite {
}
}

"be correct for full names and signatures for method problem bindings" in {
val cpg = code(
"""
|char tpe<wchar_t>::foo(char_type a, char b) const {
| return static_cast<char>(a);
|}
|const wchar_t* tpe<wchar_t>::foo(const char_type* a, const char_type* b, char c, char* d) const {
| return a;
|}
|""".stripMargin,
"foo.cpp"
)
// tpe<wchar_t> can't be resolved for both methods resulting in problem bindings.
// We can however manually reconstruct the signature from the params and return type without
// relying on the resolved function binding signature.
val fullNames = cpg.method.nameNot("<global>").map(m => (m.fullName, m.signature)).l.sortBy(_._1)
fullNames shouldBe List(
("tpe<wchar_t>.foo:char(char_type,char)", "char(char_type,char)"),
(
"tpe<wchar_t>.foo:const wchar_t*(char_type*,char_type*,char,char*)",
"const wchar_t*(char_type*,char_type*,char,char*)"
max-leuthaeuser marked this conversation as resolved.
Show resolved Hide resolved
)
)
}

"be correct for packed args" in {
val cpg = code(
"""
Expand Down
Loading