View | Details | Raw Unified | Return to bug 133125 | Differences between
and this patch

Collapse All | Expand All

(-)compiler/org/eclipse/jdt/internal/compiler/ast/ConditionalExpression.java (+13 lines)
Lines 7-12 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Stephen Herrmann <stephan@cs.tu-berlin.de> -  Contribution for bug 133125
10
 *******************************************************************************/
11
 *******************************************************************************/
11
package org.eclipse.jdt.internal.compiler.ast;
12
package org.eclipse.jdt.internal.compiler.ast;
12
13
Lines 318-323 Link Here
318
	if (ifTrueNullStatus == ifFalseNullStatus) {
319
	if (ifTrueNullStatus == ifFalseNullStatus) {
319
		return ifTrueNullStatus;
320
		return ifTrueNullStatus;
320
	}
321
	}
322
	// is there a chance of null? -> potentially null
323
	// https://bugs.eclipse.org/bugs/show_bug.cgi?id=133125
324
	switch (ifTrueNullStatus) {
325
		case FlowInfo.NULL:
326
		case FlowInfo.POTENTIALLY_NULL:
327
			return FlowInfo.POTENTIALLY_NULL;
328
	}
329
	switch (ifFalseNullStatus) {
330
		case FlowInfo.NULL:
331
		case FlowInfo.POTENTIALLY_NULL:
332
			return FlowInfo.POTENTIALLY_NULL;
333
	}
321
	return FlowInfo.UNKNOWN;
334
	return FlowInfo.UNKNOWN;
322
	// cannot decide which branch to take, and they disagree
335
	// cannot decide which branch to take, and they disagree
323
}
336
}
(-)src/org/eclipse/jdt/core/tests/compiler/regression/NullReferenceTest.java (-18 / +87 lines)
Lines 7-13 Link Here
7
 *
7
 *
8
 * Contributors:
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
9
 *     IBM Corporation - initial API and implementation
10
 *     Stephan Herrmann <stephan@cs.tu-berlin.de> - Contribution for bugs 292478, 319201 and 320170
10
 *     Stephan Herrmann <stephan@cs.tu-berlin.de> - Contribution for bugs 133125, 292478, 319201 and 320170
11
 *******************************************************************************/
11
 *******************************************************************************/
12
package org.eclipse.jdt.core.tests.compiler.regression;
12
package org.eclipse.jdt.core.tests.compiler.regression;
13
13
Lines 729-745 Link Here
729
}
729
}
730
730
731
// null analysis -- conditional expression
731
// null analysis -- conditional expression
732
// TODO (maxime) fix - may consider simultaneous computation of expression null status
732
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=133125
733
// this case is one of those which raise the need for the simultaneous calculation of
733
public void test0034_conditional_expression() {
734
// the null status of an expression and the code analysis of the said expression; this
735
// case is simplistic: we need a value (here, potentially null), that is *not* carried
736
// by the current embodiment of the flow info; other cases are less trivial in which
737
// side effects on variables could introduce errors into after the facts evaluations;
738
// one possible trick would be to add a slot for this
739
// other path: use a tainted unknown expression status; does not seem to cope well
740
// with o = (o ==  null ? new Object() : o)
741
// TODO (maxime) https://bugs.eclipse.org/bugs/show_bug.cgi?id=133125
742
public void _test0034_conditional_expression() {
743
	this.runNegativeTest(
734
	this.runNegativeTest(
744
		new String[] {
735
		new String[] {
745
			"X.java",
736
			"X.java",
Lines 750-761 Link Here
750
			"    o.toString();\n" +
741
			"    o.toString();\n" +
751
			"  }\n" +
742
			"  }\n" +
752
			"}\n"},
743
			"}\n"},
753
		"----------\n" +
744
			"----------\n" +
754
		"1. ERROR in X.java (at line 4)\n" +
745
			"1. ERROR in X.java (at line 5)\n" +
755
		"	o.toString();\n" +
746
			"	o.toString();\n" +
756
		"	^\n" +
747
			"	^\n" +
757
		"The variable o may be null\n" +
748
			"Potential null pointer access: The variable o may be null at this location\n" +
758
		"----------\n");
749
			"----------\n");
750
}
751
752
// null analysis -- conditional expression
753
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=133125
754
// variant with constant condition
755
public void test0034_conditional_expression_2() {
756
	this.runConformTest(
757
		new String[] {
758
			"X.java",
759
			"public class X {\n" +
760
			"  boolean b;\n" +
761
			"  void foo() {\n" +
762
			"    Object o = false ? null : new Object();\n" +
763
			"    o.toString();\n" +
764
			"  }\n" +
765
			"}\n"},
766
			"");
767
}
768
769
// null analysis -- conditional expression
770
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=133125
771
public void test0034_conditional_expression_3() {
772
	this.runNegativeTest(
773
		new String[] {
774
			"X.java",
775
			"public class X {\n" +
776
			"  boolean b;\n" +
777
			"  void foo(Object a) {\n" +
778
			" 	 if (a == null) {}\n" +
779
			"    Object o = b ? a : new Object();\n" +
780
			"    o.toString();\n" +
781
			"  }\n" +
782
			"}\n"},
783
			"----------\n" +
784
			"1. ERROR in X.java (at line 6)\n" +
785
			"	o.toString();\n" +
786
			"	^\n" +
787
			"Potential null pointer access: The variable o may be null at this location\n" +
788
			"----------\n");
789
}
790
791
// null analysis -- conditional expression
792
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=133125
793
// variant with dependency between condition and expression - LocalDeclaration
794
// TODO(stephan) cannot analyse this flow dependency
795
public void _test0034_conditional_expression_4() {
796
	this.runConformTest(
797
		new String[] {
798
			"X.java",
799
			"public class X {\n" +
800
			"  boolean b;\n" +
801
			"  void foo(Object u) {\n" +
802
			"    if (u == null) {}\n" + //taint
803
			"    Object o = (u == null) ? new Object() : u;\n" +
804
			"    o.toString();\n" +
805
			"  }\n" +
806
			"}\n"},
807
			"");
808
}
809
810
// null analysis -- conditional expression
811
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=133125
812
// variant with dependency between condition and expression - Assignment
813
// TODO(stephan) cannot analyse this flow dependency
814
public void _test0034_conditional_expression_5() {
815
	this.runConformTest(
816
		new String[] {
817
			"X.java",
818
			"public class X {\n" +
819
			"  boolean b;\n" +
820
			"  void foo(Object u) {\n" +
821
			"    if (u == null) {}\n" + //taint
822
			"    Object o;\n" +
823
			"    o = (u == null) ? new Object() : u;\n" +
824
			"    o.toString();\n" +
825
			"  }\n" +
826
			"}\n"},
827
			"");
759
}
828
}
760
829
761
// null analysis -- conditional expression
830
// null analysis -- conditional expression

Return to bug 133125