Skip to content

Commit

Permalink
Make the counts store check page bounds
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisvest committed Apr 7, 2016
1 parent 9c38a5a commit bd1b773
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import org.neo4j.io.pagecache.PageCursor;
import org.neo4j.io.pagecache.PagedFile;
import org.neo4j.kernel.impl.store.UnderlyingStorageException;

import static org.neo4j.io.pagecache.PagedFile.PF_SHARED_READ_LOCK;
import static org.neo4j.kernel.impl.store.kvstore.BigEndianByteArrayBuffer.buffer;
Expand Down Expand Up @@ -218,6 +219,20 @@ private static void readKeyValuePair( PageCursor cursor, int offset, WritableBuf
value.getFrom( cursor );
}
while ( cursor.shouldRetry() );
if ( cursor.checkAndClearBoundsFlag() )
{
throwOutOfBounds( cursor, offset );
}
}

private static void throwOutOfBounds( PageCursor cursor, int offset )
{
long pageId = cursor.getCurrentPageId();
int pageSize = cursor.getCurrentPageSize();
String file = cursor.getCurrentFile().getAbsolutePath();
throw new UnderlyingStorageException(
"Out of page bounds when reading key-value pair from offset " + offset + " into page " +
pageId + " (with a size of " + pageSize + " bytes) of file " + file );
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,17 @@
*/
package org.neo4j.kernel.impl.store.kvstore;

import org.junit.Test;

import java.io.IOException;
import java.util.Arrays;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

import org.junit.Test;

import org.neo4j.io.pagecache.StubPageCursor;
import org.neo4j.kernel.impl.store.UnderlyingStorageException;

import static org.junit.Assert.assertEquals;

import static org.neo4j.kernel.impl.store.kvstore.KeyValueStoreFile.maxPage;
import static org.neo4j.kernel.impl.store.kvstore.KeyValueStoreFileTest.CataloguePage.findPage;
import static org.neo4j.kernel.impl.store.kvstore.KeyValueStoreFileTest.CataloguePage.page;
Expand Down Expand Up @@ -153,6 +154,32 @@ void writeDataEntry( int record, WritableBuffer key, WritableBuffer value )
assertEquals( 4, value[0] & 0xFF );
}

@Test( expected = UnderlyingStorageException.class )
public void shouldThrowOnOutOfBoundsPageAccess() throws Exception
{
// given
AtomicBoolean goOutOfBounds = new AtomicBoolean();
byte[] key = new byte[1], value = new byte[3];
DataPage page = new DataPage( 4096, 3, 128, key, value )
{
@Override
void writeDataEntry( int record, WritableBuffer key, WritableBuffer value )
{
key.putByte( 0, (byte) 0x42 );
}

@Override
public boolean checkAndClearBoundsFlag()
{
return goOutOfBounds.get() | super.checkAndClearBoundsFlag();
}
};

page.findOffset( 0 );
goOutOfBounds.set( true );
page.findOffset( 0 );
}

@Test
public void shouldFindFirstRecordGreaterThanIfNoExactMatch() throws Exception
{
Expand All @@ -177,7 +204,7 @@ void writeDataEntry( int record, WritableBuffer key, WritableBuffer value )
}
}

static abstract class DataPage extends StubPageCursor
private static abstract class DataPage extends StubPageCursor
{
private final int headerRecords;
private final int dataRecords;
Expand Down Expand Up @@ -225,4 +252,4 @@ int findOffset( int key ) throws IOException

abstract void writeDataEntry( int record, WritableBuffer key, WritableBuffer value );
}
}
}

0 comments on commit bd1b773

Please sign in to comment.