Commons IO
  1. Commons IO
  2. IO-302

ArrayIndexOutOfBoundsException in BOMInputStream when reading a file without BOM multiple times

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 2.1
    • Fix Version/s: 2.2
    • Component/s: Streams/Writers
    • Labels:
      None
    • Environment:

      Win7 64bit, Java 6 32bit

      Description

      Resetting the BOMInputStream doesn't reset the fbLength member variable. This causes fbLength to grow bigger than the firstBytes array (when the file doesn't contain a BOM), which leads to an ArrayIndexOutOfBoundsException in the readFirstBytes method.

      The attached test case reveals the problem.

      1. IO-302.patch
        2 kB
        Marcos Vinícius da Silva
      2. Test.java
        2 kB
        Jan Steuerwald
      3. testfileBOM.xml
        6 kB
        Jan Steuerwald
      4. testfileNoBOM.xml
        6 kB
        Jan Steuerwald

        Activity

        Gary Gregory made changes -
        Status Resolved [ 5 ] Closed [ 6 ]
        Gary Gregory made changes -
        Status Open [ 1 ] Resolved [ 5 ]
        Assignee Gary D. Gregory [ garydgregory ]
        Fix Version/s 2.2 [ 12318448 ]
        Resolution Fixed [ 1 ]
        Marcos Vinícius da Silva made changes -
        Attachment IO-302.patch [ 12515527 ]
        Jan Steuerwald made changes -
        Description Resetting the BOMInputStream doesn't reset the _fbLength_ member variable. This causes _fbLength_ to grow bigger than the _firstBytes_ array (when the file doesn't contain a BOM), which leads to an ArrayIndexOutOfBoundsException in the _readFirstBytes_ method.

        The following test code reveals the problem:

        {code:title=Test.java}
        import java.io.InputStream;

        import org.apache.commons.io.input.BOMInputStream;

        public class Test {

        public static void main(String[] args) {
        try {
        // read the file with BOM twice - works
        readFile("testfileWithBOM.xml");
        // read the file without BOM twice - crashes
        readFile("testfileWithoutBOM.xml");
        } catch (Exception e) {
        e.printStackTrace();
        }
        }

        private static void readFile(String testFile) throws Exception {

        InputStream inputStream = Test.class.getClassLoader().getResourceAsStream(testFile);
        BOMInputStream bomStream = new BOMInputStream(inputStream);

        bomStream.mark(1000000);
        // read for the first time => ok
        int bytes = 0;
        byte[] bytesFromStream = new byte[100];
        do {
        bytes = bomStream.read(bytesFromStream);
        } while (bytes > 0);

        // reset and read a second time => crashes when file has no BOM
        bomStream.reset();
        do {
        bytes = bomStream.read(bytesFromStream);
        } while (bytes > 0);
        }
        }
        {code}
        Resetting the BOMInputStream doesn't reset the _fbLength_ member variable. This causes _fbLength_ to grow bigger than the _firstBytes_ array (when the file doesn't contain a BOM), which leads to an ArrayIndexOutOfBoundsException in the _readFirstBytes_ method.

        The attached test case reveals the problem.
        Jan Steuerwald made changes -
        Attachment testfileBOM.xml [ 12514466 ]
        Attachment testfileNoBOM.xml [ 12514467 ]
        Jan Steuerwald made changes -
        Field Original Value New Value
        Attachment Test.java [ 12514465 ]
        Jan Steuerwald created issue -

          People

          • Assignee:
            Gary Gregory
            Reporter:
            Jan Steuerwald
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development