Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SystemConsoleNull should consider the case where System.console() == null #4355

Open
anthonyvdotbe opened this issue Mar 29, 2024 · 3 comments

Comments

@anthonyvdotbe
Copy link
Contributor

https://errorprone.info/bugpattern/SystemConsoleNull says:

System.console() no longer returns null in JDK 22 and newer versions

However, it may still return null, both from the specification and actual implementation.

So I propose to:

(a) rephrase the above sentence to

System.console() returns null in fewer cases in JDK 22 and newer versions

(b) update the code example to handle the case where System.console() == null:

  @SuppressWarnings("SystemConsoleNull") // https://errorprone.info/bugpattern/SystemConsoleNull
  private static boolean systemConsoleIsTerminal() {
    Console systemConsole = System.console();
    if (Runtime.version().feature() < 22) {
      return systemConsole != null;
    }
    try {
      return (systemConsole != null) && (Boolean) Console.class.getMethod("isTerminal").invoke(systemConsole);
    } catch (ReflectiveOperationException e) {
      throw new LinkageError(e.getMessage(), e);
    }
  }
@cushon
Copy link
Collaborator

cushon commented Mar 29, 2024

Thanks, 'no longer returns null' could have been something like 'no longer returns null when the standard streams are redirected or connected to a virtual terminal'

Can the implementation return null, though? I'm not seeing how the implementation could return null in https://github.com/openjdk/jdk/blob/20cb6e786fbf6d924c509e28d6fded86d61a5f84/src/java.base/share/classes/java/io/Console.java#L431-L435

@anthonyvdotbe
Copy link
Contributor Author

anthonyvdotbe commented Mar 30, 2024

Can the implementation return null, though? I'm not seeing how the implementation could return null in https://github.com/openjdk/jdk/blob/20cb6e786fbf6d924c509e28d6fded86d61a5f84/src/java.base/share/classes/java/io/Console.java#L431-L435

At line 431, c can be null and istty can be false, which causes c to still be null at line 435. Or am I missing something?

@cpovirk
Copy link
Member

cpovirk commented Mar 30, 2024

Huh, thanks. I'd misread the code, too. Indeed, I can produce a null return:

$ cat SystemConsole.java
public class SystemConsole {
  public static void main(String[] args) {
    System.err.println(System.console());
  }
}

$ javac SystemConsole.java && ~/jdk-22-ea+19/bin/java -Djdk.console=nosuchthing SystemConsole > /dev/null
null

I'm hoping that that's at least possible only in the case that the user sets jdk.console (as above), but I wouldn't want to make further assumptions :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants