Closing System.out/err on Windows can cause JVM stdout/err to be directed to arbitrary files

XMLWordPrintable

    • Type: Bug
    • Resolution: Unresolved
    • Priority: P4
    • tbd
    • Affects Version/s: 26
    • Component/s: hotspot
    • windows

      As per JDK-8371944, some application may close System.out (usually inadvertently by naive code like the following):

      try (var err = new PrintWriter(System.err);
            var out = new PrintWriter(System.out)) {
           out.println("Hello Confused World");
      }

      This has the side effect of closing the JVM's stdout/stderr. On Windows we access the output device via a HANDLE which is an opaque 64-bit integer value. When System.out/err is closed by the Java code, the HANDLE value becomes available for re-use - e.g for a file. If that happens then the VM's use of stdout/stderr will go to that file. On non-Windows file descriptors 1 and 2 never get re-assigned so no similar problem occurs there (though the VM will still try writing to a closed fd).

      Whilst this is likely "day one" behaviour it seems not to have been clearly reported as a bug - likely due to the low probability of closing System.out/err and the even lower probability that the handle gets re-assigned to a newly opened file.

            Assignee:
            Unassigned
            Reporter:
            David Holmes
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated: