From 7920faaeb52047f0811c29b3f336f6c98c5b173f Mon Sep 17 00:00:00 2001 From: souraOP Date: Fri, 28 Feb 2025 16:36:24 +0530 Subject: [PATCH 1/2] Added bound checking, toString method and add documentation for unsigned integer --- android/.project | 6 +++++ .../com/google/flatbuffers/IntVector.java | 23 +++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/android/.project b/android/.project index 17f0659d4a1..6f27ae6074a 100644 --- a/android/.project +++ b/android/.project @@ -5,8 +5,14 @@ + + org.eclipse.buildship.core.gradleprojectbuilder + + + + org.eclipse.buildship.core.gradleprojectnature diff --git a/java/src/main/java/com/google/flatbuffers/IntVector.java b/java/src/main/java/com/google/flatbuffers/IntVector.java index 85549f4171b..554b0eb7277 100644 --- a/java/src/main/java/com/google/flatbuffers/IntVector.java +++ b/java/src/main/java/com/google/flatbuffers/IntVector.java @@ -34,9 +34,26 @@ public final class IntVector extends BaseVector { * `vector`. */ public IntVector __assign(int _vector, ByteBuffer _bb) { + if(_bb == null){ + throw new IllegalArgumentException("ByteBuffer cannot be null"); + } __reset(_vector, Constants.SIZEOF_INT, _bb); return this; } + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append("["); + for(int i = 0; i < length(); i++){ + if(i > 0){ + sb.append(", "); + } + sb.append(get(i)); + } + sb.append("]"); + return sb.toString(); + } + /** * Reads the integer at the given index. * @@ -44,6 +61,9 @@ public IntVector __assign(int _vector, ByteBuffer _bb) { * @return the 32-bit value at the given index. */ public int get(int j) { + if(j < 0 || j >= length()){ + throw new IndexOutOfBoundsException("Index " + j + " is out of bounds for length " + length()); + } return bb.getInt(__element(j)); } @@ -51,9 +71,12 @@ public int get(int j) { * Reads the integer at the given index, zero-extends it to type long, and returns the result, * which is therefore in the range 0 through 4294967295. * + * Java does not natively support unsigned integers, so this method returns a 'long' to represent the unsigned 32-bit value. + * * @param j The index from which the integer will be read. * @return the unsigned 32-bit at the given index. */ + public long getAsUnsigned(int j) { return (long) get(j) & 0xFFFFFFFFL; } From c49d29bb1a39da517a4347bb2aee0acf94fd8b28 Mon Sep 17 00:00:00 2001 From: souraOP Date: Fri, 28 Feb 2025 16:44:13 +0530 Subject: [PATCH 2/2] removed some changes in android project --- android/.project | 6 ------ 1 file changed, 6 deletions(-) diff --git a/android/.project b/android/.project index 6f27ae6074a..17f0659d4a1 100644 --- a/android/.project +++ b/android/.project @@ -5,14 +5,8 @@ - - org.eclipse.buildship.core.gradleprojectbuilder - - - - org.eclipse.buildship.core.gradleprojectnature