From 391bc59e8dd70db4f05bd183f2b96b94663bb822 Mon Sep 17 00:00:00 2001 From: Bert Date: Mon, 1 May 2017 14:11:45 +0200 Subject: [PATCH] Avoid exceptions - IllegalStateException not handled - remove illegal characters from path data --- .../org/sonar/cxx/sensors/visitors/CxxCpdVisitor.java | 9 ++++++--- .../main/java/org/sonar/cxx/CxxVCppBuildLogParser.java | 9 ++++++++- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/cxx-sensors/src/main/java/org/sonar/cxx/sensors/visitors/CxxCpdVisitor.java b/cxx-sensors/src/main/java/org/sonar/cxx/sensors/visitors/CxxCpdVisitor.java index 7cd979332a..4b3ce68a77 100644 --- a/cxx-sensors/src/main/java/org/sonar/cxx/sensors/visitors/CxxCpdVisitor.java +++ b/cxx-sensors/src/main/java/org/sonar/cxx/sensors/visitors/CxxCpdVisitor.java @@ -101,11 +101,14 @@ public void visitToken(Token token) { } try { - TextRange range = inputFile.newRange(token.getLine(), token.getColumn(), token.getLine(), token.getColumn() + token.getValue().length()); + TextRange range = inputFile.newRange(token.getLine(), token.getColumn(), + token.getLine(), token.getColumn() + token.getValue().length()); cpdTokens.addToken(range, text); - } catch (IllegalArgumentException e) { //NOSONAR + } catch (IllegalArgumentException|IllegalStateException e) { // ignore range errors: parsing errors could lead to wrong location data - LOG.debug("CPD error in file '{}' at line:{}, column:{}", getContext().getFile().getAbsoluteFile(), token.getLine(), token.getColumn()); + LOG.warn("CPD error in file '{}' at line:{}, column:{}", getContext().getFile().getAbsoluteFile(), + token.getLine(), token.getColumn()); + LOG.debug("CPD error in file {} {}", getContext().getFile().getAbsoluteFile(), e); } } } diff --git a/cxx-squid/src/main/java/org/sonar/cxx/CxxVCppBuildLogParser.java b/cxx-squid/src/main/java/org/sonar/cxx/CxxVCppBuildLogParser.java index cf38005050..de1a505d32 100644 --- a/cxx-squid/src/main/java/org/sonar/cxx/CxxVCppBuildLogParser.java +++ b/cxx-squid/src/main/java/org/sonar/cxx/CxxVCppBuildLogParser.java @@ -154,8 +154,15 @@ public void parseVCppLog(File buildLog, String baseDir, String charsetName) { * @param data */ private void parseCLParameters(String line, Path currentProjectPath, String data) { + String path = data.replaceAll("\"", ""); + String fileElement; try { - String fileElement = Paths.get(currentProjectPath.toAbsolutePath().toString(), data).toAbsolutePath().toString(); + if (!path.isEmpty() && path.matches("^[a-zA-Z]:.*$")) { + // do not add project path if data is not a relative path + fileElement = Paths.get(path).toAbsolutePath().toString(); + } else { + fileElement = Paths.get(currentProjectPath.toAbsolutePath().toString(), path).toAbsolutePath().toString(); + } if (!uniqueDefines.containsKey(fileElement)) { uniqueDefines.put(fileElement, new HashSet());