Determine if a function is called with a specific value as parameter based on the value of another variable #60
-
|
Hi - I’m pretty new to CodeQL and I haven’t been able to determine if a case I have in mind can be written as a CodeQL query. I want to write a query that for a specific function (validate in the example) returns if there are cases where c will be called if a is false. Example code that the query should find and return Example code that is correct and the query should not mark and return Is it possible to write such a query? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
|
Hi, It sounds like to you can use Here's a query that flags the first example but not the second: import javascript
Function validateFn() {
result.getName() = "validate"
}
ConditionGuardNode guardNode() {
result.getTest() = validateFn().getParameterByName("a").getVariable().getAnAccess() and
result.getOutcome() = false
}
predicate isGuarded(ControlFlowNode node) {
guardNode().dominates(node.getBasicBlock())
}
CallExpr sensitiveCall() {
result.getCallee() = validateFn().getParameterByName("c").getVariable().getAnAccess()
}
from CallExpr call
where call = sensitiveCall()
and not isGuarded(call)
select call |
Beta Was this translation helpful? Give feedback.
Hi,
It sounds like to you can use
ConditionGuardNode. It's a control-flow node that occurs in places where it's known that a certain expression is true or false. We can use.dominates()to check whether it dominates another node ("dominates" means all paths to the other node must first go through the guard node).Here's a query that flags the first example but not the second: