aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDocile-Alligator <25734209+Docile-Alligator@users.noreply.github.com>2023-11-26 16:05:28 +0000
committerDocile-Alligator <25734209+Docile-Alligator@users.noreply.github.com>2023-11-26 16:05:28 +0000
commit7f1ba1f3eec3a6730acb07e5ccd4db2abdd49162 (patch)
treec0f5afe3a43e6a4e9d71b472cd3cca483e3e9034
parente612170931cbe9ea66458c4176465b2358e3c378 (diff)
downloadinfinity-for-reddit-7f1ba1f3eec3a6730acb07e5ccd4db2abdd49162.tar
infinity-for-reddit-7f1ba1f3eec3a6730acb07e5ccd4db2abdd49162.tar.gz
infinity-for-reddit-7f1ba1f3eec3a6730acb07e5ccd4db2abdd49162.tar.bz2
infinity-for-reddit-7f1ba1f3eec3a6730acb07e5ccd4db2abdd49162.tar.lz
infinity-for-reddit-7f1ba1f3eec3a6730acb07e5ccd4db2abdd49162.tar.xz
infinity-for-reddit-7f1ba1f3eec3a6730acb07e5ccd4db2abdd49162.tar.zst
infinity-for-reddit-7f1ba1f3eec3a6730acb07e5ccd4db2abdd49162.zip
Fix infinite loops in Utils.parseInlineRedditImages. Fix media_metadata parsing.
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/post/ParsePost.java25
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/utils/Utils.java6
2 files changed, 20 insertions, 11 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/post/ParsePost.java b/app/src/main/java/ml/docilealligator/infinityforreddit/post/ParsePost.java
index 88d60dfa..dfade7c1 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/post/ParsePost.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/post/ParsePost.java
@@ -250,20 +250,27 @@ public class ParsePost {
try {
String k = it.next();
JSONObject media = mediaMetadataJSON.getJSONObject(k);
- JSONArray downscales = media.getJSONArray(JSONUtils.P_KEY);
- JSONObject downscaledItemJSON;
- if (downscales.length() <= 3) {
- downscaledItemJSON = downscales.getJSONObject(downscales.length() - 1);
- } else {
- downscaledItemJSON = downscales.getJSONObject(3);
- }
- MediaMetadata.MediaItem downscaledItem = new MediaMetadata.MediaItem(downscaledItemJSON.getInt(JSONUtils.X_KEY),
- downscaledItemJSON.getInt(JSONUtils.Y_KEY), downscaledItemJSON.getString(JSONUtils.U_KEY));
JSONObject originalItemJSON = media.getJSONObject(JSONUtils.S_KEY);
MediaMetadata.MediaItem originalItem = new MediaMetadata.MediaItem(originalItemJSON.getInt(JSONUtils.X_KEY),
originalItemJSON.getInt(JSONUtils.Y_KEY), originalItemJSON.getString(JSONUtils.U_KEY));
+ JSONArray downscales = media.getJSONArray(JSONUtils.P_KEY);
+ JSONObject downscaledItemJSON;
+ MediaMetadata.MediaItem downscaledItem;
+ if (downscales.length() <= 0) {
+ downscaledItem = originalItem;
+ } else {
+ if (downscales.length() <= 3) {
+ downscaledItemJSON = downscales.getJSONObject(downscales.length() - 1);
+
+ } else {
+ downscaledItemJSON = downscales.getJSONObject(3);
+ }
+ downscaledItem = new MediaMetadata.MediaItem(downscaledItemJSON.getInt(JSONUtils.X_KEY),
+ downscaledItemJSON.getInt(JSONUtils.Y_KEY), downscaledItemJSON.getString(JSONUtils.U_KEY));
+ }
+
String id = media.getString(JSONUtils.ID_KEY);
mediaMetadataMap.put(id, new MediaMetadata(id, media.getString(JSONUtils.E_KEY),
originalItem, downscaledItem));
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/Utils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/Utils.java
index 01ec20ac..c47c879a 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/utils/Utils.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/utils/Utils.java
@@ -99,8 +99,9 @@ public final class Utils {
int start = 0;
int previewReddItLength = "https://preview.redd.it/".length();
while (matcher.find(start)) {
- String id = markdownStringBuilder.substring(previewReddItLength, markdownStringBuilder.indexOf(".", previewReddItLength));
+ String id = markdownStringBuilder.substring(matcher.start() + previewReddItLength, markdownStringBuilder.indexOf(".", matcher.start() + previewReddItLength));
if (!mediaMetadataMap.containsKey(id)) {
+ start = matcher.end();
continue;
}
String replacingText = "![img](" + markdownStringBuilder.substring(matcher.start(), matcher.end()) + ")";
@@ -114,8 +115,9 @@ public final class Utils {
matcher = iReddItImagePattern.matcher(markdownStringBuilder);
int iReddItLength = "https://i.redd.it/".length();
while (matcher.find(start)) {
- String id = markdownStringBuilder.substring(iReddItLength, markdownStringBuilder.indexOf(".", iReddItLength));
+ String id = markdownStringBuilder.substring(matcher.start() + iReddItLength, matcher.start() + markdownStringBuilder.indexOf(".", iReddItLength));
if (!mediaMetadataMap.containsKey(id)) {
+ start = matcher.end();
continue;
}
String replacingText = "![img](" + markdownStringBuilder.substring(matcher.start(), matcher.end()) + ")";