From f7685933bcb77359e5b14c62da94fa2d75c43e1c Mon Sep 17 00:00:00 2001 From: Christian Banse Date: Tue, 7 Sep 2021 20:16:04 +0200 Subject: [PATCH] Fixed NPE in TypeManager (#554) --- .../aisec/cpg/graph/TypeManager.java | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/cpg-library/src/main/java/de/fraunhofer/aisec/cpg/graph/TypeManager.java b/cpg-library/src/main/java/de/fraunhofer/aisec/cpg/graph/TypeManager.java index 6586426d7f..fd1635a6e2 100644 --- a/cpg-library/src/main/java/de/fraunhofer/aisec/cpg/graph/TypeManager.java +++ b/cpg-library/src/main/java/de/fraunhofer/aisec/cpg/graph/TypeManager.java @@ -99,7 +99,13 @@ public enum Language { // as Values private Set firstOrderTypes = Collections.synchronizedSet(new HashSet<>()); private Set secondOrderTypes = Collections.synchronizedSet(new HashSet<>()); + + /** + * The language frontend that is currently active. This can be null, e.g. if we are executed in tests. + */ + @org.jetbrains.annotations.Nullable private LanguageFrontend frontend; + private boolean noFrontendWarningIssued = false; public static void reset() { @@ -515,7 +521,7 @@ public Language getLanguage() { return Language.TYPESCRIPT; } - log.error("Unknown language (frontend: {})", frontend.getClass().getSimpleName()); + log.error("Unknown language (frontend: {})", frontend != null ? frontend.getClass().getSimpleName() : null); return Language.UNKNOWN; } @@ -677,6 +683,15 @@ public void handleSingleAlias(String rawCode, Type target, String aliasString) { } TypedefDeclaration typedef = NodeBuilder.newTypedefDeclaration(currTarget, alias, rawCode); + + if (frontend == null) { + if (!noFrontendWarningIssued) { + log.warn("No frontend available. Be aware that typedef resolving cannot currently be done"); + noFrontendWarningIssued = true; + } + return; + } + frontend.getScopeManager().addTypedef(typedef); } @@ -688,6 +703,7 @@ public Type resolvePossibleTypedef(Type alias) { } return alias; } + Type toCheck = alias.getRoot(); Type finalToCheck = toCheck;