From a9f778ddb024d6a81d6b9c5f7673aed3ad3e0bb4 Mon Sep 17 00:00:00 2001 From: ghidra1 Date: Fri, 17 Feb 2023 22:05:20 -0500 Subject: [PATCH] GP-0 DB exception improvements/cleanup --- .../memory/DBTraceMemoryBufferEntry.java | 14 ++-- .../util/database/DBBufferInputStream.java | 4 +- .../database/mem/MemoryManagerTest.java | 2 +- .../DB/src/main/java/db/BinaryField.java | 6 +- .../DB/src/main/java/db/BooleanField.java | 6 +- .../Framework/DB/src/main/java/db/Buffer.java | 57 ++++++++-------- .../DB/src/main/java/db/ByteField.java | 6 +- .../DB/src/main/java/db/ChainedBuffer.java | 66 ++++++++++--------- .../DB/src/main/java/db/DBBuffer.java | 50 +++++++++----- .../DB/src/main/java/db/DBHandle.java | 31 ++++++++- .../DB/src/main/java/db/DBParms.java | 16 ++--- .../DB/src/main/java/db/DBRecord.java | 46 ++++++------- .../Framework/DB/src/main/java/db/Field.java | 15 +++-- .../DB/src/main/java/db/FixedField10.java | 6 +- .../DB/src/main/java/db/IndexField.java | 8 +-- .../DB/src/main/java/db/IntField.java | 6 +- .../DB/src/main/java/db/LongField.java | 6 +- .../DB/src/main/java/db/MasterTable.java | 4 +- .../Framework/DB/src/main/java/db/Schema.java | 2 +- .../DB/src/main/java/db/ShortField.java | 4 +- .../DB/src/main/java/db/SparseRecord.java | 4 +- .../DB/src/main/java/db/StringField.java | 8 +-- .../src/main/java/db/buffers/DataBuffer.java | 10 +-- .../java/db/AbstractChainedBufferTest.java | 58 ++++++++-------- .../DB/src/test/java/db/DBBufferTest.java | 29 +++++++- .../database/mem/BufferSubMemoryBlock.java | 14 ++-- .../program/database/mem/FileBytes.java | 9 ++- .../program/database/mem/MemoryBlockDB.java | 20 +++--- .../database/mem/MemoryBlockInputStream.java | 2 +- .../program/database/mem/SubMemoryBlock.java | 17 +++-- .../java/ghidra/program/model/mem/Memory.java | 1 + .../ghidra/program/model/mem/MemoryBlock.java | 12 +++- 32 files changed, 324 insertions(+), 215 deletions(-) diff --git a/Ghidra/Debug/Framework-TraceModeling/src/main/java/ghidra/trace/database/memory/DBTraceMemoryBufferEntry.java b/Ghidra/Debug/Framework-TraceModeling/src/main/java/ghidra/trace/database/memory/DBTraceMemoryBufferEntry.java index 5384850c77..044fe9afb6 100644 --- a/Ghidra/Debug/Framework-TraceModeling/src/main/java/ghidra/trace/database/memory/DBTraceMemoryBufferEntry.java +++ b/Ghidra/Debug/Framework-TraceModeling/src/main/java/ghidra/trace/database/memory/DBTraceMemoryBufferEntry.java @@ -125,7 +125,8 @@ public class DBTraceMemoryBufferEntry extends DBAnnotatedObject { return true; } - public int setBytes(ByteBuffer buf, int dstOffset, int len, int blockNum) throws IOException { + public int setBytes(ByteBuffer buf, int dstOffset, int len, int blockNum) + throws IndexOutOfBoundsException, IOException { assert isSane(dstOffset, len, blockNum); if (compressed) { decompress(); @@ -136,7 +137,8 @@ public class DBTraceMemoryBufferEntry extends DBAnnotatedObject { return len; } - public int getBytes(ByteBuffer buf, int srcOffset, int len, int blockNum) throws IOException { + public int getBytes(ByteBuffer buf, int srcOffset, int len, int blockNum) + throws IndexOutOfBoundsException, IOException { assert isSane(srcOffset, len, blockNum); if (compressed) { return doGetCompressedBytes(buf, srcOffset, len, blockNum); @@ -161,7 +163,8 @@ public class DBTraceMemoryBufferEntry extends DBAnnotatedObject { } } - protected void doGetBlock(int blockNum, byte[] data) throws IOException { + protected void doGetBlock(int blockNum, byte[] data) + throws IndexOutOfBoundsException, IOException { assert isInUse(blockNum); if (compressed) { doGetCompressedBlock(blockNum, data); @@ -180,7 +183,7 @@ public class DBTraceMemoryBufferEntry extends DBAnnotatedObject { } public void copyFrom(int dstBlockNum, DBTraceMemoryBufferEntry srcBuf, int srcBlockNum) - throws IOException { + throws IndexOutOfBoundsException, IOException { assert isInUse(dstBlockNum); if (compressed) { decompress(); @@ -190,7 +193,8 @@ public class DBTraceMemoryBufferEntry extends DBAnnotatedObject { buffer.put(dstBlockNum << DBTraceMemorySpace.BLOCK_SHIFT, data); } - public int cmpBytes(ByteBuffer buf, int blkOffset, int len, int blockNum) throws IOException { + public int cmpBytes(ByteBuffer buf, int blkOffset, int len, int blockNum) + throws IndexOutOfBoundsException, IOException { assert isSane(blkOffset, len, blockNum); if (compressed) { return doCmpCompressedBytes(buf, blkOffset, len, blockNum); diff --git a/Ghidra/Debug/ProposedUtils/src/main/java/ghidra/util/database/DBBufferInputStream.java b/Ghidra/Debug/ProposedUtils/src/main/java/ghidra/util/database/DBBufferInputStream.java index 458181a6f1..fc4e830119 100644 --- a/Ghidra/Debug/ProposedUtils/src/main/java/ghidra/util/database/DBBufferInputStream.java +++ b/Ghidra/Debug/ProposedUtils/src/main/java/ghidra/util/database/DBBufferInputStream.java @@ -50,7 +50,7 @@ public class DBBufferInputStream extends InputStream { } @Override - public int read(byte[] b) throws IOException { + public int read(byte[] b) throws IndexOutOfBoundsException, IOException { if (offset == buffer.length()) { return -1; } @@ -61,7 +61,7 @@ public class DBBufferInputStream extends InputStream { } @Override - public int read(byte[] b, int off, int len) throws IOException { + public int read(byte[] b, int off, int len) throws IndexOutOfBoundsException, IOException { if (offset == buffer.length()) { return -1; } diff --git a/Ghidra/Features/Base/src/test.slow/java/ghidra/program/database/mem/MemoryManagerTest.java b/Ghidra/Features/Base/src/test.slow/java/ghidra/program/database/mem/MemoryManagerTest.java index 9b956086ef..c9636c6499 100644 --- a/Ghidra/Features/Base/src/test.slow/java/ghidra/program/database/mem/MemoryManagerTest.java +++ b/Ghidra/Features/Base/src/test.slow/java/ghidra/program/database/mem/MemoryManagerTest.java @@ -1091,7 +1091,7 @@ public class MemoryManagerTest extends AbstractGhidraHeadedIntegrationTest { mem.getBytes(addr(0), b, 9, 50); fail("Expected exception"); } - catch (ArrayIndexOutOfBoundsException e) { + catch (IndexOutOfBoundsException e) { // expected } diff --git a/Ghidra/Framework/DB/src/main/java/db/BinaryField.java b/Ghidra/Framework/DB/src/main/java/db/BinaryField.java index 9955ae8541..dcf5eeca30 100644 --- a/Ghidra/Framework/DB/src/main/java/db/BinaryField.java +++ b/Ghidra/Framework/DB/src/main/java/db/BinaryField.java @@ -92,7 +92,7 @@ public class BinaryField extends Field { } @Override - int write(Buffer buf, int offset) throws IOException { + int write(Buffer buf, int offset) throws IndexOutOfBoundsException, IOException { if (data == null) { return buf.putInt(offset, -1); } @@ -101,7 +101,7 @@ public class BinaryField extends Field { } @Override - int read(Buffer buf, int offset) throws IOException { + int read(Buffer buf, int offset) throws IndexOutOfBoundsException, IOException { checkImmutable(); int len = buf.getInt(offset); offset += 4; @@ -116,7 +116,7 @@ public class BinaryField extends Field { } @Override - int readLength(Buffer buf, int offset) throws IOException { + int readLength(Buffer buf, int offset) throws IndexOutOfBoundsException, IOException { int len = buf.getInt(offset); return (len < 0 ? 0 : len) + 4; } diff --git a/Ghidra/Framework/DB/src/main/java/db/BooleanField.java b/Ghidra/Framework/DB/src/main/java/db/BooleanField.java index 350e636d52..87b25e2cd5 100644 --- a/Ghidra/Framework/DB/src/main/java/db/BooleanField.java +++ b/Ghidra/Framework/DB/src/main/java/db/BooleanField.java @@ -89,19 +89,19 @@ public final class BooleanField extends PrimitiveField { } @Override - int write(Buffer buf, int offset) throws IOException { + int write(Buffer buf, int offset) throws IndexOutOfBoundsException, IOException { return buf.putByte(offset, value); } @Override - int read(Buffer buf, int offset) throws IOException { + int read(Buffer buf, int offset) throws IndexOutOfBoundsException, IOException { updatingPrimitiveValue(); value = buf.getByte(offset); return offset + 1; } @Override - int readLength(Buffer buf, int offset) throws IOException { + int readLength(Buffer buf, int offset) { return 1; } diff --git a/Ghidra/Framework/DB/src/main/java/db/Buffer.java b/Ghidra/Framework/DB/src/main/java/db/Buffer.java index 7a5107badd..2fe2bdd424 100644 --- a/Ghidra/Framework/DB/src/main/java/db/Buffer.java +++ b/Ghidra/Framework/DB/src/main/java/db/Buffer.java @@ -41,12 +41,11 @@ public interface Buffer { * bytes array provided. * @param offset byte offset from start of buffer. * @param bytes byte array to store data - * @throws ArrayIndexOutOfBoundsException is thrown if an invalid offset is - * specified. + * @throws IndexOutOfBoundsException is thrown if an invalid offset is specified. * @throws IOException is thrown if an error occurs while accessing the * underlying storage. */ - public void get(int offset, byte[] bytes) throws IOException; + public void get(int offset, byte[] bytes) throws IndexOutOfBoundsException, IOException; /** * Get the byte data located at the specified offset and store into the data @@ -55,72 +54,71 @@ public interface Buffer { * @param data byte array to store the data. * @param dataOffset offset into the data buffer * @param length amount of data to read - * @throws ArrayIndexOutOfBoundsException if an invalid offset, dataOffset, - * or length is specified. + * @throws IndexOutOfBoundsException if an invalid offset, dataOffset, or length is specified. * @throws IOException is thrown if an error occurs while accessing the * underlying storage. */ - public void get(int offset, byte[] data, int dataOffset, int length) throws IOException; + public void get(int offset, byte[] data, int dataOffset, int length) + throws IndexOutOfBoundsException, IOException; /** * Get the byte data located at the specified offset. * @param offset byte offset from start of buffer. * @param length number of bytes to be read and returned * @return the byte array. - * @throws ArrayIndexOutOfBoundsException is thrown if an invalid offset is + * @throws IndexOutOfBoundsException is thrown if an invalid offset is * specified or the end of the buffer was encountered while reading the * data. * @throws IOException is thrown if an error occurs while accessing the * underlying storage. */ - public byte[] get(int offset, int length) throws IOException; + public byte[] get(int offset, int length) throws IndexOutOfBoundsException, IOException; /** * Get the 8-bit byte value located at the specified offset. * @param offset byte offset from start of buffer. * @return the byte value at the specified offset. - * @throws ArrayIndexOutOfBoundsException is thrown if an invalid offset is - * specified. + * @throws IndexOutOfBoundsException is thrown if an invalid offset is specified. * @throws IOException is thrown if an error occurs while accessing the * underlying storage. */ - public byte getByte(int offset) throws IOException; + public byte getByte(int offset) throws IndexOutOfBoundsException, IOException; /** * Get the 32-bit integer value located at the specified offset. * @param offset byte offset from start of buffer. * @return the integer value at the specified offset. - * @throws ArrayIndexOutOfBoundsException is thrown if an invalid offset is + * @throws IndexOutOfBoundsException is thrown if an invalid offset is * specified or the end of the buffer was encountered while reading the * value. * @throws IOException is thrown if an error occurs while accessing the * underlying storage. */ - public int getInt(int offset) throws IOException; + public int getInt(int offset) throws IndexOutOfBoundsException, IOException; /** * Get the 16-bit short value located at the specified offset. * @param offset byte offset from start of buffer. * @return the short value at the specified offset. - * @throws ArrayIndexOutOfBoundsException is thrown if an invalid offset is + * @throws IndexOutOfBoundsException is thrown if an invalid offset is * specified or the end of the buffer was encountered while reading the * value. * @throws IOException is thrown if an error occurs while accessing the * underlying storage. */ - public short getShort(int offset) throws IOException; + public short getShort(int offset) throws IndexOutOfBoundsException, IOException; /** * Get the 64-bit long value located at the specified offset. * @param offset byte offset from start of buffer. * @return the long value at the specified offset. - * @throws ArrayIndexOutOfBoundsException is thrown if an invalid offset is + * @throws IndexOutOfBoundsException is thrown if an invalid offset is * specified or the end of the buffer was encountered while reading the * value. * @throws IOException is thrown if an error occurs while accessing the * underlying storage. */ - public long getLong(int offset) throws IOException; + public long getLong(int offset) throws IndexOutOfBoundsException, IOException; /** * Put a specified number of bytes from the array provided into the buffer @@ -132,12 +130,13 @@ public interface Buffer { * @param length the number of bytes to be stored. * @return the next available offset into the buffer, or -1 if the buffer is * full. - * @throws ArrayIndexOutOfBoundsException if an invalid offset is provided + * @throws IndexOutOfBoundsException if an invalid offset is provided * or the end of buffer was encountered while storing the data. * @throws IOException is thrown if an error occurs while accessing the * underlying storage. */ - public int put(int offset, byte[] data, int dataOffset, int length) throws IOException; + public int put(int offset, byte[] data, int dataOffset, int length) + throws IndexOutOfBoundsException, IOException; /** * Put the bytes provided into the buffer at the specified offset. The @@ -147,12 +146,12 @@ public interface Buffer { * @param bytes the byte data to be stored. * @return the next available offset into the buffer, or -1 if the buffer is * full. - * @throws ArrayIndexOutOfBoundsException if an invalid offset is provided + * @throws IndexOutOfBoundsException if an invalid offset is provided * or the end of buffer was encountered while storing the data. * @throws IOException is thrown if an error occurs while accessing the * underlying storage. */ - public int put(int offset, byte[] bytes) throws IOException; + public int put(int offset, byte[] bytes) throws IndexOutOfBoundsException, IOException; /** * Put the 8-bit byte value into the buffer at the specified offset. @@ -160,11 +159,11 @@ public interface Buffer { * @param b the byte value to be stored. * @return the next available offset into the buffer, or -1 if the buffer is * full. - * @throws ArrayIndexOutOfBoundsException if an invalid offset is provided. + * @throws IndexOutOfBoundsException if an invalid offset is provided. * @throws IOException is thrown if an error occurs while accessing the * underlying storage. */ - public int putByte(int offset, byte b) throws IOException; + public int putByte(int offset, byte b) throws IndexOutOfBoundsException, IOException; /** * Put the 32-bit integer value into the buffer at the specified offset. @@ -172,12 +171,12 @@ public interface Buffer { * @param v the integer value to be stored. * @return the next available offset into the buffer, or -1 if the buffer is * full. - * @throws ArrayIndexOutOfBoundsException if an invalid offset is provided + * @throws IndexOutOfBoundsException if an invalid offset is provided * or the end of buffer was encountered while storing the data. * @throws IOException is thrown if an error occurs while accessing the * underlying storage. */ - public int putInt(int offset, int v) throws IOException; + public int putInt(int offset, int v) throws IndexOutOfBoundsException, IOException; /** * Put the 16-bit short value into the buffer at the specified offset. @@ -185,12 +184,12 @@ public interface Buffer { * @param v the short value to be stored. * @return the next available offset into the buffer, or -1 if the buffer is * full. - * @throws ArrayIndexOutOfBoundsException if an invalid offset is provided + * @throws IndexOutOfBoundsException if an invalid offset is provided * or the end of buffer was encountered while storing the data. * @throws IOException is thrown if an error occurs while accessing the * underlying storage. */ - public int putShort(int offset, short v) throws IOException; + public int putShort(int offset, short v) throws IndexOutOfBoundsException, IOException; /** * Put the 64-bit long value into the buffer at the specified offset. @@ -198,11 +197,11 @@ public interface Buffer { * @param v the long value to be stored. * @return the next available offset into the buffer, or -1 if the buffer is * full. - * @throws ArrayIndexOutOfBoundsException if an invalid offset is provided + * @throws IndexOutOfBoundsException if an invalid offset is provided * or the end of buffer was encountered while storing the data. * @throws IOException is thrown if an error occurs while accessing the * underlying storage. */ - public int putLong(int offset, long v) throws IOException; + public int putLong(int offset, long v) throws IndexOutOfBoundsException, IOException; } diff --git a/Ghidra/Framework/DB/src/main/java/db/ByteField.java b/Ghidra/Framework/DB/src/main/java/db/ByteField.java index ff1358f4f0..0515f53ad7 100644 --- a/Ghidra/Framework/DB/src/main/java/db/ByteField.java +++ b/Ghidra/Framework/DB/src/main/java/db/ByteField.java @@ -94,19 +94,19 @@ public final class ByteField extends PrimitiveField { } @Override - int write(Buffer buf, int offset) throws IOException { + int write(Buffer buf, int offset) throws IndexOutOfBoundsException, IOException { return buf.putByte(offset, value); } @Override - int read(Buffer buf, int offset) throws IOException { + int read(Buffer buf, int offset) throws IndexOutOfBoundsException, IOException { updatingPrimitiveValue(); value = buf.getByte(offset); return offset + 1; } @Override - int readLength(Buffer buf, int offset) throws IOException { + int readLength(Buffer buf, int offset) { return 1; } diff --git a/Ghidra/Framework/DB/src/main/java/db/ChainedBuffer.java b/Ghidra/Framework/DB/src/main/java/db/ChainedBuffer.java index dbc6548b3a..7049116041 100644 --- a/Ghidra/Framework/DB/src/main/java/db/ChainedBuffer.java +++ b/Ghidra/Framework/DB/src/main/java/db/ChainedBuffer.java @@ -338,7 +338,6 @@ public class ChainedBuffer implements Buffer { * @throws UnsupportedOperationException thrown if this ChainedBuffer utilizes an * Uninitialized Data Source or is read-only * @throws IOException thrown if an IO error occurs. - * @throws UnsupportedOperationException if read-only or uninitialized data source is used */ public synchronized void setSize(int size, boolean preserveData) throws IOException { if (readOnly) { @@ -595,10 +594,10 @@ public class ChainedBuffer implements Buffer { * byte within the new buffer. * @return the new DBBuffer object. * @throws UnsupportedOperationException thrown if this ChainedBuffer is read-only - * @throws ArrayIndexOutOfBoundsException if offset is invalid. + * @throws IndexOutOfBoundsException if offset is invalid. * @throws IOException thrown if an IO error occurs */ - public synchronized ChainedBuffer split(int offset) throws IOException { + public synchronized ChainedBuffer split(int offset) throws IndexOutOfBoundsException, IOException { if (readOnly) { throw new UnsupportedOperationException("Read-only buffer"); } @@ -606,7 +605,7 @@ public class ChainedBuffer implements Buffer { throw new AssertException("Invalid Buffer"); } if (offset < 0 || offset >= size) { - throw new ArrayIndexOutOfBoundsException(); + throw new IndexOutOfBoundsException(); } // Create new DBBuffer @@ -899,10 +898,11 @@ public class ChainedBuffer implements Buffer { * @return int actual number of byte read. * This could be smaller than length if the end of buffer is * encountered while reading data. + * @throws IndexOutOfBoundsException if an invalid offset, dataOffset, or length is specified. * @throws IOException thrown if IO error occurs */ private int getBytes(int offset, int index, int bufferDataOffset, byte[] data, int dataOffset, - int length) throws IOException { + int length) throws IndexOutOfBoundsException, IOException { int availableData = dataSpace - bufferDataOffset; int len = availableData < length ? availableData : length; int id = dataBufferIdTable[index]; @@ -934,15 +934,15 @@ public class ChainedBuffer implements Buffer { */ @Override public synchronized void get(int offset, byte[] data, int dataOffset, int length) - throws IOException { + throws IndexOutOfBoundsException, IOException { if (dataBufferIdTable == null) { throw new AssertException("Invalid Buffer"); } if (offset < 0 || (offset + length - 1) >= size) { - throw new ArrayIndexOutOfBoundsException(); + throw new IndexOutOfBoundsException(); } if (data.length < dataOffset + length) { - throw new ArrayIndexOutOfBoundsException(); + throw new IndexOutOfBoundsException(); } int index = offset / dataSpace; int bufferDataOffset = offset % dataSpace; @@ -960,7 +960,7 @@ public class ChainedBuffer implements Buffer { * @see ghidra.framework.store.Buffer#get(int, byte[]) */ @Override - public synchronized void get(int offset, byte[] data) throws IOException { + public synchronized void get(int offset, byte[] data) throws IndexOutOfBoundsException, IOException { get(offset, data, 0, data.length); } @@ -968,7 +968,7 @@ public class ChainedBuffer implements Buffer { * @see ghidra.framework.store.Buffer#get(int, int) */ @Override - public synchronized byte[] get(int offset, int length) throws IOException { + public synchronized byte[] get(int offset, int length) throws IndexOutOfBoundsException, IOException { byte[] data = new byte[length]; get(offset, data, 0, length); return data; @@ -978,12 +978,12 @@ public class ChainedBuffer implements Buffer { * @see ghidra.framework.store.Buffer#getByte(int) */ @Override - public synchronized byte getByte(int offset) throws IOException { + public synchronized byte getByte(int offset) throws IndexOutOfBoundsException, IOException { if (dataBufferIdTable == null) { throw new AssertException("Invalid Buffer"); } if (offset < 0 || offset >= size) { - throw new ArrayIndexOutOfBoundsException(); + throw new IndexOutOfBoundsException(); } int index = offset / dataSpace; int bufferDataOffset = offset % dataSpace; @@ -1007,14 +1007,14 @@ public class ChainedBuffer implements Buffer { * @see ghidra.framework.store.Buffer#getInt(int) */ @Override - public synchronized int getInt(int offset) throws IOException { + public synchronized int getInt(int offset) throws IndexOutOfBoundsException, IOException { int bufferOffset = dataBaseOffset + (offset % dataSpace); if (bufferOffset + 3 <= dataSpace) { if (dataBufferIdTable == null) { throw new AssertException("Invalid Buffer"); } if (offset < 0 || (offset + 3) >= size) { - throw new ArrayIndexOutOfBoundsException(); + throw new IndexOutOfBoundsException(); } int index = offset / dataSpace; int id = dataBufferIdTable[index]; @@ -1041,14 +1041,14 @@ public class ChainedBuffer implements Buffer { * @see ghidra.framework.store.Buffer#getLong(int) */ @Override - public synchronized long getLong(int offset) throws IOException { + public synchronized long getLong(int offset) throws IndexOutOfBoundsException, IOException { int bufferOffset = dataBaseOffset + (offset % dataSpace); if (bufferOffset + 7 <= dataSpace) { if (dataBufferIdTable == null) { throw new AssertException("Invalid Buffer"); } if (offset < 0 || (offset + 7) >= size) { - throw new ArrayIndexOutOfBoundsException(); + throw new IndexOutOfBoundsException(); } int index = offset / dataSpace; int id = dataBufferIdTable[index]; @@ -1077,14 +1077,14 @@ public class ChainedBuffer implements Buffer { * @see ghidra.framework.store.Buffer#getShort(int) */ @Override - public synchronized short getShort(int offset) throws IOException { + public synchronized short getShort(int offset) throws IndexOutOfBoundsException, IOException { int bufferOffset = dataBaseOffset + (offset % dataSpace); if (bufferOffset + 1 <= dataSpace) { if (dataBufferIdTable == null) { throw new AssertException("Invalid Buffer"); } if (offset < 0 || (offset + 1) >= size) { - throw new ArrayIndexOutOfBoundsException(); + throw new IndexOutOfBoundsException(); } int index = offset / dataSpace; int id = dataBufferIdTable[index]; @@ -1119,10 +1119,12 @@ public class ChainedBuffer implements Buffer { * @param startOffset starting offset, inclusive * @param endOffset ending offset, inclusive * @param fillByte byte value + * @throws IndexOutOfBoundsException if an invalid offsets are provided + * or the end of buffer was encountered while storing the data. * @throws IOException thrown if an IO error occurs */ public synchronized void fill(int startOffset, int endOffset, byte fillByte) - throws IOException { + throws IndexOutOfBoundsException, IOException { if (readOnly) { throw new UnsupportedOperationException("Read-only buffer"); } @@ -1130,7 +1132,7 @@ public class ChainedBuffer implements Buffer { throw new IllegalArgumentException(); } if (startOffset < 0 || endOffset >= size) { - throw new ArrayIndexOutOfBoundsException(); + throw new IndexOutOfBoundsException(); } byte[] fillData = new byte[dataSpace]; Arrays.fill(fillData, fillByte); @@ -1239,7 +1241,7 @@ public class ChainedBuffer implements Buffer { */ @Override public synchronized int put(int offset, byte[] data, int dataOffset, int length) - throws IOException { + throws IndexOutOfBoundsException, IOException { if (readOnly) { throw new UnsupportedOperationException("Read-only buffer"); @@ -1248,7 +1250,7 @@ public class ChainedBuffer implements Buffer { throw new AssertException("Invalid Buffer"); } if (offset < 0 || (offset + length - 1) >= size) { - throw new ArrayIndexOutOfBoundsException(); + throw new IndexOutOfBoundsException(); } int index = offset / dataSpace; int bufferDataOffset = offset % dataSpace; @@ -1275,7 +1277,7 @@ public class ChainedBuffer implements Buffer { * @see ghidra.framework.store.Buffer#put(int, byte[]) */ @Override - public synchronized int put(int offset, byte[] bytes) throws IOException { + public synchronized int put(int offset, byte[] bytes) throws IndexOutOfBoundsException, IOException { return put(offset, bytes, 0, bytes.length); } @@ -1283,7 +1285,7 @@ public class ChainedBuffer implements Buffer { * @see ghidra.framework.store.Buffer#putByte(int, byte) */ @Override - public synchronized int putByte(int offset, byte b) throws IOException { + public synchronized int putByte(int offset, byte b) throws IndexOutOfBoundsException, IOException { if (readOnly) { throw new UnsupportedOperationException("Read-only buffer"); } @@ -1291,7 +1293,7 @@ public class ChainedBuffer implements Buffer { throw new AssertException("Invalid Buffer"); } if (offset < 0 || offset >= size) { - throw new ArrayIndexOutOfBoundsException(); + throw new IndexOutOfBoundsException(); } DataBuffer buffer = getBuffer(offset / dataSpace); int bufferDataOffset = offset % dataSpace; @@ -1307,7 +1309,7 @@ public class ChainedBuffer implements Buffer { * @see ghidra.framework.store.Buffer#putInt(int, int) */ @Override - public synchronized int putInt(int offset, int v) throws IOException { + public synchronized int putInt(int offset, int v) throws IndexOutOfBoundsException, IOException { if (readOnly) { throw new UnsupportedOperationException("Read-only buffer"); } @@ -1317,7 +1319,7 @@ public class ChainedBuffer implements Buffer { throw new AssertException("Invalid Buffer"); } if (offset < 0 || (offset + 3) >= size) { - throw new ArrayIndexOutOfBoundsException(); + throw new IndexOutOfBoundsException(); } if (useXORMask) { v = v ^ (int) getXorMask(offset % dataSpace, 4); @@ -1341,7 +1343,7 @@ public class ChainedBuffer implements Buffer { * @see ghidra.framework.store.Buffer#putLong(int, long) */ @Override - public synchronized int putLong(int offset, long v) throws IOException { + public synchronized int putLong(int offset, long v) throws IndexOutOfBoundsException, IOException { if (readOnly) { throw new UnsupportedOperationException("Read-only buffer"); } @@ -1351,7 +1353,7 @@ public class ChainedBuffer implements Buffer { throw new AssertException("Invalid Buffer"); } if (offset < 0 || (offset + 7) >= size) { - throw new ArrayIndexOutOfBoundsException(); + throw new IndexOutOfBoundsException(); } if (useXORMask) { v = v ^ getXorMask(offset % dataSpace, 8); @@ -1376,7 +1378,7 @@ public class ChainedBuffer implements Buffer { } @Override - public synchronized int putShort(int offset, short v) throws IOException { + public synchronized int putShort(int offset, short v) throws IndexOutOfBoundsException, IOException { if (readOnly) { throw new UnsupportedOperationException("Read-only buffer"); } @@ -1386,7 +1388,7 @@ public class ChainedBuffer implements Buffer { throw new AssertException("Invalid Buffer"); } if (offset < 0 || (offset + 1) >= size) { - throw new ArrayIndexOutOfBoundsException(); + throw new IndexOutOfBoundsException(); } if (useXORMask) { v = (short) (v ^ (short) getXorMask(offset % dataSpace, 2)); @@ -1430,7 +1432,7 @@ public class ChainedBuffer implements Buffer { * @throws IOException thrown if an IO error occurs */ private void initializeAllocatedBuffer(int chainBufferIndex, DataBuffer buf) - throws IOException { + throws IndexOutOfBoundsException, IOException { int offset = chainBufferIndex * dataSpace; int len = size - offset; diff --git a/Ghidra/Framework/DB/src/main/java/db/DBBuffer.java b/Ghidra/Framework/DB/src/main/java/db/DBBuffer.java index f6094a2c74..c16ef39bab 100644 --- a/Ghidra/Framework/DB/src/main/java/db/DBBuffer.java +++ b/Ghidra/Framework/DB/src/main/java/db/DBBuffer.java @@ -43,10 +43,10 @@ public class DBBuffer { * @param offset the split point. The byte at this offset becomes the first * byte within the new buffer. * @return the new DBBuffer object. - * @throws ArrayIndexOutOfBoundsException if offset is invalid. + * @throws IndexOutOfBoundsException if offset is invalid. * @throws IOException thrown if an IO error occurs */ - public DBBuffer split(int offset) throws IOException { + public DBBuffer split(int offset) throws IndexOutOfBoundsException, IOException { synchronized (dbh) { dbh.checkTransaction(); return new DBBuffer(dbh, buf.split(offset)); @@ -58,6 +58,8 @@ public class DBBuffer { * @param size new size * @param preserveData if true, existing data is preserved at the original offsets. If false, * no additional effort will be expended to preserve data. + * @throws UnsupportedOperationException thrown if this ChainedBuffer utilizes an + * Uninitialized Data Source or is read-only * @throws IOException thrown if an IO error occurs. */ public void setSize(int size, boolean preserveData) throws IOException { @@ -69,7 +71,7 @@ public class DBBuffer { /** * Returns the length; - * @return + * @return this buffers length */ public int length() { synchronized (dbh) { @@ -94,8 +96,12 @@ public class DBBuffer { * @param startOffset starting offset, inclusive * @param endOffset ending offset, exclusive * @param fillByte byte value + * @throws IndexOutOfBoundsException if an invalid offsets are provided + * or the end of buffer was encountered while storing the data. + * @throws IOException thrown if an IO error occurs */ - public void fill(int startOffset, int endOffset, byte fillByte) throws IOException { + public void fill(int startOffset, int endOffset, byte fillByte) + throws IndexOutOfBoundsException, IOException { synchronized (dbh) { dbh.checkTransaction(); buf.fill(startOffset, endOffset, fillByte); @@ -107,6 +113,8 @@ public class DBBuffer { * The size of this buffer increases by the size of dbBuf. When the operation * is complete, dbBuf object is no longer valid and must not be used. * @param buffer the buffer to be appended to this buffer. + * @throws UnsupportedOperationException if read-only, uninitialized data source is used, + * or both buffers do not have the same obfuscation enablement * @throws IOException thrown if an IO error occurs */ public void append(DBBuffer buffer) throws IOException { @@ -120,13 +128,14 @@ public class DBBuffer { * Get the 8-bit byte value located at the specified offset. * @param offset byte offset from start of buffer. * @return the byte value at the specified offset. - * @throws ArrayIndexOutOfBoundsException is thrown if an invalid offset is + * @throws IndexOutOfBoundsException is thrown if an invalid offset is * specified. * @throws IOException is thrown if an error occurs while accessing the * underlying storage. */ - public byte getByte(int offset) throws IOException { + public byte getByte(int offset) throws IndexOutOfBoundsException, IOException { synchronized (dbh) { + dbh.checkIsClosed(); return buf.getByte(offset); } } @@ -138,13 +147,15 @@ public class DBBuffer { * @param data byte array to store the data. * @param dataOffset offset into the data buffer * @param length amount of data to read - * @throws ArrayIndexOutOfBoundsException if an invalid offset, dataOffset, + * @throws IndexOutOfBoundsException if an invalid offset, dataOffset, * or length is specified. * @throws IOException is thrown if an error occurs while accessing the * underlying storage. */ - public void get(int offset, byte[] data, int dataOffset, int length) throws IOException { + public void get(int offset, byte[] data, int dataOffset, int length) + throws IndexOutOfBoundsException, IOException { synchronized (dbh) { + dbh.checkIsClosed(); buf.get(offset, data, dataOffset, length); } } @@ -171,12 +182,13 @@ public class DBBuffer { * @param bytes the byte data to be stored. * @param dataOffset the starting offset into the data. * @param length the number of bytes to be stored. - * @throws ArrayIndexOutOfBoundsException if an invalid offset is provided + * @throws IndexOutOfBoundsException if an invalid offset is provided * or the end of buffer was encountered while storing the data. * @throws IOException is thrown if an error occurs while accessing the * underlying storage. */ - public void put(int offset, byte[] bytes, int dataOffset, int length) throws IOException { + public void put(int offset, byte[] bytes, int dataOffset, int length) + throws IndexOutOfBoundsException, IOException { synchronized (dbh) { dbh.checkTransaction(); buf.put(offset, bytes, dataOffset, length); @@ -190,12 +202,12 @@ public class DBBuffer { * array. * @param offset byte offset from start of buffer. * @param bytes the byte data to be stored. - * @throws ArrayIndexOutOfBoundsException if an invalid offset is provided + * @throws IndexOutOfBoundsException if an invalid offset is provided * or the end of buffer was encountered while storing the data. * @throws IOException is thrown if an error occurs while accessing the * underlying storage. */ - public void put(int offset, byte[] bytes) throws IOException { + public void put(int offset, byte[] bytes) throws IndexOutOfBoundsException, IOException { synchronized (dbh) { dbh.checkTransaction(); buf.put(offset, bytes); @@ -206,11 +218,11 @@ public class DBBuffer { * Put the 8-bit byte value into the buffer at the specified offset. * @param offset byte offset from start of buffer. * @param b the byte value to be stored. - * @throws ArrayIndexOutOfBoundsException if an invalid offset is provided. + * @throws IndexOutOfBoundsException if an invalid offset is provided. * @throws IOException is thrown if an error occurs while accessing the * underlying storage. */ - public void putByte(int offset, byte b) throws IOException { + public void putByte(int offset, byte b) throws IndexOutOfBoundsException, IOException { synchronized (dbh) { dbh.checkTransaction(); buf.putByte(offset, b); @@ -220,21 +232,25 @@ public class DBBuffer { /** * Get the byte data located at the specified offset. * @param offset byte offset from start of buffer. - * @throws ArrayIndexOutOfBoundsException is thrown if an invalid offset is + * @param data data buffer to be filled + * @throws IndexOutOfBoundsException is thrown if an invalid offset is * specified or the end of the buffer was encountered while reading the * data. * @throws IOException is thrown if an error occurs while accessing the * underlying storage. */ - public void get(int offset, byte[] data) throws IOException { + public void get(int offset, byte[] data) throws IndexOutOfBoundsException, IOException { synchronized (dbh) { + dbh.checkIsClosed(); buf.get(offset, data); } } /** - * Delete and release all underlying DataBuffers. + * Delete and release all underlying DataBuffers. + * @throws IOException is thrown if an error occurs while accessing the + * underlying storage. */ public void delete() throws IOException { synchronized (dbh) { diff --git a/Ghidra/Framework/DB/src/main/java/db/DBHandle.java b/Ghidra/Framework/DB/src/main/java/db/DBHandle.java index 3deddfabd4..76cec15f1a 100644 --- a/Ghidra/Framework/DB/src/main/java/db/DBHandle.java +++ b/Ghidra/Framework/DB/src/main/java/db/DBHandle.java @@ -242,7 +242,7 @@ public class DBHandle { databaseId = ((long) dbParms.get(DBParms.DATABASE_ID_HIGH_PARM) << 32) + (dbParms.get(DBParms.DATABASE_ID_LOW_PARM) & 0x0ffffffffL); } - catch (ArrayIndexOutOfBoundsException e) { + catch (IndexOutOfBoundsException e) { // DBParams is still at version 1 } } @@ -406,6 +406,16 @@ public class DBHandle { } } + /** + * Check if the database is closed. + * @throws ClosedException if database is closed and further operations are unsupported + */ + public void checkIsClosed() throws ClosedException { + if (isClosed()) { + throw new ClosedException(); + } + } + /** * @return true if transaction is currently active */ @@ -418,6 +428,9 @@ public class DBHandle { * @return transaction ID */ public synchronized long startTransaction() { + if (isClosed()) { + throw new IllegalStateException("Database is closed"); + } if (txStarted) { throw new IllegalStateException("Transaction already started"); } @@ -802,6 +815,8 @@ public class DBHandle { public synchronized void saveAs(File file, boolean associateWithNewFile, TaskMonitor monitor) throws IOException, CancelledException { + checkIsClosed(); + if (file.exists()) { throw new DuplicateFileException("File already exists: " + file); } @@ -861,6 +876,7 @@ public class DBHandle { * @throws IOException if an I/O error occurs while getting the buffer. */ public DBBuffer getBuffer(int id) throws IOException { + checkIsClosed(); return new DBBuffer(this, new ChainedBuffer(bufferMgr, id)); } @@ -876,6 +892,7 @@ public class DBHandle { * @throws IOException if an I/O error occurs while getting the buffer. */ public DBBuffer getBuffer(int id, DBBuffer shadowBuffer) throws IOException { + checkIsClosed(); return new DBBuffer(this, new ChainedBuffer(bufferMgr, id, shadowBuffer.buf, 0)); } @@ -1071,6 +1088,9 @@ public class DBHandle { * @return number of buffer cache hits */ public long getCacheHits() { + if (bufferMgr == null) { + throw new IllegalStateException("Database is closed"); + } return bufferMgr.getCacheHits(); } @@ -1078,6 +1098,9 @@ public class DBHandle { * @return number of buffer cache misses */ public long getCacheMisses() { + if (bufferMgr == null) { + throw new IllegalStateException("Database is closed"); + } return bufferMgr.getCacheMisses(); } @@ -1085,6 +1108,9 @@ public class DBHandle { * @return low water mark (minimum buffer pool size) */ public int getLowBufferCount() { + if (bufferMgr == null) { + throw new IllegalStateException("Database is closed"); + } return bufferMgr.getLowBufferCount(); } @@ -1102,6 +1128,9 @@ public class DBHandle { * @return buffer size utilized by this database */ public int getBufferSize() { + if (bufferMgr == null) { + throw new IllegalStateException("Database is closed"); + } return bufferMgr.getBufferSize(); } diff --git a/Ghidra/Framework/DB/src/main/java/db/DBParms.java b/Ghidra/Framework/DB/src/main/java/db/DBParms.java index 6fe402be58..84d8a127c9 100644 --- a/Ghidra/Framework/DB/src/main/java/db/DBParms.java +++ b/Ghidra/Framework/DB/src/main/java/db/DBParms.java @@ -1,6 +1,5 @@ /* ### * IP: GHIDRA - * REVIEWED: YES * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,14 +15,13 @@ */ package db; -import ghidra.util.datastruct.IntIntHashtable; -import ghidra.util.exception.AssertException; -import ghidra.util.exception.NoValueException; - import java.io.File; import java.io.IOException; import db.buffers.*; +import ghidra.util.datastruct.IntIntHashtable; +import ghidra.util.exception.AssertException; +import ghidra.util.exception.NoValueException; /** * DBParms manages 4-byte integer parameters associated with a database @@ -123,7 +121,7 @@ class DBParms { private static void storeParm(int parm, int value, DataBuffer buffer) { int maxParmCnt = (buffer.length() - PARM_BASE_OFFSET) / 4; if (parm < 0 || parm >= maxParmCnt) { - throw new ArrayIndexOutOfBoundsException("Invalid parameter index: " + parm); + throw new IndexOutOfBoundsException("Invalid parameter index: " + parm); } int size = (buffer.getInt(DATA_LENGTH_OFFSET) - VERSION_SIZE) / 4; if (parm >= size) { @@ -164,15 +162,15 @@ class DBParms { * @param parm parameter number * @return parameter value * @throws IOException thrown if an IO error occurs - * @throws ArrayIndexOutOfBoundsException if index outside of allocated + * @throws IndexOutOfBoundsException if index outside of allocated * parameter space. */ - int get(int parm) throws IOException, ArrayIndexOutOfBoundsException { + int get(int parm) throws IOException, IndexOutOfBoundsException { try { return cache.get(parm); } catch (NoValueException e) { - throw new ArrayIndexOutOfBoundsException(); + throw new IndexOutOfBoundsException(); } } diff --git a/Ghidra/Framework/DB/src/main/java/db/DBRecord.java b/Ghidra/Framework/DB/src/main/java/db/DBRecord.java index 4cea8c79a8..bc20032194 100644 --- a/Ghidra/Framework/DB/src/main/java/db/DBRecord.java +++ b/Ghidra/Framework/DB/src/main/java/db/DBRecord.java @@ -162,7 +162,7 @@ public class DBRecord implements Comparable { * Get a copy of the specified field value. * @param columnIndex field index * @return Field field value - * @throws ArrayIndexOutOfBoundsException if invalid columnIndex is specified + * @throws IndexOutOfBoundsException if invalid columnIndex is specified */ public Field getFieldValue(int columnIndex) { Field f = fieldValues[columnIndex]; @@ -173,7 +173,7 @@ public class DBRecord implements Comparable { * Set the field value for the specified field. * @param colIndex field index * @param value field value (null permitted for sparse column only) - * @throws ArrayIndexOutOfBoundsException if invalid columnIndex is specified + * @throws IndexOutOfBoundsException if invalid columnIndex is specified * @throws IllegalArgumentException if value type does not match column field type. */ public void setField(int colIndex, Field value) { @@ -189,7 +189,7 @@ public class DBRecord implements Comparable { * modified. * @param columnIndex field index * @return Field - * @throws ArrayIndexOutOfBoundsException if invalid columnIndex is specified + * @throws IndexOutOfBoundsException if invalid columnIndex is specified */ Field getField(int columnIndex) { return fieldValues[columnIndex]; @@ -210,7 +210,7 @@ public class DBRecord implements Comparable { * @param columnIndex field index * @param field field value to compare with * @return true if the fields are equal, else false. - * @throws ArrayIndexOutOfBoundsException if invalid columnIndex is specified + * @throws IndexOutOfBoundsException if invalid columnIndex is specified */ public boolean fieldEquals(int columnIndex, Field field) { return fieldValues[columnIndex].equals(field); @@ -223,7 +223,7 @@ public class DBRecord implements Comparable { * @return 0 if equals, a negative number if this record's field is less * than the specified value, or a positive number if this record's field is * greater than the specified value. - * @throws ArrayIndexOutOfBoundsException if invalid columnIndex is specified + * @throws IndexOutOfBoundsException if invalid columnIndex is specified */ public int compareFieldTo(int columnIndex, Field value) { return fieldValues[columnIndex].compareTo(value); @@ -270,7 +270,7 @@ public class DBRecord implements Comparable { * Get the long value for the specified field. * @param colIndex field index * @return field value - * @throws ArrayIndexOutOfBoundsException if invalid columnIndex is specified + * @throws IndexOutOfBoundsException if invalid columnIndex is specified * @throws IllegalFieldAccessException if field does support long data access */ public long getLongValue(int colIndex) { @@ -281,7 +281,7 @@ public class DBRecord implements Comparable { * Set the long value for the specified field. * @param colIndex field index * @param value field value - * @throws ArrayIndexOutOfBoundsException if invalid columnIndex is specified + * @throws IndexOutOfBoundsException if invalid columnIndex is specified * @throws IllegalFieldAccessException if field does support long data access */ public void setLongValue(int colIndex, long value) { @@ -293,7 +293,7 @@ public class DBRecord implements Comparable { * Get the integer value for the specified field. * @param colIndex field index * @return field value - * @throws ArrayIndexOutOfBoundsException if invalid columnIndex is specified + * @throws IndexOutOfBoundsException if invalid columnIndex is specified * @throws IllegalFieldAccessException if field does support integer data access */ public int getIntValue(int colIndex) { @@ -304,7 +304,7 @@ public class DBRecord implements Comparable { * Set the integer value for the specified field. * @param colIndex field index * @param value field value - * @throws ArrayIndexOutOfBoundsException if invalid columnIndex is specified + * @throws IndexOutOfBoundsException if invalid columnIndex is specified * @throws IllegalFieldAccessException if field does support integer data access */ public void setIntValue(int colIndex, int value) { @@ -316,7 +316,7 @@ public class DBRecord implements Comparable { * Get the short value for the specified field. * @param colIndex field index * @return field value - * @throws ArrayIndexOutOfBoundsException if invalid columnIndex is specified + * @throws IndexOutOfBoundsException if invalid columnIndex is specified * @throws IllegalFieldAccessException if field does support short data access */ public short getShortValue(int colIndex) { @@ -327,7 +327,7 @@ public class DBRecord implements Comparable { * Set the short value for the specified field. * @param colIndex field index * @param value field value - * @throws ArrayIndexOutOfBoundsException if invalid columnIndex is specified + * @throws IndexOutOfBoundsException if invalid columnIndex is specified * @throws IllegalFieldAccessException if field does support short data access */ public void setShortValue(int colIndex, short value) { @@ -339,7 +339,7 @@ public class DBRecord implements Comparable { * Get the byte value for the specified field. * @param colIndex field index * @return field value - * @throws ArrayIndexOutOfBoundsException if invalid columnIndex is specified + * @throws IndexOutOfBoundsException if invalid columnIndex is specified * @throws IllegalFieldAccessException if field does support byte data access */ public byte getByteValue(int colIndex) { @@ -350,7 +350,7 @@ public class DBRecord implements Comparable { * Set the byte value for the specified field. * @param colIndex field index * @param value field value - * @throws ArrayIndexOutOfBoundsException if invalid columnIndex is specified + * @throws IndexOutOfBoundsException if invalid columnIndex is specified * @throws IllegalFieldAccessException if field does support byte data access */ public void setByteValue(int colIndex, byte value) { @@ -362,7 +362,7 @@ public class DBRecord implements Comparable { * Get the boolean value for the specified field. * @param colIndex field index * @return field value - * @throws ArrayIndexOutOfBoundsException if invalid columnIndex is specified + * @throws IndexOutOfBoundsException if invalid columnIndex is specified * @throws IllegalFieldAccessException if field does support boolean data access */ public boolean getBooleanValue(int colIndex) { @@ -373,7 +373,7 @@ public class DBRecord implements Comparable { * Set the boolean value for the specified field. * @param colIndex field index * @param value field value - * @throws ArrayIndexOutOfBoundsException if invalid columnIndex is specified + * @throws IndexOutOfBoundsException if invalid columnIndex is specified * @throws IllegalFieldAccessException if field does support boolean data access */ public void setBooleanValue(int colIndex, boolean value) { @@ -385,7 +385,7 @@ public class DBRecord implements Comparable { * Get the binary data array for the specified field. * @param colIndex field index * @return field data - * @throws ArrayIndexOutOfBoundsException if invalid columnIndex is specified + * @throws IndexOutOfBoundsException if invalid columnIndex is specified * @throws IllegalFieldAccessException if field does support binary data access */ public byte[] getBinaryData(int colIndex) { @@ -396,7 +396,7 @@ public class DBRecord implements Comparable { * Set the binary data array for the specified field. * @param colIndex field index * @param bytes field value - * @throws ArrayIndexOutOfBoundsException if invalid columnIndex is specified + * @throws IndexOutOfBoundsException if invalid columnIndex is specified * @throws IllegalFieldAccessException if field does support binary data access * or incorrect number of bytes provided */ @@ -413,7 +413,7 @@ public class DBRecord implements Comparable { * Set the field to a null state. For a non-sparse fixed-length column field this will * set the the value to zero and the null state will not be persisted when stored. * @param colIndex field index - * @throws ArrayIndexOutOfBoundsException if invalid columnIndex is specified + * @throws IndexOutOfBoundsException if invalid columnIndex is specified */ public void setNull(int colIndex) { dirty = true; @@ -428,7 +428,7 @@ public class DBRecord implements Comparable { * Get the string value for the specified field. * @param colIndex field index * @return field data - * @throws ArrayIndexOutOfBoundsException if invalid columnIndex is specified + * @throws IndexOutOfBoundsException if invalid columnIndex is specified * @throws IllegalFieldAccessException if field does support string data access */ public String getString(int colIndex) { @@ -439,7 +439,7 @@ public class DBRecord implements Comparable { * Set the string value for the specified field. * @param colIndex field index * @param str field value - * @throws ArrayIndexOutOfBoundsException if invalid columnIndex is specified + * @throws IndexOutOfBoundsException if invalid columnIndex is specified * @throws IllegalFieldAccessException if field does support string data access */ public void setString(int colIndex, String str) { @@ -452,9 +452,10 @@ public class DBRecord implements Comparable { * Write the record fields to the specified buffer and offset. * @param buf data buffer * @param offset buffer offset + * @throws IndexOutOfBoundsException if invalid offset is specified * @throws IOException thrown if IO error occurs */ - public void write(Buffer buf, int offset) throws IOException { + public void write(Buffer buf, int offset) throws IndexOutOfBoundsException, IOException { for (Field fieldValue : fieldValues) { offset = fieldValue.write(buf, offset); } @@ -465,9 +466,10 @@ public class DBRecord implements Comparable { * Read the record field data from the specified buffer and offset * @param buf data buffer * @param offset buffer offset + * @throws IndexOutOfBoundsException if invalid offset is specified * @throws IOException thrown if IO error occurs */ - public void read(Buffer buf, int offset) throws IOException { + public void read(Buffer buf, int offset) throws IndexOutOfBoundsException, IOException { for (Field fieldValue : fieldValues) { offset = fieldValue.read(buf, offset); } diff --git a/Ghidra/Framework/DB/src/main/java/db/Field.java b/Ghidra/Framework/DB/src/main/java/db/Field.java index 2e7b20cd0d..17200a6bf2 100644 --- a/Ghidra/Framework/DB/src/main/java/db/Field.java +++ b/Ghidra/Framework/DB/src/main/java/db/Field.java @@ -347,9 +347,10 @@ public abstract class Field implements Comparable { * @param buf data buffer * @param offset data offset * @return next available Field offset within buffer, or -1 if end of buffer reached. + * @throws IndexOutOfBoundsException if invalid offset is specified * @throws IOException thrown if IO error occurs */ - abstract int write(Buffer buf, int offset) throws IOException; + abstract int write(Buffer buf, int offset) throws IndexOutOfBoundsException, IOException; /** * Read the field value from buf at the specified offset. When reading variable length @@ -357,9 +358,10 @@ public abstract class Field implements Comparable { * @param buf data buffer * @param offset data offset * @return next Field offset within buffer, or -1 if end of buffer reached. + * @throws IndexOutOfBoundsException if invalid offset is specified * @throws IOException thrown if IO error occurs */ - abstract int read(Buffer buf, int offset) throws IOException; + abstract int read(Buffer buf, int offset) throws IndexOutOfBoundsException, IOException; /** * Get the total number of bytes which will be read from the buffer @@ -370,9 +372,10 @@ public abstract class Field implements Comparable { * @param buf data buffer * @param offset data offset * @return total number of bytes for this field stored within buf + * @throws IndexOutOfBoundsException if invalid offset is specified * @throws IOException thrown if IO error occurs */ - abstract int readLength(Buffer buf, int offset) throws IOException; + abstract int readLength(Buffer buf, int offset) throws IndexOutOfBoundsException, IOException; /** * Get the number of bytes required to store this field value. @@ -445,8 +448,9 @@ public abstract class Field implements Comparable { * @return comparison value, zero if equal, -1 if this field has a value * less than the stored field, or +1 if this field has a value greater than * the stored field located at keyIndex. + * @throws IndexOutOfBoundsException if invalid offset is specified */ - abstract int compareTo(DataBuffer buffer, int offset); + abstract int compareTo(DataBuffer buffer, int offset) throws IndexOutOfBoundsException; /** * Compares this Field with another Field for order. Returns a @@ -515,8 +519,9 @@ public abstract class Field implements Comparable { /** * Get the type index value of the FixedField type which corresponds * to the specified fixed-length; - * @param fixedLength fixed length + * @param fixedLength fixed length (currently only 10 is supported) * @return FixedLength field type index + * @throws IllegalArgumentException if unsupported fixedLength is specified */ static byte getFixedType(int fixedLength) { if (fixedLength == 10) { diff --git a/Ghidra/Framework/DB/src/main/java/db/FixedField10.java b/Ghidra/Framework/DB/src/main/java/db/FixedField10.java index df11afccc3..c588c34d7e 100644 --- a/Ghidra/Framework/DB/src/main/java/db/FixedField10.java +++ b/Ghidra/Framework/DB/src/main/java/db/FixedField10.java @@ -188,7 +188,7 @@ public class FixedField10 extends FixedField { } @Override - int write(Buffer buf, int offset) throws IOException { + int write(Buffer buf, int offset) throws IndexOutOfBoundsException, IOException { if (data != null) { return buf.put(offset, data); } @@ -197,7 +197,7 @@ public class FixedField10 extends FixedField { } @Override - int read(Buffer buf, int offset) throws IOException { + int read(Buffer buf, int offset) throws IndexOutOfBoundsException, IOException { updatingValue(); data = null; // be lazy hi8 = buf.getLong(offset); @@ -206,7 +206,7 @@ public class FixedField10 extends FixedField { } @Override - int readLength(Buffer buf, int offset) throws IOException { + int readLength(Buffer buf, int offset) { return 10; } diff --git a/Ghidra/Framework/DB/src/main/java/db/IndexField.java b/Ghidra/Framework/DB/src/main/java/db/IndexField.java index 44cafbc278..836673ab16 100644 --- a/Ghidra/Framework/DB/src/main/java/db/IndexField.java +++ b/Ghidra/Framework/DB/src/main/java/db/IndexField.java @@ -108,19 +108,19 @@ class IndexField extends Field { } @Override - int write(Buffer buf, int offset) throws IOException { + int write(Buffer buf, int offset) throws IndexOutOfBoundsException, IOException { offset = indexedField.write(buf, offset); return primaryKey.write(buf, offset); } @Override - int read(Buffer buf, int offset) throws IOException { + int read(Buffer buf, int offset) throws IndexOutOfBoundsException, IOException { offset = indexedField.read(buf, offset); return primaryKey.read(buf, offset); } @Override - int readLength(Buffer buf, int offset) throws IOException { + int readLength(Buffer buf, int offset) throws IndexOutOfBoundsException, IOException { return indexedField.readLength(buf, offset) + primaryKey.length(); } @@ -229,7 +229,7 @@ class IndexField extends Field { } @Override - int compareTo(DataBuffer buffer, int offset) { + int compareTo(DataBuffer buffer, int offset) throws IndexOutOfBoundsException { int result = indexedField.compareTo(buffer, offset); if (result != 0) { return result; diff --git a/Ghidra/Framework/DB/src/main/java/db/IntField.java b/Ghidra/Framework/DB/src/main/java/db/IntField.java index 34a128bd3c..77c8b0ff28 100644 --- a/Ghidra/Framework/DB/src/main/java/db/IntField.java +++ b/Ghidra/Framework/DB/src/main/java/db/IntField.java @@ -94,19 +94,19 @@ public final class IntField extends PrimitiveField { } @Override - int write(Buffer buf, int offset) throws IOException { + int write(Buffer buf, int offset) throws IndexOutOfBoundsException, IOException { return buf.putInt(offset, value); } @Override - int read(Buffer buf, int offset) throws IOException { + int read(Buffer buf, int offset) throws IndexOutOfBoundsException, IOException { updatingPrimitiveValue(); value = buf.getInt(offset); return offset + 4; } @Override - int readLength(Buffer buf, int offset) throws IOException { + int readLength(Buffer buf, int offset) { return 4; } diff --git a/Ghidra/Framework/DB/src/main/java/db/LongField.java b/Ghidra/Framework/DB/src/main/java/db/LongField.java index 50804a7528..7d62faa200 100644 --- a/Ghidra/Framework/DB/src/main/java/db/LongField.java +++ b/Ghidra/Framework/DB/src/main/java/db/LongField.java @@ -94,19 +94,19 @@ public final class LongField extends PrimitiveField { } @Override - int write(Buffer buf, int offset) throws IOException { + int write(Buffer buf, int offset) throws IndexOutOfBoundsException, IOException { return buf.putLong(offset, value); } @Override - int read(Buffer buf, int offset) throws IOException { + int read(Buffer buf, int offset) throws IndexOutOfBoundsException, IOException { updatingPrimitiveValue(); value = buf.getLong(offset); return offset + 8; } @Override - int readLength(Buffer buf, int offset) throws IOException { + int readLength(Buffer buf, int offset) { return 8; } diff --git a/Ghidra/Framework/DB/src/main/java/db/MasterTable.java b/Ghidra/Framework/DB/src/main/java/db/MasterTable.java index 1bcc349b85..72a5d6ff94 100644 --- a/Ghidra/Framework/DB/src/main/java/db/MasterTable.java +++ b/Ghidra/Framework/DB/src/main/java/db/MasterTable.java @@ -51,7 +51,7 @@ class MasterTable { try { masterRecord.setRootBufferId(dbParms.get(DBParms.MASTER_TABLE_ROOT_BUFFER_ID_PARM)); } - catch (ArrayIndexOutOfBoundsException e) { + catch (IndexOutOfBoundsException e) { throw new IOException("Corrupt database parameters", e); } @@ -157,7 +157,7 @@ class MasterTable { table.tableRecordChanged(); } } - catch (ArrayIndexOutOfBoundsException e) { + catch (IndexOutOfBoundsException e) { throw new IOException("Corrupt database parameters", e); } diff --git a/Ghidra/Framework/DB/src/main/java/db/Schema.java b/Ghidra/Framework/DB/src/main/java/db/Schema.java index 03f79ef399..5f8ec96f06 100644 --- a/Ghidra/Framework/DB/src/main/java/db/Schema.java +++ b/Ghidra/Framework/DB/src/main/java/db/Schema.java @@ -325,7 +325,7 @@ public class Schema { initializeSparseColumnSet(sparseColumns); return consumed; } - catch (ArrayIndexOutOfBoundsException e) { + catch (IndexOutOfBoundsException e) { throw new UnsupportedFieldException("Incomplete sparse column data"); } } diff --git a/Ghidra/Framework/DB/src/main/java/db/ShortField.java b/Ghidra/Framework/DB/src/main/java/db/ShortField.java index bb4004f411..0af19b5564 100644 --- a/Ghidra/Framework/DB/src/main/java/db/ShortField.java +++ b/Ghidra/Framework/DB/src/main/java/db/ShortField.java @@ -94,12 +94,12 @@ public final class ShortField extends PrimitiveField { } @Override - int write(Buffer buf, int offset) throws IOException { + int write(Buffer buf, int offset) throws IndexOutOfBoundsException, IOException { return buf.putShort(offset, value); } @Override - int read(Buffer buf, int offset) throws IOException { + int read(Buffer buf, int offset) throws IndexOutOfBoundsException, IOException { updatingPrimitiveValue(); value = buf.getShort(offset); return offset + 2; diff --git a/Ghidra/Framework/DB/src/main/java/db/SparseRecord.java b/Ghidra/Framework/DB/src/main/java/db/SparseRecord.java index a872187a59..df30a9b8c0 100644 --- a/Ghidra/Framework/DB/src/main/java/db/SparseRecord.java +++ b/Ghidra/Framework/DB/src/main/java/db/SparseRecord.java @@ -45,7 +45,7 @@ public class SparseRecord extends DBRecord { } @Override - public void write(Buffer buf, int offset) throws IOException { + public void write(Buffer buf, int offset) throws IndexOutOfBoundsException, IOException { ArrayList sparseFieldIndexes = new ArrayList<>(); Field[] fields = getFields(); for (int i = 0; i < fields.length; i++) { @@ -74,7 +74,7 @@ public class SparseRecord extends DBRecord { } @Override - public void read(Buffer buf, int offset) throws IOException { + public void read(Buffer buf, int offset) throws IndexOutOfBoundsException, IOException { Field[] fields = getFields(); for (int i = 0; i < fields.length; i++) { Field f = fields[i]; diff --git a/Ghidra/Framework/DB/src/main/java/db/StringField.java b/Ghidra/Framework/DB/src/main/java/db/StringField.java index 8d29be268f..6fba812487 100644 --- a/Ghidra/Framework/DB/src/main/java/db/StringField.java +++ b/Ghidra/Framework/DB/src/main/java/db/StringField.java @@ -105,7 +105,7 @@ public final class StringField extends Field { } @Override - int write(Buffer buf, int offset) throws IOException { + int write(Buffer buf, int offset) throws IndexOutOfBoundsException, IOException { if (bytes == null) { return buf.putInt(offset, -1); } @@ -114,7 +114,7 @@ public final class StringField extends Field { } @Override - int read(Buffer buf, int offset) throws IOException { + int read(Buffer buf, int offset) throws IndexOutOfBoundsException, IOException { checkImmutable(); int len = buf.getInt(offset); offset += 4; @@ -131,7 +131,7 @@ public final class StringField extends Field { } @Override - int readLength(Buffer buf, int offset) throws IOException { + int readLength(Buffer buf, int offset) throws IndexOutOfBoundsException, IOException { int len = buf.getInt(offset); return (len < 0 ? 0 : len) + 4; } @@ -217,7 +217,7 @@ public final class StringField extends Field { } @Override - int compareTo(DataBuffer buffer, int offset) { + int compareTo(DataBuffer buffer, int offset) throws IndexOutOfBoundsException { StringField f = new StringField(); try { f.read(buffer, offset); diff --git a/Ghidra/Framework/DB/src/main/java/db/buffers/DataBuffer.java b/Ghidra/Framework/DB/src/main/java/db/buffers/DataBuffer.java index 0cff6aa14a..6bba19ef4f 100644 --- a/Ghidra/Framework/DB/src/main/java/db/buffers/DataBuffer.java +++ b/Ghidra/Framework/DB/src/main/java/db/buffers/DataBuffer.java @@ -150,7 +150,7 @@ public class DataBuffer implements Buffer, Externalizable { @Override public void get(int offset, byte[] bytes, int dataOffset, int length) - throws ArrayIndexOutOfBoundsException { + throws IndexOutOfBoundsException { System.arraycopy(data, offset, bytes, dataOffset, length); } @@ -160,7 +160,7 @@ public class DataBuffer implements Buffer, Externalizable { } @Override - public byte[] get(int offset, int length) throws ArrayIndexOutOfBoundsException { + public byte[] get(int offset, int length) throws IndexOutOfBoundsException { byte[] bytes = new byte[length]; System.arraycopy(data, offset, bytes, 0, bytes.length); return bytes; @@ -255,7 +255,7 @@ public class DataBuffer implements Buffer, Externalizable { * @param src source offset within this buffer * @param dest destination offset within this buffer * @param length length of data to be moved - * @throws ArrayIndexOutOfBoundsException is thrown if parameters result in + * @throws IndexOutOfBoundsException is thrown if parameters result in * data access beyond the buffer size. */ public void move(int src, int dest, int length) { @@ -269,7 +269,7 @@ public class DataBuffer implements Buffer, Externalizable { * @param buf source buffer * @param bufOffset source buffer offset * @param length amount of data to copy. - * @throws ArrayIndexOutOfBoundsException is thrown if parameters result in + * @throws IndexOutOfBoundsException is thrown if parameters result in * data access beyond the buffer size. */ public void copy(int offset, DataBuffer buf, int bufOffset, int length) { @@ -386,7 +386,7 @@ public class DataBuffer implements Buffer, Externalizable { * @param offset offset within this buffer * @param len length of data within this buffer * @return unsigned comparison result - * @throws ArrayIndexOutOfBoundsException if specified region is not + * @throws IndexOutOfBoundsException if specified region is not * contained within this buffer. */ public int unsignedCompareTo(byte[] otherData, int offset, int len) { diff --git a/Ghidra/Framework/DB/src/test/java/db/AbstractChainedBufferTest.java b/Ghidra/Framework/DB/src/test/java/db/AbstractChainedBufferTest.java index 44204255b5..9f93a33905 100644 --- a/Ghidra/Framework/DB/src/test/java/db/AbstractChainedBufferTest.java +++ b/Ghidra/Framework/DB/src/test/java/db/AbstractChainedBufferTest.java @@ -153,7 +153,7 @@ public abstract class AbstractChainedBufferTest extends AbstractGenericTest { try { cb.fill(0, 1, (byte) 0x12); } - catch (ArrayIndexOutOfBoundsException e) { + catch (IndexOutOfBoundsException e) { return; } @@ -241,7 +241,7 @@ public abstract class AbstractChainedBufferTest extends AbstractGenericTest { cb.put(-1, bytes2); Assert.fail(); } - catch (ArrayIndexOutOfBoundsException e) { + catch (IndexOutOfBoundsException e) { // good } @@ -249,7 +249,7 @@ public abstract class AbstractChainedBufferTest extends AbstractGenericTest { cb.put(size - 1, bytes2); Assert.fail(); } - catch (ArrayIndexOutOfBoundsException e) { + catch (IndexOutOfBoundsException e) { // good } @@ -257,7 +257,7 @@ public abstract class AbstractChainedBufferTest extends AbstractGenericTest { cb.put(size + 1, bytes2); Assert.fail(); } - catch (ArrayIndexOutOfBoundsException e) { + catch (IndexOutOfBoundsException e) { // good } @@ -265,7 +265,7 @@ public abstract class AbstractChainedBufferTest extends AbstractGenericTest { cb.get(-1, bytes2); Assert.fail(); } - catch (ArrayIndexOutOfBoundsException e) { + catch (IndexOutOfBoundsException e) { // good } @@ -273,7 +273,7 @@ public abstract class AbstractChainedBufferTest extends AbstractGenericTest { cb.get(size - 1, bytes2); Assert.fail(); } - catch (ArrayIndexOutOfBoundsException e) { + catch (IndexOutOfBoundsException e) { // good } @@ -281,7 +281,7 @@ public abstract class AbstractChainedBufferTest extends AbstractGenericTest { cb.get(size + 1, bytes2); Assert.fail(); } - catch (ArrayIndexOutOfBoundsException e) { + catch (IndexOutOfBoundsException e) { // good } } @@ -313,7 +313,7 @@ public abstract class AbstractChainedBufferTest extends AbstractGenericTest { cb.putByte(-1, (byte) 0); Assert.fail(); } - catch (ArrayIndexOutOfBoundsException e) { + catch (IndexOutOfBoundsException e) { // good } @@ -321,7 +321,7 @@ public abstract class AbstractChainedBufferTest extends AbstractGenericTest { cb.putByte(size + 1, (byte) 0); Assert.fail(); } - catch (ArrayIndexOutOfBoundsException e) { + catch (IndexOutOfBoundsException e) { // good } } @@ -361,7 +361,7 @@ public abstract class AbstractChainedBufferTest extends AbstractGenericTest { cb.getByte(-1); Assert.fail(); } - catch (ArrayIndexOutOfBoundsException e) { + catch (IndexOutOfBoundsException e) { // good } @@ -369,7 +369,7 @@ public abstract class AbstractChainedBufferTest extends AbstractGenericTest { cb.getByte(size + 1); Assert.fail(); } - catch (ArrayIndexOutOfBoundsException e) { + catch (IndexOutOfBoundsException e) { // good } } @@ -418,7 +418,7 @@ public abstract class AbstractChainedBufferTest extends AbstractGenericTest { cb.putInt(-1, 0); Assert.fail(); } - catch (ArrayIndexOutOfBoundsException e) { + catch (IndexOutOfBoundsException e) { // good } @@ -426,7 +426,7 @@ public abstract class AbstractChainedBufferTest extends AbstractGenericTest { cb.putInt(size - 1, 0); Assert.fail(); } - catch (ArrayIndexOutOfBoundsException e) { + catch (IndexOutOfBoundsException e) { // good } @@ -434,7 +434,7 @@ public abstract class AbstractChainedBufferTest extends AbstractGenericTest { cb.putInt(size + 1, 0); Assert.fail(); } - catch (ArrayIndexOutOfBoundsException e) { + catch (IndexOutOfBoundsException e) { // good } } @@ -482,7 +482,7 @@ public abstract class AbstractChainedBufferTest extends AbstractGenericTest { cb.getInt(-1); Assert.fail(); } - catch (ArrayIndexOutOfBoundsException e) { + catch (IndexOutOfBoundsException e) { // good } @@ -490,7 +490,7 @@ public abstract class AbstractChainedBufferTest extends AbstractGenericTest { cb.getInt(size - 1); Assert.fail(); } - catch (ArrayIndexOutOfBoundsException e) { + catch (IndexOutOfBoundsException e) { // good } @@ -498,7 +498,7 @@ public abstract class AbstractChainedBufferTest extends AbstractGenericTest { cb.getInt(size + 1); Assert.fail(); } - catch (ArrayIndexOutOfBoundsException e) { + catch (IndexOutOfBoundsException e) { // good } } @@ -559,7 +559,7 @@ public abstract class AbstractChainedBufferTest extends AbstractGenericTest { cb.putLong(-1, 0); Assert.fail(); } - catch (ArrayIndexOutOfBoundsException e) { + catch (IndexOutOfBoundsException e) { // good } @@ -567,7 +567,7 @@ public abstract class AbstractChainedBufferTest extends AbstractGenericTest { cb.putLong(size - 1, 0); Assert.fail(); } - catch (ArrayIndexOutOfBoundsException e) { + catch (IndexOutOfBoundsException e) { // good } @@ -575,7 +575,7 @@ public abstract class AbstractChainedBufferTest extends AbstractGenericTest { cb.putLong(size + 1, 0); Assert.fail(); } - catch (ArrayIndexOutOfBoundsException e) { + catch (IndexOutOfBoundsException e) { // good } } @@ -635,7 +635,7 @@ public abstract class AbstractChainedBufferTest extends AbstractGenericTest { cb.getLong(-1); Assert.fail(); } - catch (ArrayIndexOutOfBoundsException e) { + catch (IndexOutOfBoundsException e) { // good } @@ -643,7 +643,7 @@ public abstract class AbstractChainedBufferTest extends AbstractGenericTest { cb.getLong(size - 1); Assert.fail(); } - catch (ArrayIndexOutOfBoundsException e) { + catch (IndexOutOfBoundsException e) { // good } @@ -651,7 +651,7 @@ public abstract class AbstractChainedBufferTest extends AbstractGenericTest { cb.getLong(size + 1); Assert.fail(); } - catch (ArrayIndexOutOfBoundsException e) { + catch (IndexOutOfBoundsException e) { // good } } @@ -694,7 +694,7 @@ public abstract class AbstractChainedBufferTest extends AbstractGenericTest { cb.putShort(-1, (short) 0); Assert.fail(); } - catch (ArrayIndexOutOfBoundsException e) { + catch (IndexOutOfBoundsException e) { // good } @@ -702,7 +702,7 @@ public abstract class AbstractChainedBufferTest extends AbstractGenericTest { cb.putShort(size - 1, (short) 0); Assert.fail(); } - catch (ArrayIndexOutOfBoundsException e) { + catch (IndexOutOfBoundsException e) { // good } @@ -710,7 +710,7 @@ public abstract class AbstractChainedBufferTest extends AbstractGenericTest { cb.putShort(size + 1, (short) 0); Assert.fail(); } - catch (ArrayIndexOutOfBoundsException e) { + catch (IndexOutOfBoundsException e) { // good } } @@ -752,7 +752,7 @@ public abstract class AbstractChainedBufferTest extends AbstractGenericTest { cb.getShort(-1); Assert.fail(); } - catch (ArrayIndexOutOfBoundsException e) { + catch (IndexOutOfBoundsException e) { // good } @@ -760,7 +760,7 @@ public abstract class AbstractChainedBufferTest extends AbstractGenericTest { cb.getShort(size - 1); Assert.fail(); } - catch (ArrayIndexOutOfBoundsException e) { + catch (IndexOutOfBoundsException e) { // good } @@ -768,7 +768,7 @@ public abstract class AbstractChainedBufferTest extends AbstractGenericTest { cb.getShort(size + 1); Assert.fail(); } - catch (ArrayIndexOutOfBoundsException e) { + catch (IndexOutOfBoundsException e) { // good } } diff --git a/Ghidra/Framework/DB/src/test/java/db/DBBufferTest.java b/Ghidra/Framework/DB/src/test/java/db/DBBufferTest.java index 8732b1f4ed..62f208726b 100644 --- a/Ghidra/Framework/DB/src/test/java/db/DBBufferTest.java +++ b/Ghidra/Framework/DB/src/test/java/db/DBBufferTest.java @@ -23,6 +23,7 @@ import java.util.Arrays; import org.junit.*; import generic.test.AbstractGenericTest; +import ghidra.util.exception.ClosedException; public class DBBufferTest extends AbstractGenericTest { @@ -109,6 +110,32 @@ public class DBBufferTest extends AbstractGenericTest { buf.get(0, data); assertTrue("Expected bytes in buffer", Arrays.equals(bytes, data)); + dbh.close(); + + try { + txId = dbh.startTransaction(); + fail("Expected closed exception"); + } + catch (IllegalStateException e) { + // ignore + } + + try { + dbh.getBuffer(bufId); + fail("Expected closed exception"); + } + catch (ClosedException e) { + // ignore + } + + try { + buf.get(0, data); + fail("Expected closed exception"); + } + catch (ClosedException e) { + // ignore + } + } @Test @@ -159,6 +186,6 @@ public class DBBufferTest extends AbstractGenericTest { dbh.undo(); buf = dbh.getBuffer(bufId); assertEquals("Expected valid buffer", 0, buf.getByte(0)); - + } } diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/mem/BufferSubMemoryBlock.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/mem/BufferSubMemoryBlock.java index fde2b809a2..323afbc166 100644 --- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/mem/BufferSubMemoryBlock.java +++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/mem/BufferSubMemoryBlock.java @@ -40,13 +40,14 @@ class BufferSubMemoryBlock extends SubMemoryBlock { } @Override - public byte getByte(long offsetInMemBlock) throws IOException { + public byte getByte(long offsetInMemBlock) throws IndexOutOfBoundsException, IOException { long offsetInSubBlock = offsetInMemBlock - subBlockOffset; return buf.getByte((int) offsetInSubBlock); } @Override - public int getBytes(long offsetInMemBlock, byte[] b, int off, int len) throws IOException { + public int getBytes(long offsetInMemBlock, byte[] b, int off, int len) + throws IndexOutOfBoundsException, IOException { long offsetInSubBlock = offsetInMemBlock - subBlockOffset; long available = subBlockLength - offsetInSubBlock; len = (int) Math.min(len, available); @@ -55,13 +56,15 @@ class BufferSubMemoryBlock extends SubMemoryBlock { } @Override - public void putByte(long offsetInMemBlock, byte b) throws IOException { + public void putByte(long offsetInMemBlock, byte b) + throws IndexOutOfBoundsException, IOException { long offsetInSubBlock = offsetInMemBlock - subBlockOffset; buf.putByte((int) offsetInSubBlock, b); } @Override - public int putBytes(long offsetInMemBlock, byte[] b, int off, int len) throws IOException { + public int putBytes(long offsetInMemBlock, byte[] b, int off, int len) + throws IndexOutOfBoundsException, IOException { long offsetInSubBlock = offsetInMemBlock - subBlockOffset; long available = subBlockLength - offsetInSubBlock; len = (int) Math.min(len, available); @@ -95,7 +98,8 @@ class BufferSubMemoryBlock extends SubMemoryBlock { } @Override - protected SubMemoryBlock split(long memBlockOffset) throws IOException { + protected SubMemoryBlock split(long memBlockOffset) + throws IndexOutOfBoundsException, IOException { // convert from offset in block to offset in this sub block int offset = (int) (memBlockOffset - subBlockOffset); long newLength = subBlockLength - offset; diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/mem/FileBytes.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/mem/FileBytes.java index e8c8643edd..13b00d4480 100644 --- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/mem/FileBytes.java +++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/mem/FileBytes.java @@ -208,9 +208,10 @@ public class FileBytes { * * @param offset the offset into the file bytes. * @param b the new byte value; + * @throws IndexOutOfBoundsException if invalid offset is specified * @throws IOException if the write to the database fails. */ - synchronized void putByte(long offset, byte b) throws IOException { + synchronized void putByte(long offset, byte b) throws IndexOutOfBoundsException, IOException { checkValid(); @@ -252,6 +253,7 @@ public class FileBytes { * @param off the offset into the byte array to get the bytes to write. * @param length the number of bytes to write. * @return the number of bytes written + * @throws IndexOutOfBoundsException if invalid offset is specified * @throws IOException if the write to the database fails. */ synchronized int putBytes(long offset, byte[] b, int off, int length) throws IOException { @@ -294,7 +296,8 @@ public class FileBytes { return length; } - private byte getByte(DBBuffer[] buffers, long offset) throws IOException { + private byte getByte(DBBuffer[] buffers, long offset) + throws IndexOutOfBoundsException, IOException { checkValid(); @@ -313,7 +316,7 @@ public class FileBytes { } private int getBytes(DBBuffer[] buffers, long offset, byte[] b, int off, int length) - throws IOException { + throws IndexOutOfBoundsException, IOException { checkValid(); diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/mem/MemoryBlockDB.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/mem/MemoryBlockDB.java index 36ca698157..7111fe1248 100644 --- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/mem/MemoryBlockDB.java +++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/mem/MemoryBlockDB.java @@ -344,7 +344,8 @@ public class MemoryBlockDB implements MemoryBlock { } @Override - public int getBytes(Address addr, byte[] b, int off, int len) throws MemoryAccessException { + public int getBytes(Address addr, byte[] b, int off, int len) + throws IndexOutOfBoundsException, MemoryAccessException { if (memMap.getLiveMemoryHandler() != null) { return memMap.getBytes(addr, b, off, len); } @@ -379,7 +380,8 @@ public class MemoryBlockDB implements MemoryBlock { } @Override - public int putBytes(Address addr, byte[] b, int off, int len) throws MemoryAccessException { + public int putBytes(Address addr, byte[] b, int off, int len) + throws IndexOutOfBoundsException, MemoryAccessException { if (memMap.getLiveMemoryHandler() != null) { memMap.setBytes(addr, b, off, len); return len; @@ -460,12 +462,13 @@ public class MemoryBlockDB implements MemoryBlock { return 0; } - public int getBytes(long offset, byte[] b, int off, int len) throws MemoryAccessException { + public int getBytes(long offset, byte[] b, int off, int len) + throws IndexOutOfBoundsException, MemoryAccessException { if (off < 0 || off + len > b.length) { - throw new ArrayIndexOutOfBoundsException(); + throw new IndexOutOfBoundsException(); } if (offset < 0 || offset >= length) { - throw new ArrayIndexOutOfBoundsException(); + throw new IndexOutOfBoundsException(); } len = (int) Math.min(len, length - offset); @@ -512,12 +515,13 @@ public class MemoryBlockDB implements MemoryBlock { } } - private int putBytes(long offset, byte[] b, int off, int len) throws MemoryAccessException { + private int putBytes(long offset, byte[] b, int off, int len) + throws IndexOutOfBoundsException, MemoryAccessException { if (off < 0 || off + len > b.length) { - throw new ArrayIndexOutOfBoundsException(); + throw new IndexOutOfBoundsException(); } if (offset < 0 || offset >= length) { - throw new ArrayIndexOutOfBoundsException(); + throw new IndexOutOfBoundsException(); } len = (int) Math.min(len, length - offset); diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/mem/MemoryBlockInputStream.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/mem/MemoryBlockInputStream.java index 2b33d7779f..fd2ed5297c 100644 --- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/mem/MemoryBlockInputStream.java +++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/mem/MemoryBlockInputStream.java @@ -102,7 +102,7 @@ class MemoryBlockInputStream extends InputStream { } @Override - public int read(byte[] b, int off, int len) throws IOException { + public int read(byte[] b, int off, int len) throws IndexOutOfBoundsException, IOException { if (index >= numBytes) { return -1; } diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/mem/SubMemoryBlock.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/mem/SubMemoryBlock.java index 570f24ffc6..adcc503d11 100644 --- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/mem/SubMemoryBlock.java +++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/database/mem/SubMemoryBlock.java @@ -89,10 +89,12 @@ abstract class SubMemoryBlock implements Comparable { * * @param memBlockOffset the offset from the start of the containing {@link MemoryBlockDB} * @return the byte at the given containing block offset. + * @throws IndexOutOfBoundsException if invalid offset is specified * @throws MemoryAccessException if the block is uninitialized. * @throws IOException if there is a problem reading from the database */ - public abstract byte getByte(long memBlockOffset) throws MemoryAccessException, IOException; + public abstract byte getByte(long memBlockOffset) + throws IndexOutOfBoundsException, MemoryAccessException, IOException; /** * Tries to get len bytes from this block at the given offset (relative to the containing @@ -103,13 +105,14 @@ abstract class SubMemoryBlock implements Comparable { * @param off the offset into the byte array. * @param len the number of bytes to get. * @return the number of bytes actually populated. + * @throws IndexOutOfBoundsException if invalid offset is specified * @throws MemoryAccessException if any of the requested bytes are * uninitialized. * @throws IOException if there is a problem reading from the database * @throws IllegalArgumentException if the offset is not in this block. */ public abstract int getBytes(long memBlockOffset, byte[] b, int off, int len) - throws MemoryAccessException, IOException; + throws IndexOutOfBoundsException, MemoryAccessException, IOException; /** * Stores the byte in this sub block at the given offset relative to the containing @@ -118,12 +121,13 @@ abstract class SubMemoryBlock implements Comparable { * * @param memBlockOffset the offset from the start of the containing {@link MemoryBlockDB} * @param b the byte value to store at the given offset. + * @throws IndexOutOfBoundsException if invalid offset is specified * @throws MemoryAccessException if the block is uninitialized * @throws IOException if there is a problem writing to the database * @throws IllegalArgumentException if the offset is not in this block. */ public abstract void putByte(long memBlockOffset, byte b) - throws MemoryAccessException, IOException; + throws IndexOutOfBoundsException, MemoryAccessException, IOException; /** * Tries to write len bytes to this block at the given offset (relative to the containing @@ -136,12 +140,13 @@ abstract class SubMemoryBlock implements Comparable { * @param off the offset into the byte array. * @param len the number of bytes to write. * @return the number of bytes actually written + * @throws IndexOutOfBoundsException if invalid offset is specified * @throws MemoryAccessException if this block is uninitialized. * @throws IOException if there is a problem writing to the database * @throws IllegalArgumentException if the offset is not in this block. */ public abstract int putBytes(long memBlockOffset, byte[] b, int off, int len) - throws MemoryAccessException, IOException; + throws IndexOutOfBoundsException, MemoryAccessException, IOException; /** * Deletes this SumMemoryBlock @@ -204,9 +209,11 @@ abstract class SubMemoryBlock implements Comparable { * To get the offset relative to this SubMemoryBlock, you have to subtract this sub blocks * starting offset. * @return the new SubMemoryBlock that contains the back half of this block + * @throws IndexOutOfBoundsException if invalid offset is specified * @throws IOException if a database error occurs. */ - protected abstract SubMemoryBlock split(long memBlockOffset) throws IOException; + protected abstract SubMemoryBlock split(long memBlockOffset) + throws IndexOutOfBoundsException, IOException; /** * Updates this SubMemoryBlock to have a new owning MemoryBlock and offset within that block. diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/mem/Memory.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/mem/Memory.java index 3780ef5fa3..01feccccf6 100644 --- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/mem/Memory.java +++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/mem/Memory.java @@ -476,6 +476,7 @@ public interface Memory extends AddressSetView { * @param size the number of bytes to get. * @return the number of bytes put into dest. May be less than * size if the requested number extends beyond initialized / available memory. + * @throws IndexOutOfBoundsException if an invalid index is specified * @throws MemoryAccessException if the starting address is * not contained in any memory block or is an uninitialized location. */ diff --git a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/mem/MemoryBlock.java b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/mem/MemoryBlock.java index 0d8e5d2b46..bbfcb0e7ef 100644 --- a/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/mem/MemoryBlock.java +++ b/Ghidra/Framework/SoftwareModeling/src/main/java/ghidra/program/model/mem/MemoryBlock.java @@ -231,10 +231,12 @@ public interface MemoryBlock extends Serializable, Comparable { * @param off the offset into the byte array. * @param len the number of bytes to get. * @return the number of bytes actually populated. + * @throws IndexOutOfBoundsException if invalid offset is specified * @throws MemoryAccessException if any of the requested bytes are uninitialized. * @throws IllegalArgumentException if the Address is not in this block. */ - public int getBytes(Address addr, byte[] b, int off, int len) throws MemoryAccessException; + public int getBytes(Address addr, byte[] b, int off, int len) + throws IndexOutOfBoundsException, MemoryAccessException; /** * Puts the given byte at the given address in this block. @@ -266,18 +268,24 @@ public interface MemoryBlock extends Serializable, Comparable { * @param off the offset into the byte array. * @param len the number of bytes to write. * @return the number of bytes actually written. + * @throws IndexOutOfBoundsException if invalid offset is specified * @throws MemoryAccessException if the block is uninitialized * @throws IllegalArgumentException if the Address is not in this block. */ - public int putBytes(Address addr, byte[] b, int off, int len) throws MemoryAccessException; + public int putBytes(Address addr, byte[] b, int off, int len) + throws IndexOutOfBoundsException, MemoryAccessException; /** * Get the type for this block: DEFAULT, BIT_MAPPED, or BYTE_MAPPED + * (see {@link MemoryBlockType}). + * @return memory block type */ public MemoryBlockType getType(); /** * Return whether this block has been initialized. + * + * @return true if block is fully initialized else false */ public boolean isInitialized();