From f714f5b5726d2588b36568a944970c9416e5b313 Mon Sep 17 00:00:00 2001 From: Moxie Marlinspike Date: Sat, 24 Oct 2015 17:51:17 -0700 Subject: [PATCH] Really limit the number of rows that can be joined from parts. Fixes #4299 Fixes #4298 // FREEBIE --- .../thoughtcrime/securesms/database/MmsDatabase.java | 2 +- .../securesms/database/MmsSmsDatabase.java | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/org/thoughtcrime/securesms/database/MmsDatabase.java b/src/org/thoughtcrime/securesms/database/MmsDatabase.java index c938deeff8..00fae72f63 100644 --- a/src/org/thoughtcrime/securesms/database/MmsDatabase.java +++ b/src/org/thoughtcrime/securesms/database/MmsDatabase.java @@ -309,7 +309,7 @@ public class MmsDatabase extends MessagingDatabase { private Cursor rawQuery(@NonNull String where, @Nullable String[] arguments) { SQLiteDatabase database = databaseHelper.getReadableDatabase(); - return database.rawQuery("SELECT DISTINCT " + Util.join(MMS_PROJECTION, ",") + + return database.rawQuery("SELECT " + Util.join(MMS_PROJECTION, ",") + " FROM " + MmsDatabase.TABLE_NAME + " LEFT OUTER JOIN " + AttachmentDatabase.TABLE_NAME + " ON (" + MmsDatabase.TABLE_NAME + "." + MmsDatabase.ID + " = " + AttachmentDatabase.TABLE_NAME + "." + AttachmentDatabase.MMS_ID + ")" + " WHERE " + where, arguments); diff --git a/src/org/thoughtcrime/securesms/database/MmsSmsDatabase.java b/src/org/thoughtcrime/securesms/database/MmsSmsDatabase.java index 2ef65b4be0..269c441379 100644 --- a/src/org/thoughtcrime/securesms/database/MmsSmsDatabase.java +++ b/src/org/thoughtcrime/securesms/database/MmsSmsDatabase.java @@ -167,8 +167,15 @@ public class MmsSmsDatabase extends Database { mmsQueryBuilder.setDistinct(true); smsQueryBuilder.setDistinct(true); - mmsQueryBuilder.setTables(MmsDatabase.TABLE_NAME + " LEFT OUTER JOIN " + AttachmentDatabase.TABLE_NAME + " ON (" + MmsDatabase.TABLE_NAME + "." + MmsDatabase.ID + " = " + AttachmentDatabase.TABLE_NAME + "." + AttachmentDatabase.MMS_ID + ")"); smsQueryBuilder.setTables(SmsDatabase.TABLE_NAME); + mmsQueryBuilder.setTables(MmsDatabase.TABLE_NAME + " LEFT OUTER JOIN " + + AttachmentDatabase.TABLE_NAME + + " ON " + AttachmentDatabase.TABLE_NAME + "." + AttachmentDatabase.ROW_ID + " = " + + " (SELECT " + AttachmentDatabase.TABLE_NAME + "." + AttachmentDatabase.ROW_ID + + " FROM " + AttachmentDatabase.TABLE_NAME + " WHERE " + + AttachmentDatabase.TABLE_NAME + "." + AttachmentDatabase.MMS_ID + " = " + + MmsDatabase.TABLE_NAME + "." + MmsDatabase.ID + " LIMIT 1)"); + Set mmsColumnsPresent = new HashSet<>(); mmsColumnsPresent.add(MmsSmsColumns.ID); @@ -222,7 +229,6 @@ public class MmsSmsDatabase extends Database { String unionQuery = unionQueryBuilder.buildUnionQuery(new String[] {smsSubQuery, mmsSubQuery}, order, limit); SQLiteQueryBuilder outerQueryBuilder = new SQLiteQueryBuilder(); - outerQueryBuilder.setDistinct(true); outerQueryBuilder.setTables("(" + unionQuery + ")"); String query = outerQueryBuilder.buildQuery(projection, null, null, null, null, null, null);