typetools / typetools/checker-framework

Non null-safe assignment when dealing with wildcards

Open
#6,881 0 comments 0 reactions 1 assignee View on GitHub

@smillst is already working on this.

Since Nov 1, 2024.

False Negative (missing warning or unsoundness)
Dominant language
Java
Stars
1.1k
Forks
440
Avg merge
1d 12h
Merged PRs (30d)
134

Description

Command
javac -J--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED \
  -J--add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED \
  -J--add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED \
  -J--add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED \
  -J--add-exports=jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED \
  -J--add-exports=jdk.compiler/com.sun.tools.javac.processing=ALL-UNNAMED \
  -J--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED \
  -J--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED \
  -J--add-opens=jdk.compiler/com.sun.tools.javac.comp=ALL-UNNAMED \
-processor org.checkerframework.checker.nullness.NullnessChecker \
-cp checker-framework/checker/dist/checker.jar -Aignorejdkastub Test.java
File
import java.util.*;
import org.checkerframework.checker.nullness.qual.*;

public class Test {

  static public void main(String[] args) {
      List<List<? extends Number>> lists = new ArrayList<>();
      List<? super List<? extends Number>> y = lists;
      // This assignment is not null-safe
      List<? super @Nullable List<? extends Number>> x = y;
      x.add(null);
      lists.get(0).toString();
  }
}
Actual behavior

The code passes the checks of the checker, but there's NPE

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "Object.toString()" because the return value of "java.util.List.get(int)" is null
        at Test.main(Test.java:12)
Expected behavior

The assignment List<? super @Nullable List<? extends Number>> x = y; should have been rejected.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.