From d65d239550812b0dd5bc2897b3fe9576b59bb578 Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Mon, 4 Aug 2014 12:19:58 +0200 Subject: [PATCH] Make getMessages() set up collector *before* sending the request Similar flaw was also fixed in 2c7f1efe80b988. Also ensure that OfflineMessagesManager doesn't leak collector by using a try/finally block. Fixes SMACK-592 --- .../smackx/offline/OfflineMessageManager.java | 43 +++++++++++-------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/smack-extensions/src/main/java/org/jivesoftware/smackx/offline/OfflineMessageManager.java b/smack-extensions/src/main/java/org/jivesoftware/smackx/offline/OfflineMessageManager.java index 8c7bfb75d..3d7d13b88 100644 --- a/smack-extensions/src/main/java/org/jivesoftware/smackx/offline/OfflineMessageManager.java +++ b/smack-extensions/src/main/java/org/jivesoftware/smackx/offline/OfflineMessageManager.java @@ -156,20 +156,24 @@ public class OfflineMessageManager { } }); PacketCollector messageCollector = connection.createPacketCollector(messageFilter); - connection.createPacketCollectorAndSend(request).nextResultOrThrow(); - // Collect the received offline messages - Message message = (Message) messageCollector.nextResult(); - while (message != null) { - messages.add(message); - message = (Message) messageCollector.nextResult(); + try { + connection.createPacketCollectorAndSend(request).nextResultOrThrow(); + // Collect the received offline messages + Message message = (Message) messageCollector.nextResult(); + while (message != null) { + messages.add(message); + message = (Message) messageCollector.nextResult(); + } + } + finally { + // Stop queuing offline messages + messageCollector.cancel(); } - // Stop queuing offline messages - messageCollector.cancel(); return messages; } /** - * Returns an Iterator with all the offline Messages of the user. The returned offline + * Returns a List of Messages with all the offline Messages of the user. The returned offline * messages will not be deleted from the server. Use {@link #deleteMessages(java.util.List)} * to delete the messages. * @@ -183,17 +187,22 @@ public class OfflineMessageManager { List messages = new ArrayList(); OfflineMessageRequest request = new OfflineMessageRequest(); request.setFetch(true); - connection.createPacketCollectorAndSend(request).nextResultOrThrow(); PacketCollector messageCollector = connection.createPacketCollector(packetFilter); - // Collect the received offline messages - Message message = (Message) messageCollector.nextResult(); - while (message != null) { - messages.add(message); - message = (Message) messageCollector.nextResult(); + try { + connection.createPacketCollectorAndSend(request).nextResultOrThrow(); + + // Collect the received offline messages + Message message = (Message) messageCollector.nextResult(); + while (message != null) { + messages.add(message); + message = (Message) messageCollector.nextResult(); + } + } + finally { + // Stop queuing offline messages + messageCollector.cancel(); } - // Stop queuing offline messages - messageCollector.cancel(); return messages; }