Skip to content

Commit

Permalink
Fix array out bounds error for native git blame (#1253)
Browse files Browse the repository at this point in the history
  • Loading branch information
kirill-knize-sonarsource authored Feb 24, 2025
1 parent 302ea1c commit 9531ffb
Showing 1 changed file with 18 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 ";
Expand All @@ -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;
}

Expand All @@ -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()) {
Expand Down

0 comments on commit 9531ffb

Please sign in to comment.