C#: Correct printing of out and ref notes in the model generator.

This commit is contained in:
Michael Nebel
2025-03-25 15:24:13 +01:00
parent bcefdc8893
commit 8763d18c91
2 changed files with 19 additions and 1 deletions

View File

@@ -233,6 +233,10 @@ module ModelGeneratorInput implements ModelGeneratorInputSig<Location, CsharpDat
result = ParamReturnNodeAsOutput<parameterContentAccess/1>::paramReturnNodeAsOutput(c, pos)
}
ParameterPosition getReturnKindParamPosition(ReturnKind kind) {
kind.(OutRefReturnKind).getPosition() = result.getPosition()
}
Callable returnNodeEnclosingCallable(DataFlow::Node ret) {
result = DataFlowImplCommon::getNodeEnclosingCallable(ret).asCallable(_)
}

View File

@@ -221,6 +221,11 @@ signature module ModelGeneratorInputSig<LocationSig Location, InputSig<Location>
*/
string printContent(Lang::ContentSet c);
/**
* Gets the parameter position of the return kind, if any.
*/
default Lang::ParameterPosition getReturnKindParamPosition(Lang::ReturnKind node) { none() }
/**
* Holds if it is irrelevant to generate models for `api` based on data flow analysis.
*
@@ -301,6 +306,14 @@ module MakeModelGenerator<
* Gets the kind of the return node.
*/
DataFlow::ReturnKindExt getKind() { result = kind }
/**
* Gets the parameter position of the return node, if any.
*/
DataFlow::ParameterPosition getPosition() {
result = this.getKind().(DataFlow::ParamUpdateReturnKind).getPosition() or
result = getReturnKindParamPosition(this.getKind().(DataFlow::ValueReturnKind).getKind())
}
}
bindingset[c]
@@ -309,10 +322,11 @@ module MakeModelGenerator<
private module PrintReturnNodeExt<printCallableParamSig/2 printCallableParam> {
string getOutput(ReturnNodeExt node) {
node.getKind() instanceof DataFlow::ValueReturnKind and
not exists(node.getPosition()) and
result = "ReturnValue"
or
exists(DataFlow::ParameterPosition pos |
pos = node.getKind().(DataFlow::ParamUpdateReturnKind).getPosition() and
pos = node.getPosition() and
result = printCallableParam(returnNodeEnclosingCallable(node), pos)
)
}