Skip to content

Commit f92c8e6

Browse files
Audit BigIntegers and ensure error on overflow
1 parent 089bf57 commit f92c8e6

File tree

7 files changed

+38
-2
lines changed

7 files changed

+38
-2
lines changed

src/main/java/io/tiledb/java/api/Array.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,7 @@ private synchronized void openArray(
266266
byte[] key,
267267
BigInteger timestamp_end)
268268
throws TileDBError {
269+
Util.checkBigIntegerRange(timestamp_end);
269270
SWIGTYPE_p_p_tiledb_array_t _arraypp = tiledb.new_tiledb_array_tpp();
270271
try {
271272
ctx.handleError(tiledb.tiledb_array_alloc(ctx.getCtxp(), uri, _arraypp));
@@ -316,6 +317,8 @@ private synchronized void openArray(
316317
BigInteger timestamp_start,
317318
BigInteger timestamp_end)
318319
throws TileDBError {
320+
Util.checkBigIntegerRange(timestamp_start);
321+
Util.checkBigIntegerRange(timestamp_end);
319322
SWIGTYPE_p_p_tiledb_array_t _arraypp = tiledb.new_tiledb_array_tpp();
320323
try {
321324
ctx.handleError(tiledb.tiledb_array_alloc(ctx.getCtxp(), uri, _arraypp));
@@ -366,6 +369,7 @@ private synchronized void openArray(
366369

367370
private synchronized void openArray(
368371
Context ctx, String uri, QueryType query_type, BigInteger timestamp_end) throws TileDBError {
372+
Util.checkBigIntegerRange(timestamp_end);
369373
SWIGTYPE_p_p_tiledb_array_t _arraypp = tiledb.new_tiledb_array_tpp();
370374
try {
371375
ctx.handleError(tiledb.tiledb_array_alloc(ctx.getCtxp(), uri, _arraypp));
@@ -406,6 +410,8 @@ private synchronized void openArray(
406410
BigInteger timestamp_start,
407411
BigInteger timestamp_end)
408412
throws TileDBError {
413+
Util.checkBigIntegerRange(timestamp_start);
414+
Util.checkBigIntegerRange(timestamp_end);
409415
SWIGTYPE_p_p_tiledb_array_t _arraypp = tiledb.new_tiledb_array_tpp();
410416
try {
411417
ctx.handleError(tiledb.tiledb_array_alloc(ctx.getCtxp(), uri, _arraypp));
@@ -1031,7 +1037,7 @@ public Pair<String, NativeArray> getMetadataFromIndex(long index) throws TileDBE
10311037
*/
10321038
public Pair<String, NativeArray> getMetadataFromIndex(BigInteger index) throws TileDBError {
10331039
checkIsOpen();
1034-
1040+
Util.checkBigIntegerRange(index);
10351041
SWIGTYPE_p_p_char key = tiledb.new_charpp();
10361042
SWIGTYPE_p_unsigned_int key_len = tiledb.new_uintp();
10371043
SWIGTYPE_p_tiledb_datatype_t value_type = tiledb.new_tiledb_datatype_tp();
@@ -1163,6 +1169,7 @@ public ArraySchema getSchema() throws TileDBError {
11631169
* bound. The default value is `0`.
11641170
*/
11651171
public void setOpenTimestampStart(BigInteger timestamp) throws TileDBError {
1172+
Util.checkBigIntegerRange(timestamp);
11661173
try {
11671174
ctx.handleError(
11681175
tiledb.tiledb_array_set_open_timestamp_start(ctx.getCtxp(), getArrayp(), timestamp));
@@ -1177,6 +1184,7 @@ public void setOpenTimestampStart(BigInteger timestamp) throws TileDBError {
11771184
* current timestamp when an array is opened. The default value is `UINT64_MAX`.
11781185
*/
11791186
public void setOpenTimestampEnd(BigInteger timestamp) throws TileDBError {
1187+
Util.checkBigIntegerRange(timestamp);
11801188
try {
11811189
ctx.handleError(
11821190
tiledb.tiledb_array_set_open_timestamp_end(ctx.getCtxp(), getArrayp(), timestamp));

src/main/java/io/tiledb/java/api/ArraySchema.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ public void setCapacity(long capacity) throws TileDBError {
270270
* @exception TileDBError A T
271271
*/
272272
public void setCapacity(BigInteger capacity) throws TileDBError {
273+
Util.checkBigIntegerRange(capacity);
273274
ctx.handleError(tiledb.tiledb_array_schema_set_capacity(ctx.getCtxp(), schemap, capacity));
274275
}
275276

src/main/java/io/tiledb/java/api/ArraySchemaEvolution.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ public void dropAttribute(Attribute att) throws TileDBError {
116116
* @throws TileDBError
117117
*/
118118
public void setTimeStampRange(BigInteger high, BigInteger low) throws TileDBError {
119+
Util.checkBigIntegerRange(high);
120+
Util.checkBigIntegerRange(low);
119121
try {
120122
ctx.handleError(
121123
tiledb.tiledb_array_schema_evolution_set_timestamp_range(

src/main/java/io/tiledb/java/api/Attribute.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ public FilterList getFilterList() throws TileDBError {
275275
* @throws TileDBError
276276
*/
277277
public void setFillValue(NativeArray value, BigInteger size) throws TileDBError {
278+
Util.checkBigIntegerRange(size);
278279
try {
279280
ctx.handleError(
280281
tiledb.tiledb_attribute_set_fill_value(
@@ -366,7 +367,7 @@ public Pair<Object, Integer> getFillValue() throws TileDBError {
366367
*/
367368
public void setFillValueNullable(NativeArray value, BigInteger size, boolean valid)
368369
throws TileDBError {
369-
370+
Util.checkBigIntegerRange(size);
370371
try {
371372
ctx.handleError(
372373
tiledb.tiledb_attribute_set_fill_value_nullable(

src/main/java/io/tiledb/java/api/Group.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ public long getMemberCount() throws TileDBError {
198198
* @throws TileDBError
199199
*/
200200
public String getMemberURIByIndex(BigInteger index) throws TileDBError {
201+
Util.checkBigIntegerRange(index);
201202
SWIGTYPE_p_tiledb_object_t objtypep = tiledb.new_tiledb_object_tp();
202203
SWIGTYPE_p_p_char uripp = tiledb.new_charpp();
203204
SWIGTYPE_p_p_char namepp = tiledb.new_charpp(); // useless in this method
@@ -230,6 +231,7 @@ public String getMemberURIByName(String name) throws TileDBError {
230231
* @throws TileDBError
231232
*/
232233
public String getMemberNameByIndex(BigInteger index) throws TileDBError {
234+
Util.checkBigIntegerRange(index);
233235
SWIGTYPE_p_tiledb_object_t objtypep = tiledb.new_tiledb_object_tp();
234236
SWIGTYPE_p_p_char uripp = tiledb.new_charpp(); // useless in this method
235237
SWIGTYPE_p_p_char namepp = tiledb.new_charpp();
@@ -287,6 +289,7 @@ public void dumpStr(String string, boolean flag) throws TileDBError {
287289
* @throws TileDBError A TileDB exception
288290
*/
289291
public Pair<String, NativeArray> getMetadataFromIndex(BigInteger index) throws TileDBError {
292+
Util.checkBigIntegerRange(index);
290293
if (!isOpen()) throw new TileDBError("Group with URI: " + uri + " is closed");
291294
SWIGTYPE_p_p_char key = tiledb.new_charpp();
292295
SWIGTYPE_p_unsigned_int key_len = tiledb.new_uintp();

src/main/java/io/tiledb/java/api/SubArray.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ public synchronized SubArray addRange(int dimIdx, Object start, Object end, Obje
115115
*/
116116
public synchronized SubArray addPointRanges(int dimIdx, Object start, BigInteger count)
117117
throws TileDBError {
118+
Util.checkBigIntegerRange(count);
118119
Datatype dimType;
119120
int values[];
120121
try (ArraySchema schema = array.getSchema();
@@ -395,6 +396,7 @@ public Pair<Object, Object> getRangeFromName(String name, long rangeIdx) throws
395396
*/
396397
public synchronized Pair<Long, Long> getRangeVarSize(int dimIdx, BigInteger rangeIdx)
397398
throws TileDBError {
399+
Util.checkBigIntegerRange(rangeIdx);
398400
SWIGTYPE_p_unsigned_long_long startSize = tiledb.new_ullp();
399401
SWIGTYPE_p_unsigned_long_long endSize = tiledb.new_ullp();
400402
try {
@@ -418,6 +420,7 @@ public synchronized Pair<Long, Long> getRangeVarSize(int dimIdx, BigInteger rang
418420
*/
419421
public synchronized Pair<Long, Long> getRangeVarSizeByName(String name, BigInteger rangeIdx)
420422
throws TileDBError {
423+
Util.checkBigIntegerRange(rangeIdx);
421424
SWIGTYPE_p_unsigned_long_long startSize = tiledb.new_ullp();
422425
SWIGTYPE_p_unsigned_long_long endSize = tiledb.new_ullp();
423426
try {
@@ -442,6 +445,7 @@ public synchronized Pair<Long, Long> getRangeVarSizeByName(String name, BigInteg
442445
*/
443446
public synchronized Pair<String, String> getRangeVar(int dimIdx, BigInteger rangeIdx)
444447
throws TileDBError {
448+
Util.checkBigIntegerRange(rangeIdx);
445449
Datatype dimType;
446450
try (ArraySchema schema = array.getSchema();
447451
Domain domain = schema.getDomain()) {
@@ -478,6 +482,7 @@ public synchronized Pair<String, String> getRangeVar(int dimIdx, BigInteger rang
478482
*/
479483
public synchronized Pair<String, String> getRangeVarByName(String name, BigInteger rangeIdx)
480484
throws TileDBError {
485+
Util.checkBigIntegerRange(rangeIdx);
481486
Datatype dimType;
482487
try (ArraySchema schema = array.getSchema();
483488
Domain domain = schema.getDomain()) {

src/main/java/io/tiledb/java/api/Util.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.tiledb.java.api;
22

3+
import java.math.BigInteger;
34
import java.util.Arrays;
45

56
/** Contains helper-functions */
@@ -37,6 +38,21 @@ public static int castLongToInt(long num) throws TileDBError {
3738
return (int) num;
3839
}
3940

41+
/**
42+
* Checks if a given bigInteger value is in the UInt64 range
43+
*
44+
* @param bigInteger the bigInteger
45+
* @return true if the range is ok
46+
*/
47+
public static boolean checkBigIntegerRange(BigInteger bigInteger) throws TileDBError {
48+
BigInteger upperLimit = new BigInteger("18446744073709551615");
49+
if ((bigInteger.compareTo(upperLimit) > 0)
50+
|| (bigInteger.compareTo(BigInteger.valueOf(-1L)) <= 0)) {
51+
throw new TileDBError(bigInteger + " is outside the uint64 range");
52+
}
53+
return true;
54+
}
55+
4056
/**
4157
* Returns the Datatype of the input field
4258
*

0 commit comments

Comments
 (0)