Uploaded image for project: 'Commons Compress'
  1. Commons Compress
  2. COMPRESS-357

BZip2CompressorOutputStream can affect output stream incorrectly

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 1.9, 1.11
    • Fix Version/s: 1.12
    • Component/s: Compressors
    • Labels:
    • Environment:

      multithreaded

    • Flags:
      Patch, Important

      Description

      BZip2CompressorOutputStream has an unsynchronized finished() method, and an unsynchronized finalize method. Finish checks to see if the output stream is null, and if it is not it calls various methods, some of which write to the output stream.

      Now, consider something like this sequence.

      BZip2OutputStream s = ...
      ...
      s.close();
      s = null;

      After the s = null, the stream is garbage. At some point the garbage collector call finalize(), which calls finish(). But, since the GC may be on a different thread, there is no guarantee that the assignment this.out = null in finish() has actually been made visible to the GC thread, which results in bad data in the output stream.

      This is not a theoretical problem; In a part of a large project I'm working on, this happens about 2% of the time.

      The fixes are simple

      1) synchronize finish() or

      2) don't call finish from finalize().

      A workaround is to derive a class and override the finalize() method.

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              rashapiro83 Richard Shapiro
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Time Tracking

                Estimated:
                Original Estimate - 1h
                1h
                Remaining:
                Remaining Estimate - 1h
                1h
                Logged:
                Time Spent - Not Specified
                Not Specified