Skip to content

Commit b17518a

Browse files
author
Max Schaefer
committed
JavaScript: Refactor InconsistentNew to improve performance.
All the filtering is now done in `getALikelyCallee`, to which I have also added an additional parameter that improves the join in the `select` clause. I've also simplified the alert message to no longer use `toString`, which isn't meant for alert messages anyway. (This is an old query.)
1 parent 595e6fc commit b17518a

File tree

2 files changed

+42
-21
lines changed

2 files changed

+42
-21
lines changed

javascript/ql/src/LanguageFeatures/InconsistentNew.ql

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -56,26 +56,47 @@ predicate calls(DataFlow::InvokeNode cs, Function callee, int imprecision) {
5656

5757
/**
5858
* Gets a function that may be invoked at `cs`, preferring callees that
59-
* are less likely to be derived due to analysis imprecision.
59+
* are less likely to be derived due to analysis imprecision and excluding
60+
* whitelisted call sites and callees. Additionally, `isNew` is bound to
61+
* `true` if `cs` is a `new` expression, and to `false` otherwise.
6062
*/
61-
Function getALikelyCallee(DataFlow::InvokeNode cs) {
62-
calls(cs, result, min(int p | calls(cs, _, p)))
63+
Function getALikelyCallee(DataFlow::InvokeNode cs, boolean isNew) {
64+
calls(cs, result, min(int p | calls(cs, _, p))) and
65+
not cs.isUncertain() and
66+
not whitelistedCall(cs) and
67+
not whitelistedCallee(result) and
68+
(cs instanceof DataFlow::NewNode and isNew = true
69+
or
70+
cs instanceof DataFlow::CallNode and isNew = false)
71+
}
72+
73+
/**
74+
* Holds if `f` should be whitelisted, either because it guards against
75+
* inconsistent `new` or we do not want to report it.
76+
*/
77+
predicate whitelistedCallee(Function f) {
78+
// externs are special, so don't flag them
79+
f.inExternsFile() or
80+
// illegal constructor calls are flagged by query 'Illegal invocation',
81+
// so don't flag them
82+
f instanceof Constructor or
83+
// if `f` itself guards against missing `new`, don't flag it
84+
guardsAgainstMissingNew(f)
85+
}
86+
87+
/**
88+
* Holds if `call` should be whitelisted because it cannot cause problems
89+
* with inconsistent `new`.
90+
*/
91+
predicate whitelistedCall(DataFlow::CallNode call) {
92+
// super constructor calls behave more like `new`, so don't flag them
93+
call.asExpr() instanceof SuperCall or
94+
// don't flag if there is a receiver object
95+
exists(call.getReceiver())
6396
}
6497

6598
from Function f, DataFlow::NewNode new, DataFlow::CallNode call
66-
where // externs are special, so don't flag them
67-
not f.inExternsFile() and
68-
// illegal constructor calls are flagged by query 'Illegal invocation',
69-
// so don't flag them
70-
not f instanceof Constructor and
71-
f = getALikelyCallee(new) and
72-
f = getALikelyCallee(call) and
73-
not guardsAgainstMissingNew(f) and
74-
not new.isUncertain() and
75-
not call.isUncertain() and
76-
// super constructor calls behave more like `new`, so don't flag them
77-
not call.asExpr() instanceof SuperCall and
78-
// don't flag if there is a receiver object
79-
not exists(call.getReceiver())
80-
select (FirstLineOf)f, capitalize(f.describe()) + " is invoked as a constructor here $@, " +
81-
"and as a normal function here $@.", new, new.toString(), call, call.toString()
99+
where f = getALikelyCallee(new, true) and
100+
f = getALikelyCallee(call, false)
101+
select (FirstLineOf)f, capitalize(f.describe()) + " is invoked as a constructor $@, " +
102+
"and as a normal function $@.", new, "here", call, "here"
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
| m.js:1:8:1:22 | functio ... = x;\\n} | Function A is invoked as a constructor here $@, and as a normal function here $@. | c1.js:2:1:2:9 | new A(42) | new A(42) | c2.js:2:1:2:5 | A(23) | A(23) |
2-
| tst.js:1:1:1:22 | functio ... = y;\\n} | Function Point is invoked as a constructor here $@, and as a normal function here $@. | tst.js:6:1:6:17 | new Point(23, 42) | new Point(23, 42) | tst.js:7:1:7:13 | Point(56, 72) | Point(56, 72) |
1+
| m.js:1:8:1:22 | functio ... = x;\\n} | Function A is invoked as a constructor $@, and as a normal function $@. | c1.js:2:1:2:9 | new A(42) | here | c2.js:2:1:2:5 | A(23) | here |
2+
| tst.js:1:1:1:22 | functio ... = y;\\n} | Function Point is invoked as a constructor $@, and as a normal function $@. | tst.js:6:1:6:17 | new Point(23, 42) | here | tst.js:7:1:7:13 | Point(56, 72) | here |

0 commit comments

Comments
 (0)