Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4d5f158

Browse files
committed
C++: Pivot ReturnKind solution to derive types from SSA + AST, rather than SSA + MAD.
1 parent 13734d4 commit 4d5f158

2 files changed

Lines changed: 17 additions & 23 deletions

File tree

cpp/ql/lib/semmle/code/cpp/dataflow/internal/FlowSummaryImpl.qll

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -262,25 +262,3 @@ module Private {
262262
}
263263

264264
module Public = Impl::Public;
265-
266-
/**
267-
* Gets a number of indirections that can be returned by a function
268-
* modelled using models-as-data.
269-
*/
270-
int returnIndirectionForModelledFunction() {
271-
exists(string inputOutput |
272-
(
273-
sourceModel(_, _, _, _, _, _, inputOutput, _, _) or
274-
sinkModel(_, _, _, _, _, _, inputOutput, _, _) or
275-
summaryModel(_, _, _, _, _, _, inputOutput, _, _, _) or
276-
summaryModel(_, _, _, _, _, _, _, inputOutput, _, _)
277-
) and (
278-
// Return the number of stars in `ReturnValue[...]`
279-
result = inputOutput.regexpCapture("ReturnValue\\[(\\*+)\\]", 1).length()
280-
or
281-
// There are no brackets the result is 0
282-
inputOutput = "ReturnValue" and
283-
result = 0
284-
)
285-
)
286-
}

cpp/ql/lib/semmle/code/cpp/ir/dataflow/internal/DataFlowPrivate.qll

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,16 +457,32 @@ newtype TPosition =
457457

458458
private newtype TReturnKind =
459459
TNormalReturnKind(int indirectionIndex) {
460+
// derive a possible return indirection from SSA
461+
// (this is a more durable approach if SSA infers additional indirections for any reason)
460462
Ssa::hasIndirectOperand(any(ReturnValueInstruction ret).getReturnAddressOperand(),
461463
indirectionIndex + 1) // We subtract one because the return loads the value.
462464
or
463-
indirectionIndex = FlowSummaryImpl::returnIndirectionForModelledFunction()
465+
// derive a possible return kind from the AST
466+
// (this approach includes functions declared that have no body; they may still have flow summaries)
467+
indirectionIndex =
468+
[0 .. max(Ssa::Function f |
469+
|
470+
Ssa::getMaxIndirectionsForType(f.getUnspecifiedType()) - 1 // -1 because a returned value is a prvalue not a glvalue
471+
)]
464472
} or
465473
TIndirectReturnKind(int argumentIndex, int indirectionIndex) {
474+
// derive a possible return argument from SSA
466475
exists(Ssa::FinalParameterUse use |
467476
use.getIndirectionIndex() = indirectionIndex and
468477
use.getArgumentIndex() = argumentIndex
469478
)
479+
or
480+
// derive a possible return argument from the AST
481+
indirectionIndex =
482+
[0 .. max(Ssa::Function f |
483+
|
484+
Ssa::getMaxIndirectionsForType(f.getParameter(argumentIndex).getUnspecifiedType()) - 1 // -1 because an argument is a prvalue not a glvalue
485+
)]
470486
}
471487

472488
/**

0 commit comments

Comments
 (0)