Skip to content

Commit 04ec38b

Browse files
committed
#924 Fixed: IndexOutOfBoundsException in FBCachedBlob.getBytes(long, int) for position or length beyond end of data
1 parent 478ea20 commit 04ec38b

3 files changed

Lines changed: 32 additions & 3 deletions

File tree

src/docs/asciidoc/release_notes.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ This results in two minor breaking changes:
5252
** Reserved words are no longer considered simple identifiers by `enquoteIdentifier` and `isSimpleIdentifier` and will be quoted (or -- for dialect 1 -- result in a `SQLFeatureNotSupportedException`)
5353
** Presence of the NUL character (U+0000) in an identifier passed to `enquoteIdentifier` will result in a `SQLSyntaxErrorException`
5454
* Fixed: JDBC escapes should not be parsed inside dialect 3 delimited identifiers or dialect 1 string literals (https://github.com/FirebirdSQL/jaybird/issues/922[#922])
55+
* Fixed: `IndexOutOfBoundsException` in `FBCachedBlob.getBytes(long, int)` for position or length beyond end of data (https://github.com/FirebirdSQL/jaybird/issues/924[#924])
5556
5657
[#jaybird-5-0-11-changelog]
5758
=== Jaybird 5.0.11

src/main/org/firebirdsql/jdbc/FBCachedBlob.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import org.firebirdsql.gds.JaybirdErrorCodes;
2222
import org.firebirdsql.gds.ng.FbExceptionBuilder;
23+
import org.firebirdsql.util.ByteArrayHelper;
2324

2425
import java.sql.SQLException;
2526
import java.sql.Blob;
@@ -81,15 +82,17 @@ public byte[] getBytes(long pos, int length) throws SQLException {
8182
if (pos < 1) {
8283
throw new SQLException("Expected value of pos > 0, got " + pos,
8384
SQLStateConstants.SQL_STATE_INVALID_ARG_VALUE);
84-
}
85-
if (length < 0) {
85+
} else if (length < 0) {
8686
throw new SQLException("Expected value of length >= 0, got " + length,
8787
SQLStateConstants.SQL_STATE_INVALID_ARG_VALUE);
8888
}
8989
checkClosed();
90+
byte[] blobData = this.blobData;
9091
if (blobData == null) return BYTES_NULL_VALUE;
9192

92-
// TODO What if pos or length are beyond blobData
93+
if (pos > blobData.length) return ByteArrayHelper.emptyByteArray();
94+
length = (int) Math.min(length, blobData.length - pos + 1L);
95+
9396
byte[] result = new byte[length];
9497
System.arraycopy(blobData, (int) pos - 1, result, 0, length);
9598
return result;

src/test/org/firebirdsql/jdbc/FBCachedBlobTest.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import org.firebirdsql.gds.JaybirdErrorCodes;
2222
import org.hamcrest.Matcher;
2323
import org.junit.jupiter.api.Test;
24+
import org.junit.jupiter.params.ParameterizedTest;
25+
import org.junit.jupiter.params.provider.ValueSource;
2426

2527
import java.io.ByteArrayOutputStream;
2628
import java.io.InputStream;
@@ -31,6 +33,7 @@
3133
import static org.firebirdsql.common.matchers.SQLExceptionMatchers.*;
3234
import static org.hamcrest.MatcherAssert.assertThat;
3335
import static org.hamcrest.CoreMatchers.*;
36+
import static org.hamcrest.Matchers.greaterThan;
3437
import static org.junit.jupiter.api.Assertions.*;
3538

3639
/**
@@ -162,6 +165,28 @@ void testGetBytes() throws Exception {
162165
assertArrayEquals(new byte[] { 5, 6, 7, 8, 9 }, data, "Unexpected data");
163166
}
164167

168+
@ParameterizedTest
169+
@ValueSource(longs = { 11, Integer.MAX_VALUE, Integer.MAX_VALUE + 1L, Long.MAX_VALUE })
170+
void testGetBytes_long_int_posBeyondEnd(long pos) throws Exception {
171+
assertThat("Wrong value for pos", pos, greaterThan(10L));
172+
FBCachedBlob blob = new FBCachedBlob(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
173+
174+
byte[] data = blob.getBytes(pos, 5);
175+
176+
assertNotNull(data, "Expected non-null array");
177+
assertArrayEquals(new byte[0], data, "Unexpected data");
178+
}
179+
180+
@Test
181+
void testGetBytes_long_int_lengthBeyondEnd() throws Exception {
182+
FBCachedBlob blob = new FBCachedBlob(new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
183+
184+
byte[] data = blob.getBytes(5, 10);
185+
186+
assertNotNull(data, "Expected non-null array");
187+
assertArrayEquals(new byte[] { 5, 6, 7, 8, 9, 10 }, data, "Unexpected data");
188+
}
189+
165190
/**
166191
* Test if {@link FBCachedBlob#position(byte[], long)} throws SQLFeatureNotSupportedException.
167192
*/

0 commit comments

Comments
 (0)