Fix crash when processing invalid mentions.

fork-5.53.8
Cody Henthorne 2021-05-19 13:15:28 -04:00
rodzic f490d1f6d2
commit 6770d21cf7
2 zmienionych plików z 13 dodań i 0 usunięć

Wyświetl plik

@ -76,6 +76,10 @@ public final class MentionUtil {
int bodyIndex = 0;
for (Mention mention : sortedMentions) {
if (invalidMention(body, mention)) {
return new UpdatedBodyAndMentions(body, Collections.emptyList());
}
updatedBody.append(body.subSequence(bodyIndex, mention.getStart()));
CharSequence replaceWith = replacementTextGenerator.apply(mention);
Mention updatedMention = new Mention(mention.getRecipientId(), updatedBody.length(), replaceWith.length());
@ -139,6 +143,13 @@ public final class MentionUtil {
throw new IllegalArgumentException("Unknown mention setting: " + mentionSetting);
}
private static boolean invalidMention(@NonNull CharSequence body, @NonNull Mention mention) {
int start = mention.getStart();
int length = mention.getLength();
return start < 0 || length < 0 || (start + length) > body.length();
}
public static class UpdatedBodyAndMentions {
@Nullable private final CharSequence body;
@NonNull private final List<Mention> mentions;

Wyświetl plik

@ -69,6 +69,8 @@ public class MentionUtilTest_updateBodyAndMentionsWithPlaceholders {
builder().text("middle 🤡👍🏾 👨🏼‍🤝‍👨🏽 ").mention("a").text(" 👍🏾 middle 👩‍👩‍👦‍👦").build(),
builder().text("start ").mention("emoji 🩳").build(),
builder().text("start ").mention("emoji 🩳").text(" middle ").mention("emoji 🩳").text(" end").build(),
{ "message", Collections.singletonList(new Mention(RecipientId.from(1), 30, 5)), "message", Collections.<Mention>emptyList() }
});
}