### Eclipse Workspace Patch 1.0 #P org.eclipse.jdt.core Index: compiler/org/eclipse/jdt/internal/compiler/ast/ConditionalExpression.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/ConditionalExpression.java,v retrieving revision 1.95 diff -u -r1.95 ConditionalExpression.java --- compiler/org/eclipse/jdt/internal/compiler/ast/ConditionalExpression.java 15 Jul 2009 17:42:43 -0000 1.95 +++ compiler/org/eclipse/jdt/internal/compiler/ast/ConditionalExpression.java 1 Sep 2010 15:41:07 -0000 @@ -7,6 +7,7 @@ * * Contributors: * IBM Corporation - initial API and implementation + * Stephen Herrmann - Contribution for bug 133125 *******************************************************************************/ package org.eclipse.jdt.internal.compiler.ast; @@ -318,6 +319,18 @@ if (ifTrueNullStatus == ifFalseNullStatus) { return ifTrueNullStatus; } + // is there a chance of null? -> potentially null + // https://bugs.eclipse.org/bugs/show_bug.cgi?id=133125 + switch (ifTrueNullStatus) { + case FlowInfo.NULL: + case FlowInfo.POTENTIALLY_NULL: + return FlowInfo.POTENTIALLY_NULL; + } + switch (ifFalseNullStatus) { + case FlowInfo.NULL: + case FlowInfo.POTENTIALLY_NULL: + return FlowInfo.POTENTIALLY_NULL; + } return FlowInfo.UNKNOWN; // cannot decide which branch to take, and they disagree } #P org.eclipse.jdt.core.tests.compiler Index: src/org/eclipse/jdt/core/tests/compiler/regression/NullReferenceTest.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/NullReferenceTest.java,v retrieving revision 1.100 diff -u -r1.100 NullReferenceTest.java --- src/org/eclipse/jdt/core/tests/compiler/regression/NullReferenceTest.java 31 Aug 2010 14:58:00 -0000 1.100 +++ src/org/eclipse/jdt/core/tests/compiler/regression/NullReferenceTest.java 1 Sep 2010 15:41:18 -0000 @@ -7,7 +7,7 @@ * * Contributors: * IBM Corporation - initial API and implementation - * Stephan Herrmann - Contribution for bugs 292478, 319201 and 320170 + * Stephan Herrmann - Contribution for bugs 133125, 292478, 319201 and 320170 *******************************************************************************/ package org.eclipse.jdt.core.tests.compiler.regression; @@ -729,17 +729,8 @@ } // null analysis -- conditional expression -// TODO (maxime) fix - may consider simultaneous computation of expression null status -// this case is one of those which raise the need for the simultaneous calculation of -// the null status of an expression and the code analysis of the said expression; this -// case is simplistic: we need a value (here, potentially null), that is *not* carried -// by the current embodiment of the flow info; other cases are less trivial in which -// side effects on variables could introduce errors into after the facts evaluations; -// one possible trick would be to add a slot for this -// other path: use a tainted unknown expression status; does not seem to cope well -// with o = (o == null ? new Object() : o) -// TODO (maxime) https://bugs.eclipse.org/bugs/show_bug.cgi?id=133125 -public void _test0034_conditional_expression() { +// https://bugs.eclipse.org/bugs/show_bug.cgi?id=133125 +public void test0034_conditional_expression() { this.runNegativeTest( new String[] { "X.java", @@ -750,12 +741,90 @@ " o.toString();\n" + " }\n" + "}\n"}, - "----------\n" + - "1. ERROR in X.java (at line 4)\n" + - " o.toString();\n" + - " ^\n" + - "The variable o may be null\n" + - "----------\n"); + "----------\n" + + "1. ERROR in X.java (at line 5)\n" + + " o.toString();\n" + + " ^\n" + + "Potential null pointer access: The variable o may be null at this location\n" + + "----------\n"); +} + +// null analysis -- conditional expression +// https://bugs.eclipse.org/bugs/show_bug.cgi?id=133125 +// variant with constant condition +public void test0034_conditional_expression_2() { + this.runConformTest( + new String[] { + "X.java", + "public class X {\n" + + " boolean b;\n" + + " void foo() {\n" + + " Object o = false ? null : new Object();\n" + + " o.toString();\n" + + " }\n" + + "}\n"}, + ""); +} + +// null analysis -- conditional expression +// https://bugs.eclipse.org/bugs/show_bug.cgi?id=133125 +public void test0034_conditional_expression_3() { + this.runNegativeTest( + new String[] { + "X.java", + "public class X {\n" + + " boolean b;\n" + + " void foo(Object a) {\n" + + " if (a == null) {}\n" + + " Object o = b ? a : new Object();\n" + + " o.toString();\n" + + " }\n" + + "}\n"}, + "----------\n" + + "1. ERROR in X.java (at line 6)\n" + + " o.toString();\n" + + " ^\n" + + "Potential null pointer access: The variable o may be null at this location\n" + + "----------\n"); +} + +// null analysis -- conditional expression +// https://bugs.eclipse.org/bugs/show_bug.cgi?id=133125 +// variant with dependency between condition and expression - LocalDeclaration +// TODO(stephan) cannot analyse this flow dependency +public void _test0034_conditional_expression_4() { + this.runConformTest( + new String[] { + "X.java", + "public class X {\n" + + " boolean b;\n" + + " void foo(Object u) {\n" + + " if (u == null) {}\n" + //taint + " Object o = (u == null) ? new Object() : u;\n" + + " o.toString();\n" + + " }\n" + + "}\n"}, + ""); +} + +// null analysis -- conditional expression +// https://bugs.eclipse.org/bugs/show_bug.cgi?id=133125 +// variant with dependency between condition and expression - Assignment +// TODO(stephan) cannot analyse this flow dependency +public void _test0034_conditional_expression_5() { + this.runConformTest( + new String[] { + "X.java", + "public class X {\n" + + " boolean b;\n" + + " void foo(Object u) {\n" + + " if (u == null) {}\n" + //taint + " Object o;\n" + + " o = (u == null) ? new Object() : u;\n" + + " o.toString();\n" + + " }\n" + + "}\n"}, + ""); } // null analysis -- conditional expression