You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by jp...@apache.org on 2012/11/21 15:10:07 UTC

svn commit: r1412115 - in /lucene/dev/branches/branch_4x/lucene: CHANGES.txt core/src/java/org/apache/lucene/util/PagedBytes.java core/src/test/org/apache/lucene/util/TestPagedBytes.java

Author: jpountz
Date: Wed Nov 21 14:10:06 2012
New Revision: 1412115

URL: http://svn.apache.org/viewvc?rev=1412115&view=rev
Log:
LUCENE-4568: Fix integer overflow in PagedBytes.PagedBytesData{In,Out}put.getPosition (merged from r1412099).

Modified:
    lucene/dev/branches/branch_4x/lucene/CHANGES.txt   (contents, props changed)
    lucene/dev/branches/branch_4x/lucene/core/src/java/org/apache/lucene/util/PagedBytes.java
    lucene/dev/branches/branch_4x/lucene/core/src/test/org/apache/lucene/util/TestPagedBytes.java

Modified: lucene/dev/branches/branch_4x/lucene/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_4x/lucene/CHANGES.txt?rev=1412115&r1=1412114&r2=1412115&view=diff
==============================================================================
--- lucene/dev/branches/branch_4x/lucene/CHANGES.txt (original)
+++ lucene/dev/branches/branch_4x/lucene/CHANGES.txt Wed Nov 21 14:10:06 2012
@@ -138,6 +138,9 @@ Bug Fixes
   WFST suggesters when no suggestions were added (selckin via Mike
   McCandless)
 
+* LUCENE-4568: Fixed integer overflow in
+  PagedBytes.PagedBytesData{In,Out}put.getPosition. (Adrien Grand)
+
 Optimizations
 
 * LUCENE-2221: oal.util.BitUtil was modified to use Long.bitCount and

Modified: lucene/dev/branches/branch_4x/lucene/core/src/java/org/apache/lucene/util/PagedBytes.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_4x/lucene/core/src/java/org/apache/lucene/util/PagedBytes.java?rev=1412115&r1=1412114&r2=1412115&view=diff
==============================================================================
--- lucene/dev/branches/branch_4x/lucene/core/src/java/org/apache/lucene/util/PagedBytes.java (original)
+++ lucene/dev/branches/branch_4x/lucene/core/src/java/org/apache/lucene/util/PagedBytes.java Wed Nov 21 14:10:06 2012
@@ -256,6 +256,7 @@ public final class PagedBytes {
   /** 1<<blockBits must be bigger than biggest single
    *  BytesRef slice that will be pulled */
   public PagedBytes(int blockBits) {
+    assert blockBits > 0 && blockBits <= 31 : blockBits;
     this.blockSize = 1 << blockBits;
     this.blockBits = blockBits;
     blockMask = blockSize-1;
@@ -423,7 +424,7 @@ public final class PagedBytes {
 
     /** Returns the current byte position. */
     public long getPosition() {
-      return currentBlockIndex * blockSize + currentBlockUpto;
+      return (long) currentBlockIndex * blockSize + currentBlockUpto;
     }
   
     /** Seek to a position previously obtained from
@@ -525,11 +526,7 @@ public final class PagedBytes {
 
     /** Return the current byte position. */
     public long getPosition() {
-      if (currentBlock == null) {
-        return 0;
-      } else {
-        return blocks.size() * blockSize + upto;
-      }
+      return getPointer();
     }
   }
 

Modified: lucene/dev/branches/branch_4x/lucene/core/src/test/org/apache/lucene/util/TestPagedBytes.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_4x/lucene/core/src/test/org/apache/lucene/util/TestPagedBytes.java?rev=1412115&r1=1412114&r2=1412115&view=diff
==============================================================================
--- lucene/dev/branches/branch_4x/lucene/core/src/test/org/apache/lucene/util/TestPagedBytes.java (original)
+++ lucene/dev/branches/branch_4x/lucene/core/src/test/org/apache/lucene/util/TestPagedBytes.java Wed Nov 21 14:10:06 2012
@@ -17,10 +17,14 @@
 
 package org.apache.lucene.util;
 
+import java.io.IOException;
 import java.util.*;
 
 import org.apache.lucene.store.DataInput;
 import org.apache.lucene.store.DataOutput;
+import org.apache.lucene.util.PagedBytes.PagedBytesDataInput;
+import org.apache.lucene.util.PagedBytes.PagedBytesDataOutput;
+import org.junit.Ignore;
 
 public class TestPagedBytes extends LuceneTestCase {
 
@@ -131,4 +135,34 @@ public class TestPagedBytes extends Luce
       assertEquals(bytes2[i], answer.bytes[answer.offset + i]);
     }
   }
+
+  @Ignore // memory hole
+  public void testOverflow() throws IOException {
+    final int blockBits = _TestUtil.nextInt(random(), 14, 28);
+    final int blockSize = 1 << blockBits;
+    byte[] arr = new byte[_TestUtil.nextInt(random(), blockSize / 2, blockSize * 2)];
+    for (int i = 0; i < arr.length; ++i) {
+      arr[i] = (byte) i;
+    }
+    final long numBytes = (1L << 31) + _TestUtil.nextInt(random(), 1, blockSize * 3);
+    final PagedBytes p = new PagedBytes(blockBits);
+    final PagedBytesDataOutput out = p.getDataOutput();
+    for (long i = 0; i < numBytes; ) {
+      assertEquals(i, out.getPosition());
+      final int len = (int) Math.min(arr.length, numBytes - i);
+      out.writeBytes(arr, len);
+      i += len;
+    }
+    assertEquals(numBytes, out.getPosition());
+    p.freeze(random().nextBoolean());
+    final PagedBytesDataInput in = p.getDataInput();
+
+    for (long offset : new long[] {0L, Integer.MAX_VALUE, numBytes - 1,
+        _TestUtil.nextLong(random(), 1, numBytes - 2)}) {
+      in.setPosition(offset);
+      assertEquals(offset, in.getPosition());
+      assertEquals(arr[(int) (offset % arr.length)], in.readByte());
+      assertEquals(offset+1, in.getPosition());
+    }
+  }
 }