From e141de9aa4141d787102674f7ea0b88bf6be467e Mon Sep 17 00:00:00 2001 From: Florian Schmaus Date: Tue, 7 Mar 2017 08:50:22 +0100 Subject: [PATCH] Fix possible NPE in roster push listener Daniele Ricci reporting the following NPE using Smack 4.1.9 java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.indexOf(int)' on a null object reference at org.jxmpp.util.XmppStringUtils.parseBareJid(XmppStringUtils.java:124) at org.jivesoftware.smack.roster.Roster$RosterPushListener.handleIQRequest(Roster.java:1416) at org.jivesoftware.smack.AbstractXMPPConnection$2.run(AbstractXMPPConnection.java:1061) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) at java.lang.Thread.run(Thread.java:818) This is possibly caused by a service sending roster pushes for unbound connections, i.e. where getUsers() returns 'null'. We now log such situations instead throwing an NPE. --- .../java/org/jivesoftware/smack/roster/Roster.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/smack-im/src/main/java/org/jivesoftware/smack/roster/Roster.java b/smack-im/src/main/java/org/jivesoftware/smack/roster/Roster.java index e7e6dbce2..286c968b7 100644 --- a/smack-im/src/main/java/org/jivesoftware/smack/roster/Roster.java +++ b/smack-im/src/main/java/org/jivesoftware/smack/roster/Roster.java @@ -66,6 +66,7 @@ import org.jivesoftware.smack.roster.rosterstore.RosterStore; import org.jivesoftware.smack.util.Objects; import org.jxmpp.jid.BareJid; import org.jxmpp.jid.EntityBareJid; +import org.jxmpp.jid.EntityFullJid; import org.jxmpp.jid.Jid; import org.jxmpp.jid.FullJid; import org.jxmpp.jid.impl.JidCreate; @@ -1674,9 +1675,16 @@ public final class Roster extends Manager { final XMPPConnection connection = connection(); RosterPacket rosterPacket = (RosterPacket) iqRequest; + EntityFullJid localAddress = connection.getUser(); + if (localAddress == null) { + LOGGER.warning("Ignoring roster push " + iqRequest + " while " + connection + + " has no bound resource. This may be a server bug."); + return null; + } + // Roster push (RFC 6121, 2.1.6) // A roster push with a non-empty from not matching our address MUST be ignored - EntityBareJid jid = connection.getUser().asEntityBareJid(); + EntityBareJid jid = localAddress.asEntityBareJid(); Jid from = rosterPacket.getFrom(); if (from != null && !from.equals(jid)) { LOGGER.warning("Ignoring roster push with a non matching 'from' ourJid='" + jid + "' from='" + from