mirror of
https://github.com/signalapp/Signal-Android.git
synced 2025-12-24 04:58:45 +00:00
Fix crash when processing invalid mentions.
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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() }
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user