Skip to content

Commit ce63358

Browse files
committed
Python: Move ModificationOfParameterWithDefault to new dataflow API
1 parent e8e8d97 commit ce63358

File tree

3 files changed

+37
-6
lines changed

3 files changed

+37
-6
lines changed

python/ql/src/Functions/ModificationOfParameterWithDefault.ql

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313

1414
import python
1515
import semmle.python.functions.ModificationOfParameterWithDefault
16-
import DataFlow::PathGraph
16+
import ModificationOfParameterWithDefault::Flow::PathGraph
1717

1818
from
19-
ModificationOfParameterWithDefault::Configuration config, DataFlow::PathNode source,
20-
DataFlow::PathNode sink
21-
where config.hasFlowPath(source, sink)
19+
ModificationOfParameterWithDefault::Flow::PathNode source,
20+
ModificationOfParameterWithDefault::Flow::PathNode sink
21+
where ModificationOfParameterWithDefault::Flow::flowPath(source, sink)
2222
select sink.getNode(), source, sink, "This expression mutates a $@.", source.getNode(),
2323
"default value"

python/ql/src/semmle/python/functions/ModificationOfParameterWithDefault.qll

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ module ModificationOfParameterWithDefault {
1616
import ModificationOfParameterWithDefaultCustomizations::ModificationOfParameterWithDefault
1717

1818
/**
19+
* DEPRECATED: Use `Flow` module instead.
20+
*
1921
* A data-flow configuration for detecting modifications of a parameters default value.
2022
*/
21-
class Configuration extends DataFlow::Configuration {
23+
deprecated class Configuration extends DataFlow::Configuration {
2224
/** Record whether the default value being tracked is non-empty. */
2325
boolean nonEmptyDefault;
2426

@@ -43,4 +45,33 @@ module ModificationOfParameterWithDefault {
4345
nonEmptyDefault = false and node instanceof MustBeNonEmpty
4446
}
4547
}
48+
49+
private module Config implements DataFlow::StateConfigSig {
50+
class FlowState = boolean;
51+
52+
predicate isSource(DataFlow::Node source, FlowState state) {
53+
source.(Source).isNonEmpty() = state
54+
}
55+
56+
predicate isSink(DataFlow::Node sink) { sink instanceof Sink }
57+
58+
predicate isSink(DataFlow::Node sink, FlowState state) {
59+
// dummy implementation since this predicate is required, but actual logic is in
60+
// the predicate above.
61+
none()
62+
}
63+
64+
predicate isBarrier(DataFlow::Node node, FlowState state) {
65+
// if we are tracking a non-empty default, then it is ok to modify empty values,
66+
// so our tracking ends at those.
67+
state = true and node instanceof MustBeEmpty
68+
or
69+
// if we are tracking a empty default, then it is ok to modify non-empty values,
70+
// so our tracking ends at those.
71+
state = false and node instanceof MustBeNonEmpty
72+
}
73+
}
74+
75+
/** Global data-flow for detecting modifications of a parameters default value. */
76+
module Flow = DataFlow::GlobalWithState<Config>;
4677
}

python/ql/test/query-tests/Functions/ModificationOfParameterWithDefault/test.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module ModificationOfParameterWithDefaultTest implements TestSig {
88
string getARelevantTag() { result = "modification" }
99

1010
private predicate relevant_node(DataFlow::Node sink) {
11-
exists(ModificationOfParameterWithDefault::Configuration cfg | cfg.hasFlowTo(sink))
11+
ModificationOfParameterWithDefault::Flow::flowTo(sink)
1212
}
1313

1414
predicate hasActualResult(Location location, string element, string tag, string value) {

0 commit comments

Comments
 (0)