Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 314830 - [compiler] Switching on a null expression doesn't always throw NullPointerException
Summary: [compiler] Switching on a null expression doesn't always throw NullPointerExc...
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.6   Edit
Hardware: PC Windows Vista
: P3 normal with 2 votes (vote)
Target Milestone: 3.6.1   Edit
Assignee: Olivier Thomann CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-05-28 04:57 EDT by yogy.namara CLA
Modified: 2010-08-26 02:58 EDT (History)
2 users (show)

See Also:
srikanth_sankaran: review+


Attachments
Proposed fix + regression tests (6.39 KB, patch)
2010-05-31 11:14 EDT, Olivier Thomann CLA
no flags Details | Diff
Proposed fix + regression tests (10.89 KB, patch)
2010-06-02 13:24 EDT, Olivier Thomann CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description yogy.namara CLA 2010-05-28 04:57:55 EDT
Build Identifier: 20090920-1017

Eclipse doesn't always throw NullPointerException when switching on a null expression. It can even allow execution of a default case.

See: JLS 14.11 The Switch Statement (http://java.sun.com/docs/books/jls/third_edition/html/statements.html#14.11)

SwitchStatement:
    switch ( Expression ) SwitchBlock

When the switch statement is executed, first the Expression is evaluated. If the Expression evaluates to null, a NullPointerException is thrown and the entire switch statement completes abruptly for that reason.

Reproducible: Always

Steps to Reproduce:
java.math.RoundingMode x = null;
switch(x) {};

switch((Integer) null) {};

switch((Character) null) {
   default: System.out.println("I've got sunshine!");
}
Comment 1 Olivier Thomann CLA 2010-05-28 08:37:21 EDT
We are too aggressive on the optimization.
For:
switch((Integer) null) {};

we optimize out the whole switch statement when we should at least evaluate the expression.

I'll take a look.

Candidate for 3.6.1.
Comment 2 Olivier Thomann CLA 2010-05-31 11:14:18 EDT
Created attachment 170539 [details]
Proposed fix + regression tests
Comment 3 Olivier Thomann CLA 2010-05-31 11:15:23 EDT
Srikanth, please review.
Comment 4 yogy.namara CLA 2010-05-31 13:07:59 EDT
(In reply to comment #2)
> Created an attachment (id=170539) [details]
> Proposed fix + regression tests

A quick glance doesn't reveal any test on enum. I'd just like to point out that currently, enum seems "special" somehow.

		java.math.RoundingMode mode = null;
		switch (mode) {};
		
		Integer ii = null;
		switch (ii) {};

Given the above snippet, currently Eclipse (mine at least) DOES throw NullPointerException, but only on the second switch.
Comment 5 Srikanth Sankaran CLA 2010-06-01 08:10:05 EDT
(In reply to comment #3)
> Srikanth, please review.

Other than what is already pointed out in comment #4 it
looks good.
Comment 6 Olivier Thomann CLA 2010-06-02 13:24:46 EDT
Created attachment 170844 [details]
Proposed fix + regression tests

Patch to fix the enum case. Under testing.
Comment 7 Olivier Thomann CLA 2010-06-17 12:42:33 EDT
Srikanth, please review the last patch.
Thanks.
Comment 8 Srikanth Sankaran CLA 2010-06-18 05:26:18 EDT
Looks good.
Comment 9 Olivier Thomann CLA 2010-06-18 12:29:26 EDT
Released in HEAD and 3.6 maintenance stream.
Comment 10 Srikanth Sankaran CLA 2010-08-26 02:58:15 EDT
Verified for 3.6.1 RC2 using Build id: M20100825-0800