summaryrefslogtreecommitdiffstats
path: root/gerrit-prettify
diff options
context:
space:
mode:
authorEdwin Kempin <edwin.kempin@sap.com>2013-01-24 18:28:32 +0100
committerEdwin Kempin <edwin.kempin@sap.com>2013-01-24 20:27:10 +0100
commit45a3621e4321054db00956ab7c647a793e38f1c5 (patch)
treefb5f0e780c00ad9bd10c7c7724d7a7f7628e8808 /gerrit-prettify
parent65ff2266d094703cfb9cc0632b0878dc4cd820cf (diff)
Fix ArrayIndexOutOfBoundsException on intraline diff
In some cases displaying the intraline diff fails with an exception like this: java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 10 at com.google.gerrit.prettify.common.SparseFileContent.mapIndexToLine(SparseFileContent.java:149) at com.google.gerrit.prettify.common.PrettyFormatter.format(PrettyFormatter.java:188) at com.google.gerrit.client.patches.AbstractPatchContentTable.getSparseHtmlFileB(AbstractPatchContentTable.java:287) at com.google.gerrit.client.patches.SideBySideTable.render(SideBySideTable.java:113) at com.google.gerrit.client.patches.AbstractPatchContentTable.display(AbstractPatchContentTable.java:238) at com.google.gerrit.client.patches.PatchScreen.onResult(PatchScreen.java:444) ... This happens when the old line is: foo-old<LF> and the new line is: foo-new<CRLF> The computed intraline diff that should be highlighted for the new file will be 'new<CR>'. To do the highlighting 'span' tags are inserted which separates <CR> and <LF> for the new file. The prettify parser now replaces both with a 'br' tag. Since the number of 'br' tags is expected to match the number of lines and we now have one 'br' too much we run into the ArrayIndexOutOfBoundsException when looking up the lines. Avoid this problem by removing <CR> from the intraline diff before running the prettify parser. Bug: issue 1759 Change-Id: I738b1c780116b890d8db8bb0ab795c6b2b72128d Signed-off-by: Edwin Kempin <edwin.kempin@sap.com>
Diffstat (limited to 'gerrit-prettify')
-rw-r--r--gerrit-prettify/src/main/java/com/google/gerrit/prettify/common/PrettyFormatter.java15
1 files changed, 15 insertions, 0 deletions
diff --git a/gerrit-prettify/src/main/java/com/google/gerrit/prettify/common/PrettyFormatter.java b/gerrit-prettify/src/main/java/com/google/gerrit/prettify/common/PrettyFormatter.java
index 151149b52e..cccb1acc24 100644
--- a/gerrit-prettify/src/main/java/com/google/gerrit/prettify/common/PrettyFormatter.java
+++ b/gerrit-prettify/src/main/java/com/google/gerrit/prettify/common/PrettyFormatter.java
@@ -135,6 +135,21 @@ public abstract class PrettyFormatter implements SparseHtmlFile {
// confuse the parser.
//
html = html.replaceAll("&#39;", "'");
+
+ // The prettify parser converts all line endings ('\r', '\n' and '\r\n')
+ // into 'br' tags.
+ // If a line is modified at its end and the line ending is changed from
+ // '\n' to '\r\n' then the '\r' of the new line is part of the modified
+ // text. If intraline diffs are highlighted the modified text is
+ // surrounded by a 'span' tag. As result '\r' and '\n' of the new line get
+ // separated by '</span>'. For the prettify parser this now looks like two
+ // separate line endings and 2 'br' tags are inserted. This messes up the
+ // line counting by 'br' tags which is done below, since we now have more
+ // 'br' tags than lines. As result we would run into an
+ // ArrayIndexOutOfBoundsException when trying to lookup the non-existing
+ // lines. Drop the '\r' to avoid this problem.
+ html = html.replace("\r</span>\n", "</span>\n");
+
html = prettify(html, getFileType());
} else {