uber / uber/NullAway

System.exit()

Open
#314 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement lowpriority
Dominant language
Java
Stars
4.1k
Forks
370
Avg merge
1d 13h
Merged PRs (30d)
68

Description

I've found a case, that is not handled properly (I think). I expect the JVM to shutdown when I call System.exit(1); and therefore I'd expect this statement to behave as a control-flow guard, similar to putting there a return;. But it is ignored.

@Nullable CommandsGroup commandBean = getCommandBean(commandNameParts[0], context);
if (commandBean == null) {
    System.err.println("Command " + commandName + " not found.");
    System.exit(1);
}

commandBean.runCommand(commandNameParts[1], context.getBean(ApplicationArguments.class));

produces

[ERROR] ConsoleApplication.java:[54,24] [NullAway] dereferenced expression commandBean is @Nullable
    (see http://t.uber.com/nullaway )

The fix in my code is trivial

@Nullable CommandsGroup commandBean = getCommandBean(commandNameParts[0], context);
if (commandBean == null) {
    System.err.println("Command " + commandName + " not found.");
    System.exit(1);

} else {
    commandBean.runCommand(commandNameParts[1], context.getBean(ApplicationArguments.class));
}

Alternatively this

@Nullable CommandsGroup commandBean = getCommandBean(commandNameParts[0], context);
if (commandBean == null) {
    System.err.println("Command " + commandName + " not found.");
    System.exit(1);
    return;
}

commandBean.runCommand(commandNameParts[1], context.getBean(ApplicationArguments.class));

But IMHO this is something the NullAway should handle.

I wasn't looking very hard, but I don't believe there is a way to escape the System.exit(1); (apart from the shutdown handler hook, that is executed in different thread and there doesn't affect the method and thread calling the exit).

maven: effective pom
    <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.0</version>
        <dependencies>
          <dependency>
            <groupId>org.codehaus.plexus</groupId>
            <artifactId>plexus-compiler-javac-errorprone</artifactId>
            <version>2.8.5</version>
            <scope>compile</scope>
          </dependency>
          <dependency>
            <groupId>com.google.errorprone</groupId>
            <artifactId>error_prone_core</artifactId>
            <version>2.3.3</version>
            <scope>compile</scope>
          </dependency>
        </dependencies>
        <configuration>
          <compilerId>javac-with-errorprone</compilerId>
          <forceJavacCompilerUse>true</forceJavacCompilerUse>
          <source>1.8</source>
          <target>1.8</target>
          <compilerArgs>
            <arg>-Werror</arg>
            <arg>-Xlint:all,-fallthrough,-processing,-serial,-classfile</arg>
            <arg>-parameters</arg>
            <arg>-XDcompilePolicy=simple</arg>
            <arg>-Xep:NullAway:ERROR</arg>
            <arg>-XepOpt:NullAway:AnnotatedPackages=com.cogvio.dl</arg>
            <arg>-XepOpt:NullAway:TreatGeneratedAsUnannotated=true</arg>
            <arg>-XepOpt:NullAway:AcknowledgeRestrictiveAnnotations=true</arg>
          </compilerArgs>
          <annotationProcessorPaths>
            <path>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-configuration-processor</artifactId>
              <version>2.2.0.M1</version>
            </path>
            <path>
              <groupId>org.springframework</groupId>
              <artifactId>spring-context-indexer</artifactId>
              <version>5.2.0.M1</version>
            </path>
            <path>
              <groupId>com.uber.nullaway</groupId>
              <artifactId>nullaway</artifactId>
              <version>0.7.2</version>
            </path>
          </annotationProcessorPaths>
          <showWarnings>true</showWarnings>
          <parameters>true</parameters>
        </configuration>
      </plugin>

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.

Research direction

Start with NullAway's analysis of the Java System.exit(1) call and determine whether it models that call as terminating control flow. Add coverage for the shown nullable dereference case and verify that the diagnostic is not emitted after exit; the issue names no repository file or test.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.