You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by bo...@apache.org on 2016/06/10 12:59:53 UTC

commons-compress git commit: fix COMPRESS-357 with a volatile flag

Repository: commons-compress
Updated Branches:
  refs/heads/master 1dc330f53 -> 8769bb698


fix COMPRESS-357 with a volatile flag


Project: http://git-wip-us.apache.org/repos/asf/commons-compress/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-compress/commit/8769bb69
Tree: http://git-wip-us.apache.org/repos/asf/commons-compress/tree/8769bb69
Diff: http://git-wip-us.apache.org/repos/asf/commons-compress/diff/8769bb69

Branch: refs/heads/master
Commit: 8769bb6980ea9d46f8fbec1fa1075128d6f61936
Parents: 1dc330f
Author: Stefan Bodewig <bo...@apache.org>
Authored: Fri Jun 10 14:59:28 2016 +0200
Committer: Stefan Bodewig <bo...@apache.org>
Committed: Fri Jun 10 14:59:28 2016 +0200

----------------------------------------------------------------------
 src/changes/changes.xml                                 |  4 ++--
 .../compressors/bzip2/BZip2CompressorOutputStream.java  | 12 +++++++-----
 2 files changed, 9 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-compress/blob/8769bb69/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index eb9bb00..d74fd98 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -76,8 +76,8 @@ The <action> type attribute can be add,update,fix,remove.
         used in Apple's iWork 13 files.
       </action>
       <action issue="COMPRESS-357" type="fix" date="2016-05-26">
-        BZip2CompressorOutputStream#finish is now synchronized to
-        avoid a race condition with the finalize method.
+        A race-condition between BZip2CompressorOutputStream's finish
+        and finalize methods could lead to corrupted streams.
       </action>
       <action issue="COMPRESS-351" type="update" date="2016-06-07">
         ZipArchiveInputStream and CpioArchiveInputStream could throw

http://git-wip-us.apache.org/repos/asf/commons-compress/blob/8769bb69/src/main/java/org/apache/commons/compress/compressors/bzip2/BZip2CompressorOutputStream.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/compress/compressors/bzip2/BZip2CompressorOutputStream.java b/src/main/java/org/apache/commons/compress/compressors/bzip2/BZip2CompressorOutputStream.java
index 3bdee5a..483d844 100644
--- a/src/main/java/org/apache/commons/compress/compressors/bzip2/BZip2CompressorOutputStream.java
+++ b/src/main/java/org/apache/commons/compress/compressors/bzip2/BZip2CompressorOutputStream.java
@@ -322,6 +322,7 @@ public class BZip2CompressorOutputStream extends CompressorOutputStream
     private BlockSort blockSorter;
 
     private OutputStream out;
+    private volatile boolean closed;
 
     /**
      * Chooses a blocksize based on the given length of the data to compress.
@@ -392,7 +393,7 @@ public class BZip2CompressorOutputStream extends CompressorOutputStream
 
     @Override
     public void write(final int b) throws IOException {
-        if (this.out != null) {
+        if (!closed) {
             write0(b);
         } else {
             throw new IOException("closed");
@@ -476,8 +477,8 @@ public class BZip2CompressorOutputStream extends CompressorOutputStream
     }
 
 
-    public synchronized void finish() throws IOException {
-        if (out != null) {
+    public void finish() throws IOException {
+        if (!closed) {
             try {
                 if (this.runLength > 0) {
                     writeRun();
@@ -486,6 +487,7 @@ public class BZip2CompressorOutputStream extends CompressorOutputStream
                 endBlock();
                 endCompression();
             } finally {
+                closed = true;
                 this.out = null;
                 this.data = null;
                 this.blockSorter = null;
@@ -495,7 +497,7 @@ public class BZip2CompressorOutputStream extends CompressorOutputStream
 
     @Override
     public void close() throws IOException {
-        if (out != null) {
+        if (!closed) {
             final OutputStream outShadow = this.out;
             finish();
             outShadow.close();
@@ -625,7 +627,7 @@ public class BZip2CompressorOutputStream extends CompressorOutputStream
                                                 + len + ") > buf.length("
                                                 + buf.length + ").");
         }
-        if (this.out == null) {
+        if (closed) {
             throw new IOException("stream closed");
         }