Uploaded image for project: 'Commons Compress'
  1. Commons Compress
  2. COMPRESS-363

Overflow in BitInputStream

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 1.12
    • Fix Version/s: 1.13
    • Component/s: Compressors
    • Labels:
      None

      Description

      in Class BitInputStream.java(\src\main\java\org\apache\commons\compress\utils),
      funcion:

      public long readBits(final int count) throws IOException {
      if (count < 0 || count > MAXIMUM_CACHE_SIZE)

      { throw new IllegalArgumentException("count must not be negative or greater than " + MAXIMUM_CACHE_SIZE); }

      while (bitsCachedSize < count) {
      final long nextByte = in.read();
      if (nextByte < 0)

      { return nextByte; }

      if (byteOrder == ByteOrder.LITTLE_ENDIAN)

      { bitsCached |= (nextByte << bitsCachedSize); }

      else

      { bitsCached <<= 8; bitsCached |= nextByte; }

      bitsCachedSize += 8;
      }

      final long bitsOut;
      if (byteOrder == ByteOrder.LITTLE_ENDIAN)

      { bitsOut = (bitsCached & MASKS[count]); bitsCached >>>= count; }

      else

      { bitsOut = (bitsCached >> (bitsCachedSize - count)) & MASKS[count]; }

      bitsCachedSize -= count;
      return bitsOut;
      }

      I think here "bitsCached |= (nextByte << bitsCachedSize);" will overflow in some cases. for example, below is a test case:

      public static void test() {

      ByteArrayInputStream in = new ByteArrayInputStream(new byte[]

      {87, 45, 66, 15, 90, 29, 88, 61, 33, 74}

      );
      BitInputStream bin = new BitInputStream(in, ByteOrder.LITTLE_ENDIAN);
      try

      { long ret = bin.readBits(5); ret = bin.readBits(63); ret = bin.readBits(12); }

      catch (Exception e)

      { e.printStackTrace(); }

      }

      overflow occur in "bin.readBits(63);" , so ,result in wrong result from "bin.readBits(12);"

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              fredwang_00 fredwang_00
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: