Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4505257

Race can lead to NullPointerException in BufferedInputStream.close()

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.4.0
    • core-libs
    • generic
    • generic



      Name: nt126004 Date: 09/20/2001


      java version "1.4.0-beta2"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.0-beta2-b77)
      Java HotSpot(TM) Client VM (build 1.4.0-beta2-b77, mixed mode)


      Reading through the code to BufferedInputStream, I notice that close is implemented as follows:

          public void close() throws IOException {
              if (in == null)
                  return;
              in.close();
              in = null;
              buf = null;
          }

      There are many problems with this code:

      1) Close is accessing/modifying the shared variables in and buf outside of a
      synchronized block

      2) Two threads could call close simultaneously. One thread can set 'in' to null
      right before the second thread reads 'in' to call in.close()

      3) Setting in to null could cause other methods to throw a NullPointerException
      instead of an IOException.

      The answer to this problem is not to make close synchronized. This means that
      close could hang indefinitely if some other thread were hanging in read. Probably the best thing to do is to get rid of the in = null and buf = null.

      This will require rewriting ensureOpen too.
      (Review ID: 132314)
      ======================================================================

            mmcclosksunw Michael Mccloskey (Inactive)
            nthompsosunw Nathanael Thompson (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: