Skip to content

Commit d633410

Browse files
committed
make the extractor not crash on invalid "package.json" files
1 parent 1b655f9 commit d633410

File tree

8 files changed

+95
-1
lines changed

8 files changed

+95
-1
lines changed

javascript/extractor/src/com/semmle/js/extractor/ScriptExtractor.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.util.Optional;
1010

1111
import com.google.gson.Gson;
12+
import com.google.gson.JsonSyntaxException;
1213

1314
import com.semmle.js.extractor.ExtractorConfig.Platform;
1415
import com.semmle.js.extractor.ExtractorConfig.SourceType;
@@ -128,7 +129,7 @@ private String getPackageType(File folder) {
128129
String result = new Gson().fromJson(reader, PackageJSON.class).type;
129130
packageTypeCache.put(folder, Optional.ofNullable(result));
130131
return result;
131-
} catch (IOException e) {
132+
} catch (IOException | JsonSyntaxException e) {
132133
return null;
133134
}
134135
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// I'm invalid JSON
2+
{
3+
"type": "foo"

javascript/extractor/tests/moduleTypes/input/invalid1/tst.js

Whitespace-only changes.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"type": 123
3+
}

javascript/extractor/tests/moduleTypes/input/invalid2/tst2.js

Whitespace-only changes.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#10000=@"/invalid2/package.json;sourcefile"
2+
files(#10000,"/invalid2/package.json","package","json",0)
3+
#10001=@"/invalid2;folder"
4+
folders(#10001,"/invalid2","invalid2")
5+
#10002=@"/;folder"
6+
folders(#10002,"/","")
7+
containerparent(#10002,#10001)
8+
containerparent(#10001,#10000)
9+
#10003=@"loc,{#10000},0,0,0,0"
10+
locations_default(#10003,#10000,0,0,0,0)
11+
hasLocation(#10000,#10003)
12+
#20000=*
13+
json(#20000,5,#10000,0,"{\n ""type"": 123\n}")
14+
#20001=@"loc,{#10000},1,1,3,1"
15+
locations_default(#20001,#10000,1,1,3,1)
16+
json_locations(#20000,#20001)
17+
#20002=*
18+
json(#20002,2,#20000,0,"123")
19+
#20003=@"loc,{#10000},2,11,2,13"
20+
locations_default(#20003,#10000,2,11,2,13)
21+
json_locations(#20002,#20003)
22+
json_literals("123","123",#20002)
23+
json_properties(#20000,"type",#20002)
24+
numlines(#10000,3,0,0)
25+
filetype(#10000,"json")
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#10000=@"/invalid1/tst.js;sourcefile"
2+
files(#10000,"/invalid1/tst.js","tst","js",0)
3+
#10001=@"/invalid1;folder"
4+
folders(#10001,"/invalid1","invalid1")
5+
#10002=@"/;folder"
6+
folders(#10002,"/","")
7+
containerparent(#10002,#10001)
8+
containerparent(#10001,#10000)
9+
#10003=@"loc,{#10000},0,0,0,0"
10+
locations_default(#10003,#10000,0,0,0,0)
11+
hasLocation(#10000,#10003)
12+
#20000=@"global_scope"
13+
scopes(#20000,0)
14+
#20001=@"script;{#10000},1,1"
15+
numlines(#20001,0,0,0)
16+
#20002=*
17+
tokeninfo(#20002,0,#20001,0,"")
18+
#20003=@"loc,{#10000},1,1,1,0"
19+
locations_default(#20003,#10000,1,1,1,0)
20+
hasLocation(#20002,#20003)
21+
toplevels(#20001,0)
22+
hasLocation(#20001,#20003)
23+
#20004=*
24+
entry_cfg_node(#20004,#20001)
25+
hasLocation(#20004,#20003)
26+
#20005=*
27+
exit_cfg_node(#20005,#20001)
28+
hasLocation(#20005,#20003)
29+
successor(#20004,#20005)
30+
numlines(#10000,0,0,0)
31+
filetype(#10000,"javascript")
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#10000=@"/invalid2/tst2.js;sourcefile"
2+
files(#10000,"/invalid2/tst2.js","tst2","js",0)
3+
#10001=@"/invalid2;folder"
4+
folders(#10001,"/invalid2","invalid2")
5+
#10002=@"/;folder"
6+
folders(#10002,"/","")
7+
containerparent(#10002,#10001)
8+
containerparent(#10001,#10000)
9+
#10003=@"loc,{#10000},0,0,0,0"
10+
locations_default(#10003,#10000,0,0,0,0)
11+
hasLocation(#10000,#10003)
12+
#20000=@"global_scope"
13+
scopes(#20000,0)
14+
#20001=@"script;{#10000},1,1"
15+
numlines(#20001,0,0,0)
16+
#20002=*
17+
tokeninfo(#20002,0,#20001,0,"")
18+
#20003=@"loc,{#10000},1,1,1,0"
19+
locations_default(#20003,#10000,1,1,1,0)
20+
hasLocation(#20002,#20003)
21+
toplevels(#20001,0)
22+
hasLocation(#20001,#20003)
23+
#20004=*
24+
entry_cfg_node(#20004,#20001)
25+
hasLocation(#20004,#20003)
26+
#20005=*
27+
exit_cfg_node(#20005,#20001)
28+
hasLocation(#20005,#20003)
29+
successor(#20004,#20005)
30+
numlines(#10000,0,0,0)
31+
filetype(#10000,"javascript")

0 commit comments

Comments
 (0)