Bug 147118 - [compiler][null] Incorrect null analysis involving do_while loop
Summary: [compiler][null] Incorrect null analysis involving do_while loop
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.2   Edit
Hardware: Macintosh Mac OS X - Carbon (unsup.)
: P3 normal (vote)
Target Milestone: 3.3 M3   Edit
Assignee: Maxime Daniel CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks: 157154
  Show dependency tree
 
Reported: 2006-06-14 17:04 EDT by Gordon Henriksen CLA
Modified: 2007-07-29 09:20 EDT (History)
2 users (show)

See Also:


Attachments
Suggested fix plus activated test case (2.19 KB, patch)
2006-09-19 02:58 EDT, Maxime Daniel CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Gordon Henriksen CLA 2006-06-14 17:04:44 EDT
This snippet causes null analysis to raise an incorrect warning. It could probably be slightly more minimal, but this actually makes sense.

	private final static class Node {
		public Node parent;
		public Node sibling;
		public Node child;
	}
	
	public void nullAnalysisBug(Node node) {
		Node cur = node;
		do {
			if (null != cur.child)  // INCORRECT -> warning: The variable cur may be null
				cur = cur.child;
			else if (null != cur.sibling)
				cur = cur.sibling;
			else if (null != cur.parent)
				cur = cur.parent.sibling;
			else
				cur = null; // B
		} while (cur != null);
	}

There are two workarounds:
A. Transforming the loop into a while loop silences the warning, at the cost of an additional comparison at runtime.
B. Transforming the final else statement into a 'break' also averts the warning:
Comment 1 Maxime Daniel CLA 2006-06-15 04:21:19 EDT
Agreed. Following shorter test case should remove any doubt about it:

public class X {
  String f;
  void foo (boolean b) {
    X x = new X();
    do {
      System.out.println(x.f); // yields may be null, whereas it is not first 
                               // time, and is protected by the test thereafter
      if (b) {
        x = null;
      }
    } while (x != null);
  }
}

Added (inactive) test NullReferenceTest#613.
Comment 2 Maxime Daniel CLA 2006-09-19 02:58:26 EDT
Created attachment 50447 [details]
Suggested fix plus activated test case

Mistakenly used the action flow info in input for the looping context late check. Must use the upstream flow instead.
Waiting for 3.3 M2 to be declared before committing.
Comment 3 Maxime Daniel CLA 2006-09-25 06:25:20 EDT
Released for 3.3M3.
Comment 4 Olivier Thomann CLA 2006-10-30 14:00:34 EST
Verified for 3.3 M3 using warm-up build I20061030-0010
Comment 5 Eclipse Webmaster CLA 2007-07-29 09:20:21 EDT
Changing OS from Mac OS to Mac OS X as per bug 185991