NPE at USM.addLocalizedUsmUserEntry

Hello!

I have found something strange while upgrading my pet project from SNMP4J 2.5.0 to 3.2.1. I have managed to narrow it to a short test case:

    //creating snmpv3 user 
    OctetString userName = new OctetString("SnmpUser1");
    UsmUser usmUser = new UsmUser(userName, AuthSHA.ID, new OctetString("auth5678"), PrivAES128.ID, new OctetString("priv5678"));
    UsmUserEntry userEntry1 = usm.getUser(new OctetString(), userName);         

    //we add the user, but the engineId is not known yet
    usm.addUser(new OctetString(userName), new OctetString(), usmUser);
    UsmUserEntry userEntry2 = usm.getUser(new OctetString(), userName);
    
    //we get the engineid from database and assign it to the address
    OctetString engineId = new OctetString("EngineID1");

    //this call fails with 3.2.1, but is ok with 2.5.0
    UsmUserEntry userEntry3 = usm.getUser(engineId, userName);
    //java.lang.NullPointerException
    //  at org.snmp4j.security.USM.addLocalizedUsmUserEntry(USM.java:282)
    //  at org.snmp4j.security.USM.getUser(USM.java:263)

Did I miss something in the changelog? Or is this code snipplet wrong in some way?

Kind regards,
Zoltan

The code snippet is syntactically correct, but is based on a wrong assumption.
Line

UsmUserEntry userEntry2 = usm.getUser(new OctetString(), userName);

actually tries to get a localised user for a zero length engine ID. During localisation, passphrase information is removed from the user entry. That’s why the call

UsmUserEntry userEntry3 = usm.getUser(engineId, userName);

then throws a NullPointerException. I have modified the user.getUser code to prevent localisation of a UsmUserEntry with a zero length engine ID. Thus, in version 3.2.2 the code snippet will work again, although it does not make much sense except for interface symmetry to use getUser with an empty engine ID.

Thank you very much!

SNMP4J 3.2.2 is now available from https://agentpp.com/download.html