Skip to content

Commit ced79e0

Browse files
committed
#923 Fixed: IndexOutOfBoundsException in FBCachedBlob.getBytes(long, int) for position or length beyond end of data
1 parent 8643b6d commit ced79e0

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
@@ -49,6 +49,7 @@ This results in two minor breaking changes:
4949
** Reserved words are no longer considered simple identifiers by `enquoteIdentifier` and `isSimpleIdentifier` and will be quoted (or -- for dialect 1 -- result in a `SQLFeatureNotSupportedException`)
5050
** Presence of the NUL character (U+0000) in an identifier passed to `enquoteIdentifier` will result in a `SQLSyntaxErrorException`
5151
* Fixed: JDBC escapes should not be parsed inside dialect 3 delimited identifiers or dialect 1 string literals (https://github.com/FirebirdSQL/jaybird/issues/921[#921])
52+
* Fixed: `IndexOutOfBoundsException` in `FBCachedBlob.getBytes(long, int)` for position or length beyond end of data (https://github.com/FirebirdSQL/jaybird/issues/923[#923])
5253

5354
=== Jaybird 6.0.4
5455

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.jaybird.util.ByteArrayHelper;
2324
import org.firebirdsql.util.InternalApi;
2425

2526
import java.sql.SQLException;
@@ -88,15 +89,17 @@ public byte[] getBytes(long pos, int length) throws SQLException {
8889
if (pos < 1) {
8990
throw new SQLException("Expected value of pos > 0, got " + pos,
9091
SQLStateConstants.SQL_STATE_INVALID_STRING_LENGTH);
91-
}
92-
if (length < 0) {
92+
} else if (length < 0) {
9393
throw new SQLException("Expected value of length >= 0, got " + length,
9494
SQLStateConstants.SQL_STATE_INVALID_STRING_LENGTH);
9595
}
9696
checkClosed();
97+
byte[] blobData = this.blobData;
9798
if (blobData == null) return null;
9899

99-
// TODO What if pos or length are beyond blobData
100+
if (pos > blobData.length) return ByteArrayHelper.emptyByteArray();
101+
length = (int) Math.min(length, blobData.length - pos + 1L);
102+
100103
byte[] result = new byte[length];
101104
System.arraycopy(blobData, (int) pos - 1, result, 0, length);
102105
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_long_int() 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
@Test
166191
void testGetBytes() throws Exception {
167192
byte[] input = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

0 commit comments

Comments
 (0)