Skip to content

Commit 1b30a25

Browse files
authored
Merge pull request #1668 from esben-semmle/js/ignore-mocked-callee-argument-count
Approved by xiemaisi
2 parents 108e5bc + 90862fe commit 1b30a25

File tree

4 files changed

+55
-1
lines changed

4 files changed

+55
-1
lines changed

change-notes/1.22/analysis-javascript.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
| **Query** | **Expected impact** | **Change** |
2727
|--------------------------------|------------------------------|---------------------------------------------------------------------------|
2828
| Shift out of range | Fewer false positive results | This rule now correctly handles BigInt shift operands. |
29+
| Superfluous trailing arguments | Fewer false-positive results. | This rule no longer flags calls to placeholder functions that trivially throw an exception. |
2930

3031
## Changes to QL libraries
3132

javascript/ql/src/LanguageFeatures/SpuriousArguments.ql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,5 +94,10 @@ where
9494
f instanceof ArrowFunctionExpr or // cannot be empty
9595
f instanceof ExternalFunction or // always empty
9696
f.isAmbient() // always empty
97+
) and
98+
not (
99+
// exclude no-param functions that trivially throw exceptions, they are probably placeholders
100+
f.getNumParameter() = 0 and
101+
f.getBodyStmt(0) instanceof ThrowStmt
97102
)
98103
select args, "Superfluous " + arguments + " passed to $@.", f, f.describe()

javascript/ql/test/query-tests/LanguageFeatures/SpuriousArguments/SpuriousArguments.expected

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,8 @@
1717
| tst.js:76:31:76:32 | 42 | Superfluous argument passed to $@. | tst.js:64:33:64:32 | () {} | default constructor of class ImplicitEmptyConstructor |
1818
| tst.js:77:31:77:32 | 42 | Superfluous argument passed to $@. | tst.js:67:14:68:3 | (){\\n\\t\\t} | constructor of class ExplicitEmptyConstructor |
1919
| tst.js:78:20:78:21 | 10 | Superfluous argument passed to $@. | externs.js:36:1:36:27 | functio ... num) {} | function parseFloat |
20+
| tst.js:114:20:114:21 | 42 | Superfluous argument passed to $@. | tst.js:82:2:86:2 | functio ... \\n\\t\\t}\\n\\t} | function notAPlainThrower1 |
21+
| tst.js:115:20:115:21 | 42 | Superfluous argument passed to $@. | tst.js:87:2:90:2 | functio ... .");\\n\\t} | function notAPlainThrower2 |
22+
| tst.js:116:20:116:21 | 42 | Superfluous argument passed to $@. | tst.js:91:2:94:2 | functio ... .");\\n\\t} | function notAPlainThrower3 |
23+
| tst.js:120:23:120:24 | 87 | Superfluous argument passed to $@. | tst.js:102:2:104:2 | functio ... (p);\\n\\t} | function throwerWithParam |
24+
| tst.js:121:18:121:19 | 42 | Superfluous argument passed to $@. | tst.js:105:2:113:2 | functio ... )();\\n\\t} | function throwerIndirect |

javascript/ql/test/query-tests/LanguageFeatures/SpuriousArguments/tst.js

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,47 @@ parseFloat("123", 10);
7676
new ImplicitEmptyConstructor(42); // NOT OK
7777
new ExplicitEmptyConstructor(42); // NOT OK
7878
parseFloat("123", 10); // NOT OK
79-
})
79+
});
80+
81+
(function testWhitelistThrowingFunctions() {
82+
function notAPlainThrower1(){
83+
if(DEBUG) {
84+
throw new Error("Remove this statement and implement this function");
85+
}
86+
};
87+
function notAPlainThrower2(){
88+
f();
89+
throw new Error("Internal error: should have thrown an exception before this.");
90+
};
91+
function notAPlainThrower3(){
92+
return;
93+
throw new Error("Internal error: should have returned before this.");
94+
};
95+
function thrower(){
96+
throw new Error("Remove this statement and implement this function");
97+
};
98+
const throwerArrow = () => { throw new Error("Remove this statement and implement this function"); };
99+
function throwerCustom(){
100+
throw new MyError("Remove this statement and implement this function");
101+
};
102+
function throwerWithParam(p){
103+
throw new Error(p);
104+
};
105+
function throwerIndirect(){
106+
(function(){
107+
{
108+
{
109+
throw Error("Remove this statement and implement this function");
110+
}
111+
}
112+
})();
113+
}
114+
notAPlainThrower1(42); // NOT OK
115+
notAPlainThrower2(42); // NOT OK
116+
notAPlainThrower3(42); // NOT OK
117+
thrower(42); // OK
118+
throwerArrow(42); // OK
119+
throwerCustom(42); // OK
120+
throwerWithParam(42, 87); // NOT OK
121+
throwerIndirect(42); // OK, but still flagged due to complexity
122+
});

0 commit comments

Comments
 (0)