Fix non-thread-safe operations on canonical id cache

// FREEBIE
This commit is contained in:
Moxie Marlinspike 2017-01-12 16:06:14 -08:00
parent fd3d02d818
commit edac0e85c7

View File

@ -28,19 +28,14 @@ import android.telephony.PhoneNumberUtils;
import android.text.TextUtils; import android.text.TextUtils;
import android.util.Log; import android.util.Log;
import com.google.i18n.phonenumbers.NumberParseException;
import com.google.i18n.phonenumbers.PhoneNumberUtil;
import com.google.i18n.phonenumbers.Phonenumber;
import com.google.i18n.phonenumbers.ShortNumberInfo;
import org.thoughtcrime.securesms.util.GroupUtil; import org.thoughtcrime.securesms.util.GroupUtil;
import org.thoughtcrime.securesms.util.LRUCache; import org.thoughtcrime.securesms.util.LRUCache;
import org.thoughtcrime.securesms.util.ShortCodeUtil; import org.thoughtcrime.securesms.util.ShortCodeUtil;
import org.thoughtcrime.securesms.util.TextSecurePreferences; import org.thoughtcrime.securesms.util.TextSecurePreferences;
import org.whispersystems.libsignal.util.guava.Optional;
import org.whispersystems.signalservice.api.util.InvalidNumberException; import org.whispersystems.signalservice.api.util.InvalidNumberException;
import org.whispersystems.signalservice.api.util.PhoneNumberFormatter; import org.whispersystems.signalservice.api.util.PhoneNumberFormatter;
import java.util.Collections;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -64,9 +59,9 @@ public class CanonicalAddressDatabase {
private DatabaseHelper databaseHelper; private DatabaseHelper databaseHelper;
private final Context context; private final Context context;
private final Map<String, Long> addressCache = new ConcurrentHashMap<>(); private final Map<String, Long> addressCache = new ConcurrentHashMap<>();
private final Map<Long, String> idCache = new ConcurrentHashMap<>(); private final Map<Long, String> idCache = new ConcurrentHashMap<>();
private final LRUCache<String, String> formattedAddressCache = new LRUCache<>(100); private final Map<String, String> formattedAddressCache = Collections.synchronizedMap(new LRUCache<String, String>(100));
public synchronized static CanonicalAddressDatabase getInstance(Context context) { public synchronized static CanonicalAddressDatabase getInstance(Context context) {
if (instance == null) if (instance == null)