Skip to content

Commit

Permalink
EmptyStackException when invoking a static method on a null literal i…
Browse files Browse the repository at this point in the history
…n a ternary operator (#3043)

* Fixes #3042
  • Loading branch information
srikanth-sankaran authored Oct 2, 2024
1 parent 3357118 commit 03c1de7
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -331,10 +331,12 @@ public void generateCode(
// End of if statement
endifLabel.place();
}
if (this.valueIfFalse.resolvedType == TypeBinding.NULL) {
if (!this.resolvedType.isBaseType()) {
codeStream.operandStack.pop(TypeBinding.NULL);
codeStream.operandStack.push(this.resolvedType);
if (valueRequired) {
if (this.valueIfFalse.resolvedType == TypeBinding.NULL) {
if (!this.resolvedType.isBaseType()) {
codeStream.operandStack.pop(TypeBinding.NULL);
codeStream.operandStack.push(this.resolvedType);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -657,4 +657,92 @@ public void testIssue2677_2() {
},
"42");
}

// https://github.com/eclipse-jdt/eclipse.jdt.core/issues/3042
// java.util.EmptyStackException: null when invoking a static method on a null string literal in a ternary operator
public void testIssue3042() {
if (this.complianceLevel < ClassFileConstants.JDK14)
return;
this.runConformTest(
new String[] {
"X.java",
"""
public class X {
static void parseFailure(X o) {
(o != null ? o : null).bar();
}
static void bar() {
System.out.println("Bar!");
}
public static void main(String[] args) {
parseFailure(new X());
parseFailure(null);
}
}
"""
},
"Bar!\nBar!");
}

public void testIssue3042_2() {
if (this.complianceLevel < ClassFileConstants.JDK14)
return;
this.runConformTest(
new String[] {
"X.java",
"""
public class X {
void parseFailure(X o) {
(o != null ? o : null).bar();
}
void bar() {
System.out.println("Bar!");
}
public static void main(String[] args) {
new X().parseFailure(new X());
try {
new X().parseFailure(null);
} catch (NullPointerException npe) {
System.out.println("NPE!");
}
}
}
"""
},
"Bar!\nNPE!");
}

public void testIssue3042_3() {
if (this.complianceLevel < ClassFileConstants.JDK14)
return;
this.runConformTest(
new String[] {
"X.java",
"""
public class X {
void parseSuccess(X o) {
(o == null ? null : o).bar();
((X) null).bar();
}
static void bar() {
System.out.println("Bar!");
}
public static void main(String[] args) {
new X().parseSuccess(new X());
new X().parseSuccess(null);
}
}
"""
},
"Bar!\nBar!\nBar!\nBar!");
}
}

0 comments on commit 03c1de7

Please sign in to comment.