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(
       at org.jivesoftware.smack.roster.Roster$RosterPushListener.handleIQRequest(
       at org.jivesoftware.smack.AbstractXMPPConnection$
       at java.util.concurrent.ThreadPoolExecutor.runWorker(
       at java.util.concurrent.ThreadPoolExecutor$

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.
This commit is contained in:
Florian Schmaus 2017-03-07 08:50:22 +01:00
parent 6b7a8142c9
commit e141de9aa4
1 changed files with 9 additions and 1 deletions

View File

@ -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