Uploaded image for project: 'MINA'
  1. MINA
  2. DIRMINA-829

AbstractIoBuffer.getSlice(int index, int length) has incorrect behavior

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Minor
    • Resolution: Fixed
    • 2.0.2
    • 2.0.5
    • Core
    • None

    Description

      All indexed access function of IoBuffer (getXXX(int index, ...)) supposed to keep IoBuffer state intact. getSlice(int index, int length) method in AbstractIoBuffer class behave differenlty. After its execution buffer posistion is moved to value of index parameter:

          public final IoBuffer getSlice(int index, int length) {
              if (length < 0) {
                  throw new IllegalArgumentException("length: " + length);
              }
              
              int limit = limit();
              
              if (index > limit) {
                  throw new IllegalArgumentException("index: " + index);
              }
              
              int endIndex = index + length;
      
              if (capacity() < endIndex) {
                  throw new IndexOutOfBoundsException("index + length (" + endIndex
                          + ") is greater " + "than capacity (" + capacity() + ").");
              }
      
              clear();
              position(index);
              limit(endIndex);
      
              IoBuffer slice = slice();
              position(index);
              limit(limit);
              return slice;
          }
      

      As you can see, this method checks also resulting slice upper limit against original buffer capcity (not against limit), which I suppose is wrong too.

      I think this method should be changed like this:

          public final IoBuffer getSlice(int index, int length) {
              if (length < 0) {
                  throw new IllegalArgumentException("length: " + length);
              }
              
              int pos = position();
              int limit = limit();
              
              if (index > limit) {
                  throw new IllegalArgumentException("index: " + index);
              }
              
              int endIndex = index + length;
      
              if (endIndex > limit) {
                  throw new IndexOutOfBoundsException("index + length (" + endIndex
                          + ") is greater " + "than limit (" + limit + ").");
              }
      
              clear();
              position(index);
              limit(endIndex);
      
              IoBuffer slice = slice();
              position(pos);
              limit(limit);
              return slice;
          }
      

      Attachments

        Activity

          People

            elecharny Emmanuel Lécharny
            asitnikov Anton Sitnikov
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: