You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ti...@apache.org on 2020/05/28 17:59:43 UTC

svn commit: r1878241 - /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/filter/FlateFilter.java

Author: tilman
Date: Thu May 28 17:59:43 2020
New Revision: 1878241

URL: http://svn.apache.org/viewvc?rev=1878241&view=rev
Log:
PDFBOX-4849: avoid Flate leak, as suggested by Christof Lorenz

Modified:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/filter/FlateFilter.java

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/filter/FlateFilter.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/filter/FlateFilter.java?rev=1878241&r1=1878240&r2=1878241&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/filter/FlateFilter.java (original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/filter/FlateFilter.java Thu May 28 17:59:43 2020
@@ -76,41 +76,47 @@ final class FlateFilter extends Filter
             inflater.setInput(buf,0,read);
             byte[] res = new byte[1024];
             boolean dataWritten = false;
-            while (true) 
-            { 
-                int resRead = 0;
-                try
-                {
-                    resRead = inflater.inflate(res);
-                }
-                catch(DataFormatException exception)
-                {
-                    if (dataWritten)
+            try
+            {
+                while (true) 
+                { 
+                    int resRead = 0;
+                    try
                     {
-                        // some data could be read -> don't throw an exception
-                        LOG.warn("FlateFilter: premature end of stream due to a DataFormatException");
-                        break;
+                        resRead = inflater.inflate(res);
                     }
-                    else
+                    catch(DataFormatException exception)
                     {
-                        // nothing could be read -> re-throw exception
-                        throw exception;
+                        if (dataWritten)
+                        {
+                            // some data could be read -> don't throw an exception
+                            LOG.warn("FlateFilter: premature end of stream due to a DataFormatException");
+                            break;
+                        }
+                        else
+                        {
+                            // nothing could be read -> re-throw exception
+                            throw exception;
+                        }
                     }
+                    if (resRead != 0) 
+                    { 
+                        out.write(res,0,resRead);
+                        dataWritten = true;
+                        continue; 
+                    } 
+                    if (inflater.finished() || inflater.needsDictionary() || in.available() == 0) 
+                    {
+                        break;
+                    } 
+                    read = in.read(buf); 
+                    inflater.setInput(buf,0,read);
                 }
-                if (resRead != 0) 
-                { 
-                    out.write(res,0,resRead);
-                    dataWritten = true;
-                    continue; 
-                } 
-                if (inflater.finished() || inflater.needsDictionary() || in.available() == 0) 
-                {
-                    break;
-                } 
-                read = in.read(buf); 
-                inflater.setInput(buf,0,read);
             }
-            inflater.end();
+            finally
+            {
+                inflater.end();
+            }
         }
         out.flush();
     }