Skip to content

Commit

Permalink
check tuple length in annotate;
Browse files Browse the repository at this point in the history
refactor annotate;
  • Loading branch information
esaulpaugh committed Feb 10, 2024
1 parent f9bc5a5 commit 7f8d5a0
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions src/main/java/com/esaulpaugh/headlong/abi/TupleType.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,11 @@ public int validate(final Tuple value) {
if (value.size() == this.size()) {
return countBytes(i -> validateObject(get(i), value.elements[i]));
}
throw new IllegalArgumentException("tuple length mismatch: expected length " + this.size() + " but found " + value.size());
throw lengthMismatch(value);
}

private IllegalArgumentException lengthMismatch(Tuple args) {
throw new IllegalArgumentException("tuple length mismatch: expected length " + this.size() + " but found " + args.size());
}

private static int validateObject(ABIType<Object> type, Object value) {
Expand Down Expand Up @@ -443,16 +447,19 @@ public String annotate(byte[] abi) {
* This method is subject to change or removal in a future release.
*/
public String annotate(Tuple tuple) {
if (tuple.elements.length == 0) {
if (size() != tuple.elements.length) {
throw lengthMismatch(tuple);
}
if (size() == 0) {
return "";
}
int row = 0;
final StringBuilder sb = new StringBuilder();
int i = 0;
final int last = tuple.elements.length - 1;
final int last = size() - 1;
int offset = firstOffset;
final byte[] rowBuffer = newUnitBuffer();
for (;; i++) {
do {
final ABIType<Object> t = get(i);
if (!t.dynamic) {
row = encodeTailAnnotated(sb, row, i, tuple.elements[i]);
Expand All @@ -466,13 +473,15 @@ public String annotate(Tuple tuple) {
}
offset += t.dynamicByteLength(tuple.elements[i]); // calculate next offset
}
}
for (i = 0; i < tuple.elements.length; i++) {
i++;
} while (true);
i = 0;
do {
final ABIType<Object> t = get(i);
if (t.dynamic) {
row = encodeTailAnnotated(sb, row, i, tuple.elements[i]);
}
}
} while (++i < size());
return sb.toString();
}

Expand Down

0 comments on commit 7f8d5a0

Please sign in to comment.