From 8edfc61346e8ebdbadada72265c993e097450c9d Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Sat, 30 May 2020 10:57:22 +0200 Subject: [PATCH] fixed concurrent modification when iterating over presences --- .../eu/siacs/conversations/entities/Account.java | 2 +- .../eu/siacs/conversations/entities/Presences.java | 12 ++++++++++-- .../services/XmppConnectionService.java | 4 ++-- .../xmpp/jingle/JingleFileTransferConnection.java | 2 +- .../conversations/xmpp/jingle/RtpCapability.java | 2 +- 5 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/entities/Account.java b/src/main/java/eu/siacs/conversations/entities/Account.java index d2d25e321..4b7e424a3 100644 --- a/src/main/java/eu/siacs/conversations/entities/Account.java +++ b/src/main/java/eu/siacs/conversations/entities/Account.java @@ -432,7 +432,7 @@ public class Account extends AbstractEntity implements AvatarService.Avatarable public int activeDevicesWithRtpCapability() { int i = 0; - for(Presence presence : getSelfContact().getPresences().getPresences().values()) { + for(Presence presence : getSelfContact().getPresences().getPresences()) { if (RtpCapability.check(presence) != RtpCapability.Capability.NONE) { i++; } diff --git a/src/main/java/eu/siacs/conversations/entities/Presences.java b/src/main/java/eu/siacs/conversations/entities/Presences.java index c8301123b..5a8452019 100644 --- a/src/main/java/eu/siacs/conversations/entities/Presences.java +++ b/src/main/java/eu/siacs/conversations/entities/Presences.java @@ -11,8 +11,16 @@ import java.util.Map; public class Presences { private final Hashtable presences = new Hashtable<>(); - public Hashtable getPresences() { - return this.presences; + public List getPresences() { + synchronized (this.presences) { + return new ArrayList<>(this.presences.values()); + } + } + + public Presence get(String resource) { + synchronized (this.presences) { + return this.presences.get(resource); + } } public void updatePresence(String resource, Presence presence) { diff --git a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java index 8ea3ed2ea..6f1ff65cc 100644 --- a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java @@ -4468,8 +4468,8 @@ public class XmppConnectionService extends Service { } private void injectServiceDiscoveryResult(Roster roster, String hash, String ver, ServiceDiscoveryResult disco) { - for (Contact contact : roster.getContacts()) { - for (Presence presence : contact.getPresences().getPresences().values()) { + for (final Contact contact : roster.getContacts()) { + for (final Presence presence : contact.getPresences().getPresences()) { if (hash.equals(presence.getHash()) && ver.equals(presence.getVer())) { presence.setServiceDiscoveryResult(disco); } diff --git a/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleFileTransferConnection.java b/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleFileTransferConnection.java index 5d25cb3b0..2f84f7c45 100644 --- a/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleFileTransferConnection.java +++ b/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleFileTransferConnection.java @@ -417,7 +417,7 @@ public class JingleFileTransferConnection extends AbstractJingleConnection imple final Jid jid = this.id.with; String resource = jid != null ? jid.getResource() : null; if (resource != null) { - Presence presence = this.id.account.getRoster().getContact(jid).getPresences().getPresences().get(resource); + Presence presence = this.id.account.getRoster().getContact(jid).getPresences().get(resource); ServiceDiscoveryResult result = presence != null ? presence.getServiceDiscoveryResult() : null; return result == null ? Collections.emptyList() : result.getFeatures(); } else { diff --git a/src/main/java/eu/siacs/conversations/xmpp/jingle/RtpCapability.java b/src/main/java/eu/siacs/conversations/xmpp/jingle/RtpCapability.java index 646137495..d040694c4 100644 --- a/src/main/java/eu/siacs/conversations/xmpp/jingle/RtpCapability.java +++ b/src/main/java/eu/siacs/conversations/xmpp/jingle/RtpCapability.java @@ -40,7 +40,7 @@ public class RtpCapability { public static Capability check(final Contact contact) { final Presences presences = contact.getPresences(); Capability result = Capability.NONE; - for(Presence presence : presences.getPresences().values()) { + for(Presence presence : presences.getPresences()) { Capability capability = check(presence); if (capability == Capability.VIDEO) { result = capability;