Uploaded image for project: 'Commons IO'
  1. Commons IO
  2. IO-825

Some methods in EndianUtils throws IndexOutOfBoundsException when the provided byte array does not have enough data

Attach filesAttach ScreenshotVotersWatch issueWatchersCreate sub-taskLinkCloneUpdate Comment AuthorReplace String in CommentUpdate Comment VisibilityDelete Comments
    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Minor
    • Resolution: Fixed
    • None
    • 2.16.0
    • None
    • None

    Description

      In `EndianUtils` class, the method for handling swappedShort / swappedInteger / swappedLong from or to byte array assumes that the byte array still has enough space or data with the provided offset for an Integer / Long / Short reading or writing. Thus a byte array with a much shorter length could make the code throw IndexOutOfBoundsException. A possible fix is adding a validation to ensure the byte array has enough data space before processing.

      Some example code snippets are attached below.

      public static void writeSwappedInteger(final byte[] data, final int offset, final int value) {
          data[offset + 0] = (byte) (value >> 0 & 0xff);
          data[offset + 1] = (byte) (value >> 8 & 0xff);
          data[offset + 2] = (byte) (value >> 16 & 0xff);
          data[offset + 3] = (byte) (value >> 24 & 0xff);
      } 
      public static int readSwappedUnsignedShort(final byte[] data, final int offset) {
          return ((data[offset + 0] & 0xff) << 0) + ((data[offset + 1] & 0xff) << 8);
      }
      

      Attachments

        Activity

          This comment will be Viewable by All Users Viewable by All Users
          Cancel

          People

            Unassigned Unassigned
            arthur.chan Sheung Chi Chan
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Slack

                Issue deployment