From ba4a47204b1d9e76a5d48656ac2e4b0f9f8f6a42 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Sun, 31 Oct 2021 10:20:34 +0100 Subject: [PATCH] fixed IndexOutOfBounds when rendering quotes --- .../ui/adapter/MessageAdapter.java | 16 ++++----- .../conversations/ui/util/QuoteHelper.java | 34 +++++++++---------- 2 files changed, 24 insertions(+), 26 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java b/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java index 0eb8a10fb..ccb40418a 100644 --- a/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java +++ b/src/main/java/eu/siacs/conversations/ui/adapter/MessageAdapter.java @@ -370,9 +370,7 @@ public class MessageAdapter extends ArrayAdapter { char current = body.length() > i ? body.charAt(i) : '\n'; if (lineStart == -1) { if (previous == '\n') { - if ( - QuoteHelper.isPositionQuoteStart(body, i) - ) { + if (i < body.length() && QuoteHelper.isPositionQuoteStart(body, i)) { // Line start with quote lineStart = i; if (quoteStart == -1) quoteStart = i; @@ -806,12 +804,12 @@ public class MessageAdapter extends ArrayAdapter { } else if (message.treatAsDownloadable()) { try { final URI uri = new URI(message.getBody()); - displayDownloadableMessage(viewHolder, - message, - activity.getString(R.string.check_x_filesize_on_host, - UIHelper.getFileDescriptionString(activity, message), - uri.getHost()), - darkBackground); + displayDownloadableMessage(viewHolder, + message, + activity.getString(R.string.check_x_filesize_on_host, + UIHelper.getFileDescriptionString(activity, message), + uri.getHost()), + darkBackground); } catch (Exception e) { displayDownloadableMessage(viewHolder, message, diff --git a/src/main/java/eu/siacs/conversations/ui/util/QuoteHelper.java b/src/main/java/eu/siacs/conversations/ui/util/QuoteHelper.java index ac2913037..4beee8a22 100644 --- a/src/main/java/eu/siacs/conversations/ui/util/QuoteHelper.java +++ b/src/main/java/eu/siacs/conversations/ui/util/QuoteHelper.java @@ -11,47 +11,47 @@ public class QuoteHelper { public static final char QUOTE_ALT_CHAR = '»'; public static final char QUOTE_ALT_END_CHAR = '«'; - public static boolean isPositionQuoteCharacter(CharSequence body, int pos){ + public static boolean isPositionQuoteCharacter(CharSequence body, int pos) { // second part of logical check actually goes against the logic indicated in the method name, since it also checks for context // but it's very useful return body.charAt(pos) == QUOTE_CHAR || isPositionAltQuoteStart(body, pos); } - public static boolean isPositionQuoteEndCharacter(CharSequence body, int pos){ + public static boolean isPositionQuoteEndCharacter(CharSequence body, int pos) { return body.charAt(pos) == QUOTE_END_CHAR; } - public static boolean isPositionAltQuoteCharacter (CharSequence body, int pos){ + public static boolean isPositionAltQuoteCharacter(CharSequence body, int pos) { return body.charAt(pos) == QUOTE_ALT_CHAR; } - public static boolean isPositionAltQuoteEndCharacter(CharSequence body, int pos){ + public static boolean isPositionAltQuoteEndCharacter(CharSequence body, int pos) { return body.charAt(pos) == QUOTE_ALT_END_CHAR; } - public static boolean isPositionAltQuoteStart(CharSequence body, int pos){ + public static boolean isPositionAltQuoteStart(CharSequence body, int pos) { return isPositionAltQuoteCharacter(body, pos) && !isPositionFollowedByAltQuoteEnd(body, pos); } public static boolean isPositionFollowedByQuoteChar(CharSequence body, int pos) { - return body.length() > pos + 1 && isPositionQuoteCharacter(body, pos +1 ); + return body.length() > pos + 1 && isPositionQuoteCharacter(body, pos + 1); } // 'Prequote' means anything we require or can accept in front of a QuoteChar - public static boolean isPositionPrecededByPrequote(CharSequence body, int pos){ + public static boolean isPositionPrecededByPreQuote(CharSequence body, int pos) { return UIHelper.isPositionPrecededByLineStart(body, pos); } - public static boolean isPositionQuoteStart (CharSequence body, int pos){ + public static boolean isPositionQuoteStart(CharSequence body, int pos) { return (isPositionQuoteCharacter(body, pos) - && isPositionPrecededByPrequote(body, pos) + && isPositionPrecededByPreQuote(body, pos) && (UIHelper.isPositionFollowedByQuoteableCharacter(body, pos) - || isPositionFollowedByQuoteChar(body, pos))); + || isPositionFollowedByQuoteChar(body, pos))); } - public static boolean bodyContainsQuoteStart (CharSequence body){ - for (int i = 0; i < body.length(); i++){ - if (isPositionQuoteStart(body, i)){ + public static boolean bodyContainsQuoteStart(CharSequence body) { + for (int i = 0; i < body.length(); i++) { + if (isPositionQuoteStart(body, i)) { return true; } } @@ -76,7 +76,7 @@ public class QuoteHelper { return false; } - public static boolean isNestedTooDeeply (CharSequence line){ + public static boolean isNestedTooDeeply(CharSequence line) { if (isPositionQuoteStart(line, 0)) { int nestingDepth = 1; for (int i = 1; i < line.length(); i++) { @@ -91,9 +91,9 @@ public class QuoteHelper { return false; } - public static String replaceAltQuoteCharsInText(String text){ - for (int i = 0; i < text.length(); i++){ - if (isPositionAltQuoteStart(text, i)){ + public static String replaceAltQuoteCharsInText(String text) { + for (int i = 0; i < text.length(); i++) { + if (isPositionAltQuoteStart(text, i)) { text = text.substring(0, i) + QUOTE_CHAR + text.substring(i + 1); } }