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

Bug 345383

Summary: [null][correlation] spurious "null pointer access" warning
Product: [Eclipse Project] JDT Reporter: Noel Grandin <noelgrandin>
Component: CoreAssignee: Ayushman Jain <amj87.iitr>
Status: CLOSED DUPLICATE QA Contact:
Severity: enhancement    
Priority: P3 CC: amj87.iitr, david, remy.suen, stephan.herrmann
Version: 3.7Keywords: helpwanted
Target Milestone: 4.7 M1   
Hardware: PC   
OS: Windows 7   
Whiteboard:

Description Noel Grandin CLA 2011-05-11 05:30:37 EDT
Build Identifier: 20110218-0911

"Null Pointer Access" triggered for following code

Reproducible: Always

Steps to Reproduce:
public class TestFile {

    public void doTest() {
        byte[] buffMem = null;
        byte[] buffFile = null;

        for (int i = 0; i < 10; i++) {
            if (buffMem == null) {
                buffMem = new byte[10];
                buffFile = new byte[10];
            }
            if (buffFile.length > 16) { // <<< warning here
            }
        }
    }

}
Comment 1 Ayushman Jain CLA 2011-05-11 05:47:03 EDT
This case is a bit complicated. The warning is not correct, nor can we say that it is entirely wrong (that's why its only a "potential" warning). The warning is a result of initializing buffFile inside an if statement which is true only for 1 iteration of the loop. So, the compiler sees it as either initialized or not initialized depending on whether the if is true or false in any given iteration. Ofcourse, for a human reader it is straightforward to see that buffFile will always be initialized in the first iteration itself since the if statement is true in the 1st iteration. Yet for the compiler this isn't a trivial task.

I will classify this under correlation analysis since the nullness of buffFile is correlated with that of buffMem here. We don't do correlation analysis in null analysis currently.
Comment 2 Noel Grandin CLA 2011-05-11 05:50:17 EDT
Simplified test case: It can be demonstrated without the for loop, but you are correct, it is a correlation analysis problem.


public class TestFile 
{
	public void doTest(byte[] buffMem) {
		byte[] buffFile = null;

		if (buffMem == null) {
			buffMem = new byte[10];
			buffFile = new byte[10];
		}
		if (buffFile.length > 16) { // <<< warning here
		}
	}
}
Comment 3 Ayushman Jain CLA 2011-05-11 06:22:21 EDT
(In reply to comment #2)
> 
> public class TestFile 
> {
>     public void doTest(byte[] buffMem) {
>         byte[] buffFile = null;
> 
>         if (buffMem == null) {
>             buffMem = new byte[10];
>             buffFile = new byte[10];
>         }
>         if (buffFile.length > 16) { // <<< warning here
>         }
>     }
> }

Actually the warning is correct in this case. This is because the if statement can either be true or be false, but we are not able to determine that path because we dont know what value will be passed to the parameter when doTest is called. But the same logic fails in the for loop case because for atleast 1 iteration of the loop, we can deterministically tell the execution path.
Comment 4 Stephan Herrmann CLA 2016-06-28 17:17:37 EDT
Bulk closing all compiler bugs tagged [null][correlation], because we have no plans to add such a feature: it would be a tremendous implementation effort, beyond our current man power, and may be impossible to achieve within the desired performance bounds.

If s.o. has a viable strategy or even implementation for such a feature, I'm all ears.
Comment 5 Sasikanth Bharadwaj CLA 2016-08-02 04:49:09 EDT
Verified for 4.7 M1
Comment 6 Stephan Herrmann CLA 2018-08-30 10:36:17 EDT
I created a new umbrella RFE outlining what would be needed to address this issue.

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