This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-io.git


The following commit(s) were added to refs/heads/master by this push:
     new af5f764  Fix infinite loops in ObservableInputStream read(*) when an 
exception is caught but not re-thrown.
af5f764 is described below

commit af5f764aa3eef21f4bd7d8b404c5d6d3b7c02eab
Author: Gary Gregory <gardgreg...@gmail.com>
AuthorDate: Mon Feb 1 16:30:52 2021 -0500

    Fix infinite loops in ObservableInputStream read(*) when an exception is
    caught but not re-thrown.
---
 src/changes/changes.xml                            |  3 +++
 .../commons/io/input/ObservableInputStream.java    | 14 ++++++++------
 .../io/input/ObservableInputStreamTest.java        | 22 ++++++++++++++++++++++
 3 files changed, 33 insertions(+), 6 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index bc2d032..7deebbc 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -108,6 +108,9 @@ The <action> type attribute can be add,update,fix,remove.
       <action issue="IO-705" dev="ggregory" type="fix" due-to="Hao Zhong, Gary 
Gregory">
         LockableFileWriter.close() should fail when the lock file cannot be 
deleted.
       </action>
+      <action issue="IO-705" dev="ggregory" type="fix" due-to="Hao Zhong, Gary 
Gregory">
+        Fix infinite loops in ObservableInputStream read(*) when an exception 
is caught but not re-thrown.
+      </action>
       <!-- ADD -->
       <action dev="ggregory" type="add" due-to="Gary Gregory">
         Add FileSystemProviders class.
diff --git 
a/src/main/java/org/apache/commons/io/input/ObservableInputStream.java 
b/src/main/java/org/apache/commons/io/input/ObservableInputStream.java
index d7d9191..13dd25b 100644
--- a/src/main/java/org/apache/commons/io/input/ObservableInputStream.java
+++ b/src/main/java/org/apache/commons/io/input/ObservableInputStream.java
@@ -244,6 +244,7 @@ public class ObservableInputStream extends ProxyInputStream 
{
     private void notify(final byte[] buffer, final int offset, int result, 
IOException ioe) throws IOException {
         if (ioe != null) {
             noteError(ioe);
+            throw ioe;
         } else if (result == EOF) {
             noteFinished();
         } else if (result > 0) {
@@ -257,11 +258,12 @@ public class ObservableInputStream extends 
ProxyInputStream {
         IOException ioe = null;
         try {
             result = super.read();
-        } catch (final IOException pException) {
-            ioe = pException;
+        } catch (final IOException ex) {
+            ioe = ex;
         }
         if (ioe != null) {
             noteError(ioe);
+            throw ioe;
         } else if (result == EOF) {
             noteFinished();
         } else {
@@ -276,8 +278,8 @@ public class ObservableInputStream extends ProxyInputStream 
{
         IOException ioe = null;
         try {
             result = super.read(buffer);
-        } catch (final IOException pException) {
-            ioe = pException;
+        } catch (final IOException ex) {
+            ioe = ex;
         }
         notify(buffer, 0, result, ioe);
         return result;
@@ -289,8 +291,8 @@ public class ObservableInputStream extends ProxyInputStream 
{
         IOException ioe = null;
         try {
             result = super.read(buffer, offset, length);
-        } catch (final IOException pException) {
-            ioe = pException;
+        } catch (final IOException ex) {
+            ioe = ex;
         }
         notify(buffer, offset, result, ioe);
         return result;
diff --git 
a/src/test/java/org/apache/commons/io/input/ObservableInputStreamTest.java 
b/src/test/java/org/apache/commons/io/input/ObservableInputStreamTest.java
index b7920c8..b8ff336 100644
--- a/src/test/java/org/apache/commons/io/input/ObservableInputStreamTest.java
+++ b/src/test/java/org/apache/commons/io/input/ObservableInputStreamTest.java
@@ -19,6 +19,7 @@ package org.apache.commons.io.input;
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertNotEquals;
 import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
@@ -125,6 +126,27 @@ public class ObservableInputStreamTest {
 
     }
 
+    @Test
+    public void testBrokenInputStreamRead() throws IOException {
+        try (final ObservableInputStream ois = new ObservableInputStream(new 
BrokenInputStream())) {
+            assertThrows(IOException.class, () -> ois.read());
+        }
+    }
+
+    @Test
+    public void testBrokenInputStreamReadBuffer() throws IOException {
+        try (final ObservableInputStream ois = new ObservableInputStream(new 
BrokenInputStream())) {
+            assertThrows(IOException.class, () -> ois.read(new byte[1]));
+        }
+    }
+
+    @Test
+    public void testBrokenInputStreamReadSubBuffer() throws IOException {
+        try (final ObservableInputStream ois = new ObservableInputStream(new 
BrokenInputStream())) {
+            assertThrows(IOException.class, () -> ois.read(new byte[2], 0, 1));
+        }
+    }
+
     /**
      * Tests that {@link Observer#data(int)} is called.
      */

Reply via email to