Skip to content

Commit

Permalink
SONAR-3442 Fix possible ArrayIndexOutOfBoundsException
Browse files Browse the repository at this point in the history
  • Loading branch information
Godin authored and simonbrandhof committed May 10, 2012
1 parent b847d1c commit 0def1fc
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
Expand Up @@ -40,6 +40,14 @@ private ViolationTrackingBlocksRecognizer(StringText a, StringText b, StringText
this.cmp = new HashedSequenceComparator<StringText>(cmp);
}

public boolean isValidLineInReference(int line) {
return (0 <= line) && (line < a.length());
}

public boolean isValidLineInSource(int line) {
return (0 <= line) && (line < b.length());
}

/**
* @param startA number of line from first version of text (numbering starts from 0)
* @param startB number of line from second version of text (numbering starts from 0)
Expand Down
Expand Up @@ -124,9 +124,9 @@ Map<Violation, RuleFailureModel> mapViolations(List<Violation> newViolations, Li

List<ViolationPair> possiblePairs = Lists.newArrayList();
for (Violation newViolation : newViolations) {
if (newViolation.getLineId() != null) {
if (newViolation.getLineId() != null && rec.isValidLineInSource(newViolation.getLineId() - 1)) {
for (RuleFailureModel pastViolation : pastViolationsByRule.get(newViolation.getRule().getId())) {
if (pastViolation.getLine() != null) {
if (pastViolation.getLine() != null && rec.isValidLineInReference(pastViolation.getLine() - 1)) {
int weight = rec.computeLengthOfMaximalBlock(pastViolation.getLine() - 1, newViolation.getLineId() - 1);
possiblePairs.add(new ViolationPair(pastViolation, newViolation, weight));
}
Expand Down
Expand Up @@ -31,7 +31,10 @@ public HashedSequenceComparator(SequenceComparator<? super S> cmp) {
}

public boolean equals(HashedSequence<S> a, int ai, HashedSequence<S> b, int bi) {
return a.hashes[ai] == b.hashes[bi] && cmp.equals(a.base, ai, b.base, bi);
if (a.hashes[ai] == b.hashes[bi]) {
return cmp.equals(a.base, ai, b.base, bi);
}
return false;
}

public int hash(HashedSequence<S> seq, int i) {
Expand Down

0 comments on commit 0def1fc

Please sign in to comment.