From 6230abb524d6453bed1b7673c999520565f76717 Mon Sep 17 00:00:00 2001 From: Moxie Marlinspike Date: Sun, 3 Mar 2013 18:49:39 -0800 Subject: [PATCH] Add synchronized access to cache --- .../crypto/MessageDisplayHelper.java | 27 +++++++++++-------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/src/org/thoughtcrime/securesms/crypto/MessageDisplayHelper.java b/src/org/thoughtcrime/securesms/crypto/MessageDisplayHelper.java index 229da3b7e3..64e88aa105 100644 --- a/src/org/thoughtcrime/securesms/crypto/MessageDisplayHelper.java +++ b/src/org/thoughtcrime/securesms/crypto/MessageDisplayHelper.java @@ -34,17 +34,19 @@ public class MessageDisplayHelper { }; private static String checkCacheForBody(String body) { - if (decryptedBodyCache.containsKey(body)) { - String decryptedBody = decryptedBodyCache.get(body).get(); - if (decryptedBody != null) { - return decryptedBody; - } else { - decryptedBodyCache.remove(body); - return null; + synchronized (decryptedBodyCache) { + if (decryptedBodyCache.containsKey(body)) { + String decryptedBody = decryptedBodyCache.get(body).get(); + if (decryptedBody != null) { + return decryptedBody; + } else { + decryptedBodyCache.remove(body); + return null; + } } - } - return null; + return null; + } } public static String getDecryptedMessageBody(MasterCipher bodyCipher, String body) throws InvalidMessageException { @@ -55,11 +57,14 @@ public class MessageDisplayHelper { return cacheResult; String decryptedBody = bodyCipher.decryptBody(body.substring(Prefix.SYMMETRIC_ENCRYPT.length())); - decryptedBodyCache.put(body, new SoftReference(decryptedBody)); + + synchronized (decryptedBodyCache) { + decryptedBodyCache.put(body, new SoftReference(decryptedBody)); + } return decryptedBody; } return body; } -} +} \ No newline at end of file