Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 244222

Summary: [compiler][null][correlation] Using xor when checking for null results in false "potential null pointer" warning
Product: [Eclipse Project] JDT Reporter: Karl Stenerud <kstenerud>
Component: CoreAssignee: JDT-Core-Inbox <jdt-core-inbox>
Status: CLOSED DUPLICATE QA Contact:
Severity: normal    
Priority: P5 CC: Olivier_Thomann, remy.suen, stephan.herrmann
Version: 3.3.2Keywords: helpwanted
Target Milestone: ---   
Hardware: PC   
OS: Linux   
Whiteboard:

Description Karl Stenerud CLA 2008-08-14 14:28:15 EDT
The following will cause eclipse to issue a "potential null pointer" warning on the last line, even though there's no possibility for s1 to be null at that point.

public boolean areDifferent(String s1, String s2)
{
    if(s1 == null && s2 == null)
    {
        return false;
    }
    if((s1 == null) ^ (s2 == null))
    {
        return true;
    }
    return !s1.equals(s2);
}


As proof, the following lines do not cause a NullPointerException:

areDifferent(null, null);
areDifferent("a", null);
areDifferent(null, "a");
areDifferent("a", "a");
Comment 1 Remy Suen CLA 2009-08-29 16:34:44 EDT
Still a problem on I20090811-0800. If you use |, the warning will also appear.
Comment 2 Stephan Herrmann CLA 2011-02-28 06:42:01 EST
Fixing this would require analysis of the correlation of s1 and s2,
which we currently don't do.

Closing as duplicate despite the slight different in logic.

*** This bug has been marked as a duplicate of bug 290263 ***
Comment 3 Stephan Herrmann CLA 2018-08-30 10:29:43 EDT

*** This bug has been marked as a duplicate of bug 538421 ***