Skip to content

Commit

Permalink
Prevent crashing on duplicate gv1 storage records.
Browse files Browse the repository at this point in the history
  • Loading branch information
greyson-signal committed Mar 17, 2021
1 parent faa19ac commit 0172c1e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -812,17 +812,18 @@ public void applyStorageIdUpdates(@NonNull Map<RecipientId, StorageId> storageId
}
}

public void applyStorageSyncUpdates(@NonNull Collection<SignalContactRecord> contactInserts,
@NonNull Collection<RecordUpdate<SignalContactRecord>> contactUpdates,
@NonNull Collection<SignalGroupV1Record> groupV1Inserts,
@NonNull Collection<RecordUpdate<SignalGroupV1Record>> groupV1Updates,
@NonNull Collection<SignalGroupV2Record> groupV2Inserts,
@NonNull Collection<RecordUpdate<SignalGroupV2Record>> groupV2Updates)
public boolean applyStorageSyncUpdates(@NonNull Collection<SignalContactRecord> contactInserts,
@NonNull Collection<RecordUpdate<SignalContactRecord>> contactUpdates,
@NonNull Collection<SignalGroupV1Record> groupV1Inserts,
@NonNull Collection<RecordUpdate<SignalGroupV1Record>> groupV1Updates,
@NonNull Collection<SignalGroupV2Record> groupV2Inserts,
@NonNull Collection<RecordUpdate<SignalGroupV2Record>> groupV2Updates)
{
SQLiteDatabase db = databaseHelper.getWritableDatabase();
IdentityDatabase identityDatabase = DatabaseFactory.getIdentityDatabase(context);
ThreadDatabase threadDatabase = DatabaseFactory.getThreadDatabase(context);
Set<RecipientId> needsRefresh = new HashSet<>();
boolean forcePush = false;

db.beginTransaction();

Expand Down Expand Up @@ -944,12 +945,17 @@ public void applyStorageSyncUpdates(@NonNull Collection<SignalContactRecord>
}

for (SignalGroupV1Record insert : groupV1Inserts) {
db.insertOrThrow(TABLE_NAME, null, getValuesForStorageGroupV1(insert));
long id = db.insertWithOnConflict(TABLE_NAME, null, getValuesForStorageGroupV1(insert), SQLiteDatabase.CONFLICT_IGNORE);

Recipient recipient = Recipient.externalGroupExact(context, GroupId.v1orThrow(insert.getGroupId()));
if (id < 0) {
Log.w(TAG, "Duplicate GV1 entry detected! Ignoring, suggesting force-push.");
forcePush = true;
} else {
Recipient recipient = Recipient.externalGroupExact(context, GroupId.v1orThrow(insert.getGroupId()));

threadDatabase.applyStorageSyncUpdate(recipient.getId(), insert);
needsRefresh.add(recipient.getId());
threadDatabase.applyStorageSyncUpdate(recipient.getId(), insert);
needsRefresh.add(recipient.getId());
}
}

for (RecordUpdate<SignalGroupV1Record> update : groupV1Updates) {
Expand Down Expand Up @@ -1017,6 +1023,8 @@ public void applyStorageSyncUpdates(@NonNull Collection<SignalContactRecord>
for (RecipientId id : needsRefresh) {
Recipient.live(id).refresh();
}

return forcePush;
}

public void applyStorageSyncUpdates(@NonNull StorageId storageId, SignalAccountRecord update) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ private boolean performSync() throws IOException, RetryLaterException, InvalidKe
}

migrateToGv2IfNecessary(context, mergeResult.getLocalGroupV2Inserts());
recipientDatabase.applyStorageSyncUpdates(mergeResult.getLocalContactInserts(), mergeResult.getLocalContactUpdates(), mergeResult.getLocalGroupV1Inserts(), mergeResult.getLocalGroupV1Updates(), mergeResult.getLocalGroupV2Inserts(), mergeResult.getLocalGroupV2Updates());
needsForcePush |= recipientDatabase.applyStorageSyncUpdates(mergeResult.getLocalContactInserts(), mergeResult.getLocalContactUpdates(), mergeResult.getLocalGroupV1Inserts(), mergeResult.getLocalGroupV1Updates(), mergeResult.getLocalGroupV2Inserts(), mergeResult.getLocalGroupV2Updates());
storageKeyDatabase.applyStorageSyncUpdates(mergeResult.getLocalUnknownInserts(), mergeResult.getLocalUnknownDeletes());
StorageSyncHelper.applyAccountStorageSyncUpdates(context, mergeResult.getLocalAccountUpdate());

Expand Down

0 comments on commit 0172c1e

Please sign in to comment.