typetools / typetools/checker-framework

False negative: Nullness Checker allows leaking implicit `@UnderInitialization this` to inner class c'tor

Open
#3,407 4 comments 1 reaction 1 assignee View on GitHub

@smillst is already working on this.

Since Dec 3, 2020.

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

Description

Nullness Checker apparently does not verify the implicit reference to enclosing class instance.

Let's consider the following class:

class SampleClass {
  final InnerClass inner;
  final String foo;

  SampleClass() {
    this.inner = new InnerClass();
    this.inner.bar(); /// WHOOPS... NPE
    this.foo = "Hello world";
  }

  class InnerClass {
    String bar() {
      return foo.substring(1);
    }
  }
}

This compiles with no error, despite clearly leading to an NPE in the runtime.

A nearly equivalent snippet that uses a static nested class instead:

class SampleClass {
  final InnerClass inner;
  final String foo;

  SampleClass() {
    this.inner = new InnerClass(this); // Ok, NullnessChecker raises an error
    this.inner.bar();
    this.foo = "Hello world";
  }

  static class InnerClass {
    SampleClass outer;
    InnerClass(SampleClass outer) {
      this.outer = outer;
    }
    String bar() {
      return outer.foo.substring(1);
    }
  }
}

fails as expected on nullness:assignment.type.incompatible and argument.type.incompatible.

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.