make the extractor not crash on invalid "package.json" files

This commit is contained in:
Erik Krogh Kristensen
2020-08-24 12:42:08 +02:00
parent 1b655f9046
commit d633410e3c
8 changed files with 95 additions and 1 deletions

View File

@@ -9,6 +9,7 @@ import java.util.concurrent.ConcurrentMap;
import java.util.Optional;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.semmle.js.extractor.ExtractorConfig.Platform;
import com.semmle.js.extractor.ExtractorConfig.SourceType;
@@ -128,7 +129,7 @@ public class ScriptExtractor implements IExtractor {
String result = new Gson().fromJson(reader, PackageJSON.class).type;
packageTypeCache.put(folder, Optional.ofNullable(result));
return result;
} catch (IOException e) {
} catch (IOException | JsonSyntaxException e) {
return null;
}
}

View File

@@ -0,0 +1,3 @@
// I'm invalid JSON
{
"type": "foo"

View File

@@ -0,0 +1,3 @@
{
"type": 123
}

View File

@@ -0,0 +1,25 @@
#10000=@"/invalid2/package.json;sourcefile"
files(#10000,"/invalid2/package.json","package","json",0)
#10001=@"/invalid2;folder"
folders(#10001,"/invalid2","invalid2")
#10002=@"/;folder"
folders(#10002,"/","")
containerparent(#10002,#10001)
containerparent(#10001,#10000)
#10003=@"loc,{#10000},0,0,0,0"
locations_default(#10003,#10000,0,0,0,0)
hasLocation(#10000,#10003)
#20000=*
json(#20000,5,#10000,0,"{\n ""type"": 123\n}")
#20001=@"loc,{#10000},1,1,3,1"
locations_default(#20001,#10000,1,1,3,1)
json_locations(#20000,#20001)
#20002=*
json(#20002,2,#20000,0,"123")
#20003=@"loc,{#10000},2,11,2,13"
locations_default(#20003,#10000,2,11,2,13)
json_locations(#20002,#20003)
json_literals("123","123",#20002)
json_properties(#20000,"type",#20002)
numlines(#10000,3,0,0)
filetype(#10000,"json")

View File

@@ -0,0 +1,31 @@
#10000=@"/invalid1/tst.js;sourcefile"
files(#10000,"/invalid1/tst.js","tst","js",0)
#10001=@"/invalid1;folder"
folders(#10001,"/invalid1","invalid1")
#10002=@"/;folder"
folders(#10002,"/","")
containerparent(#10002,#10001)
containerparent(#10001,#10000)
#10003=@"loc,{#10000},0,0,0,0"
locations_default(#10003,#10000,0,0,0,0)
hasLocation(#10000,#10003)
#20000=@"global_scope"
scopes(#20000,0)
#20001=@"script;{#10000},1,1"
numlines(#20001,0,0,0)
#20002=*
tokeninfo(#20002,0,#20001,0,"")
#20003=@"loc,{#10000},1,1,1,0"
locations_default(#20003,#10000,1,1,1,0)
hasLocation(#20002,#20003)
toplevels(#20001,0)
hasLocation(#20001,#20003)
#20004=*
entry_cfg_node(#20004,#20001)
hasLocation(#20004,#20003)
#20005=*
exit_cfg_node(#20005,#20001)
hasLocation(#20005,#20003)
successor(#20004,#20005)
numlines(#10000,0,0,0)
filetype(#10000,"javascript")

View File

@@ -0,0 +1,31 @@
#10000=@"/invalid2/tst2.js;sourcefile"
files(#10000,"/invalid2/tst2.js","tst2","js",0)
#10001=@"/invalid2;folder"
folders(#10001,"/invalid2","invalid2")
#10002=@"/;folder"
folders(#10002,"/","")
containerparent(#10002,#10001)
containerparent(#10001,#10000)
#10003=@"loc,{#10000},0,0,0,0"
locations_default(#10003,#10000,0,0,0,0)
hasLocation(#10000,#10003)
#20000=@"global_scope"
scopes(#20000,0)
#20001=@"script;{#10000},1,1"
numlines(#20001,0,0,0)
#20002=*
tokeninfo(#20002,0,#20001,0,"")
#20003=@"loc,{#10000},1,1,1,0"
locations_default(#20003,#10000,1,1,1,0)
hasLocation(#20002,#20003)
toplevels(#20001,0)
hasLocation(#20001,#20003)
#20004=*
entry_cfg_node(#20004,#20001)
hasLocation(#20004,#20003)
#20005=*
exit_cfg_node(#20005,#20001)
hasLocation(#20005,#20003)
successor(#20004,#20005)
numlines(#10000,0,0,0)
filetype(#10000,"javascript")