JDK-7011804 : SequenceInputStream with lots of empty substreams can cause StackOverflowError
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.io
  • Affected Version: 6u23
  • Priority: P4
  • Status: Closed
  • Resolution: Fixed
  • OS: windows_7
  • CPU: x86
  • Submitted: 2011-01-12
  • Updated: 2015-01-21
  • Resolved: 2014-03-13
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 8 JDK 9
8u40Fixed 9 b06Fixed
Description
FULL PRODUCT VERSION :
Linux:
Java(TM) SE Runtime Environment (build 1.6.0_20-b02)
Java HotSpot(TM) 64-Bit Server VM (build 16.3-b01, mixed mode)

Windows:
java version "1.6.0_23"
Java(TM) SE Runtime Environment (build 1.6.0_23-b05)
Java HotSpot(TM) Client VM (build 19.0-b09, mixed mode, sharing)

ADDITIONAL OS VERSION INFORMATION :
Linux <name goes here> 2.6.18-194.3.1.el5 #1 SMP Thu May 13 13:08:30 EDT 2010 x86_64 x86_64 x86_64 GNU/Linux

Microsoft Windows [Version 6.1.7600]

A DESCRIPTION OF THE PROBLEM :
The SequenceInputStream uses recursion to read additional data when there is more space to fill in the provided buffer.

In my scenario, I have an Enumeration<FileInputStream> of about 16k files that are all zero bytes of length. I then use the SequenceInputStream#read(byte[], int, int) method to read bytes from the streams.

Because after each zero-byte read the next stream is retrieved from the Enumeration and then a recursive call to the same read(byte[], int, int) method is done, I get a SOE.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
No SOE.
ACTUAL -
2011.01.11 21:22:58 CET FATAL [com.leanapps.life.process.general.jms.JMSProcessQueueBean] - callFinalize: Exception:
javax.transaction.TransactionRolledbackException: EJB Exception: : java.lang.StackOverflowError
        at java.io.FileInputStream.<init>(FileInputStream.java:66)
        at com.leanapps.life.util.FileUtil$ConcatenatableFiles.nextElement(FileUtil.java:269)
        at com.leanapps.life.util.FileUtil$ConcatenatableFiles.nextElement(FileUtil.java:214)
        at java.io.SequenceInputStream.nextStream(SequenceInputStream.java:93)
        at java.io.SequenceInputStream.read(SequenceInputStream.java:193)
        at java.io.SequenceInputStream.read(SequenceInputStream.java:194)
        at java.io.SequenceInputStream.read(SequenceInputStream.java:194)
        at java.io.SequenceInputStream.read(SequenceInputStream.java:194)
        at java.io.SequenceInputStream.read(SequenceInputStream.java:194)
        at java.io.SequenceInputStream.read(SequenceInputStream.java:194)
        at java.io.SequenceInputStream.read(SequenceInputStream.java:194)
        at java.io.SequenceInputStream.read(SequenceInputStream.java:194)
        at java.io.SequenceInputStream.read(SequenceInputStream.java:194)
        at java.io.SequenceInputStream.read(SequenceInputStream.java:194)
        at java.io.SequenceInputStream.read(SequenceInputStream.java:194)
        at java.io.SequenceInputStream.read(SequenceInputStream.java:194)
        at java.io.SequenceInputStream.read(SequenceInputStream.java:194)
        at java.io.SequenceInputStream.read(SequenceInputStream.java:194)
        at java.io.SequenceInputStream.read(SequenceInputStream.java:194)
        at java.io.SequenceInputStream.read(SequenceInputStream.java:194)
        at java.io.SequenceInputStream.read(SequenceInputStream.java:194)
        at java.io.SequenceInputStream.read(SequenceInputStream.java:194)
        at java.io.SequenceInputStream.read(SequenceInputStream.java:194)
        at java.io.SequenceInputStream.read(SequenceInputStream.java:194)

.. and so on, and so on ...

REPRODUCIBILITY :
This bug can be reproduced always.

Comments
Review with approvals: http://mail.openjdk.java.net/pipermail/core-libs-dev/2014-March/025481.html http://mail.openjdk.java.net/pipermail/core-libs-dev/2014-March/025484.html
04-03-2014

EVALUATION This seems to be an issue since jdk1.0. Unlikely to an issue for anything other than the extreme usage in the description.
18-01-2011