From 9531ffb3ed16abe19d3aefe35a2f4dd4f782f4b5 Mon Sep 17 00:00:00 2001 From: Knize <62898885+kirill-knize-sonarsource@users.noreply.github.com> Date: Mon, 24 Feb 2025 11:09:10 +0100 Subject: [PATCH] Fix array out bounds error for native git blame (#1253) --- .../core/commons/util/git/BlameParser.java | 23 +++++++++++++++---- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/backend/commons/src/main/java/org/sonarsource/sonarlint/core/commons/util/git/BlameParser.java b/backend/commons/src/main/java/org/sonarsource/sonarlint/core/commons/util/git/BlameParser.java index 4e92f411d2..2442930983 100644 --- a/backend/commons/src/main/java/org/sonarsource/sonarlint/core/commons/util/git/BlameParser.java +++ b/backend/commons/src/main/java/org/sonarsource/sonarlint/core/commons/util/git/BlameParser.java @@ -29,9 +29,10 @@ import java.util.regex.Pattern; import org.sonar.scm.git.blame.BlameResult; import org.sonarsource.sonarlint.core.commons.SonarLintBlameResult; +import org.sonarsource.sonarlint.core.commons.log.SonarLintLogger; public class BlameParser { - + private static final SonarLintLogger LOG = SonarLintLogger.get(); private static final String FILENAME = "filename "; private static final String AUTHOR_MAIL = "author-mail "; private static final String COMMITTER_TIME = "committer-time "; @@ -43,13 +44,14 @@ private BlameParser() { public static SonarLintBlameResult parseBlameOutput(String blameOutput, String currentFilePath, Path projectBaseDir) throws IOException { var blameResult = new BlameResult(); - var currentFileBlame = new BlameResult.FileBlame(currentFilePath, countCommitterTimeOccurrences(blameOutput)); + var numberOfLines = numberOfLinesInBlameOutput(blameOutput); + var currentFileBlame = new BlameResult.FileBlame(currentFilePath, numberOfLines); blameResult.getFileBlameByPath().put(currentFilePath, currentFileBlame); var fileSections = blameOutput.split(FILENAME); var currentLineNumber = 0; for (var fileSection : fileSections) { - if (fileSection.isBlank()) { + if (shouldSkipSection(fileSection, currentLineNumber >= numberOfLines)) { continue; } @@ -73,8 +75,19 @@ public static SonarLintBlameResult parseBlameOutput(String blameOutput, String c return new SonarLintBlameResult(blameResult, projectBaseDir); } - public static int countCommitterTimeOccurrences(String blameOutput) { - var pattern = Pattern.compile("^" + COMMITTER_TIME, Pattern.MULTILINE); + private static boolean shouldSkipSection(String fileSection, boolean lineNumberIsOff) { + if (fileSection.isBlank()) { + return true; + } + if (lineNumberIsOff) { + LOG.warn("Number of blame output sections is higher than expected number of lines. Section content: {}", fileSection); + return true; + } + return false; + } + + public static int numberOfLinesInBlameOutput(String blameOutput) { + var pattern = Pattern.compile("^" + FILENAME, Pattern.MULTILINE); var matcher = pattern.matcher(blameOutput); var count = 0; while (matcher.find()) {