coheigea commented on code in PR #3715:
URL: https://github.com/apache/cxf/pull/3715#discussion_r4564577733


##########
core/src/main/java/org/apache/cxf/transport/common/gzip/GZIPInInterceptor.java:
##########
@@ -112,4 +125,71 @@ public void handleMessage(Message message) {
         }
     }
 
+    private static long getMaxDecompressedSize(Message message) {
+        Object maxSize = 
message.getContextualProperty(GZIP_MAX_DECOMPRESSED_SIZE);
+        if (maxSize instanceof Number) {
+            return ((Number)maxSize).longValue();
+        } else if (maxSize instanceof String) {
+            try {
+                return Long.parseLong(((String)maxSize).trim());
+            } catch (NumberFormatException ex) {
+                LOG.warning("Invalid value for " + GZIP_MAX_DECOMPRESSED_SIZE 
+ ": " + maxSize);
+            }
+        }
+        return DEFAULT_GZIP_MAX_DECOMPRESSED_SIZE;
+    }
+
+    /**
+     * Throws an IOException once more than the given number of bytes has been 
read, to guard against
+     * a small compressed message that decompresses to a very large one.
+     */
+    private static final class SizeLimitedInputStream extends 
FilterInputStream {
+        private final long maxSize;
+        private long count;
+
+        SizeLimitedInputStream(InputStream in, long maxSize) {
+            super(in);
+            this.maxSize = maxSize;
+        }
+
+        @Override
+        public int read() throws IOException {
+            int b = super.read();
+            if (b != -1) {
+                checkSize(1);
+            }
+            return b;
+        }
+
+        @Override
+        public int read(byte[] b, int off, int len) throws IOException {
+            int n = super.read(b, off, len);
+            if (n > 0) {
+                checkSize(n);
+            }
+            return n;
+        }
+
+        @Override
+        public long skip(long n) throws IOException {
+            long skipped = super.skip(n);
+            if (skipped > 0) {
+                checkSize(skipped);
+            }
+            return skipped;
+        }
+
+        @Override
+        public boolean markSupported() {
+            return false;
+        }
+
+        private void checkSize(long n) throws IOException {
+            count += n;
+            if (count > maxSize) {

Review Comment:
   Good point, thanks. I’ve changed it to close the underlying stream as soon 
as the limit is exceeded, so the Inflater is released straight away rather than 
waiting for GC. Any further read throws the same size-limit exception without 
touching the closed stream.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to