]> source.dussan.org Git - poi.git/commitdiff
Bug 61550: Add more information to exception text and verify that it is thrown
authorDominik Stadler <centic@apache.org>
Mon, 1 Jan 2018 14:39:26 +0000 (14:39 +0000)
committerDominik Stadler <centic@apache.org>
Mon, 1 Jan 2018 14:39:26 +0000 (14:39 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1819772 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/util/LittleEndianByteArrayInputStream.java
src/testcases/org/apache/poi/util/TestLittleEndianStreams.java

index ff88e7164a8d1a3a71235c5c57f831a071c017ed..fcb44f653eb1835ac35341bdd9bf4b31bc6c54c7 100644 (file)
@@ -37,7 +37,8 @@ public class LittleEndianByteArrayInputStream extends ByteArrayInputStream imple
 
        protected void checkPosition(int i) {
                if (i > count - pos) {
-                       throw new RuntimeException("Buffer overrun");
+                       throw new RuntimeException("Buffer overrun, having " + count + " bytes in the stream and position is at " + pos +
+                                       ", but trying to increment position by " + i);
                }
        }
 
index c05e365a9236a11b680ac6401669fcc52dfd7009..1b327669ae54f9cfe0cc5595a1896859ec14dd07 100644 (file)
@@ -81,4 +81,20 @@ public final class TestLittleEndianStreams extends TestCase {
                assertEquals(0x33, lei.readUByte());
                assertEquals(0, lei.available());
        }
+
+       public void testBufferOverrun() {
+               byte[] srcBuf = HexRead.readFromString("99 88 77");
+               LittleEndianInput lei = new LittleEndianByteArrayInputStream(srcBuf);
+
+               // do initial read to increment the read index beyond zero
+               assertEquals(0x8899, lei.readUShort());
+
+               // only one byte left, so this should fail
+               try {
+                       lei.readFully(new byte[4]);
+                       fail("Should catch exception here");
+               } catch (RuntimeException e) {
+                       assertTrue(e.getMessage().contains("Buffer overrun"));
+               }
+       }
 }