This is an automated email from the ASF dual-hosted git repository.

toulmean pushed a commit to branch 2.3
in repository https://gitbox.apache.org/repos/asf/incubator-tuweni.git

commit 67b92a4afde9bf2cba60689facd18aeb0d0cf38e
Author: Antoine Toulme <anto...@lunar-ocean.com>
AuthorDate: Sat Nov 19 21:58:13 2022 -0800

    fix zero compares of different lengths
---
 bytes/src/main/java/org/apache/tuweni/bytes/Bytes.java     | 7 ++++++-
 bytes/src/test/java/org/apache/tuweni/bytes/BytesTest.java | 4 ++++
 2 files changed, 10 insertions(+), 1 deletion(-)

diff --git a/bytes/src/main/java/org/apache/tuweni/bytes/Bytes.java 
b/bytes/src/main/java/org/apache/tuweni/bytes/Bytes.java
index 50291371..668abad8 100644
--- a/bytes/src/main/java/org/apache/tuweni/bytes/Bytes.java
+++ b/bytes/src/main/java/org/apache/tuweni/bytes/Bytes.java
@@ -1687,10 +1687,15 @@ public interface Bytes extends Comparable<Bytes> {
   default int compareTo(Bytes b) {
     checkNotNull(b);
 
-    int sizeCmp = Integer.compare(bitLength(), b.bitLength());
+    int bitLength = bitLength();
+    int sizeCmp = Integer.compare(bitLength, b.bitLength());
     if (sizeCmp != 0) {
       return sizeCmp;
     }
+    // same bitlength and is zeroes only, return 0.
+    if (bitLength == 0) {
+      return 0;
+    }
 
     for (int i = 0; i < size(); i++) {
       int cmp = Integer.compare(get(i) & 0xff, b.get(i) & 0xff);
diff --git a/bytes/src/test/java/org/apache/tuweni/bytes/BytesTest.java 
b/bytes/src/test/java/org/apache/tuweni/bytes/BytesTest.java
index 35507ef9..8a293cf0 100644
--- a/bytes/src/test/java/org/apache/tuweni/bytes/BytesTest.java
+++ b/bytes/src/test/java/org/apache/tuweni/bytes/BytesTest.java
@@ -309,6 +309,10 @@ class BytesTest extends CommonBytesTests {
     assertEquals(-1, Bytes.of(0x01).compareTo(Bytes.of(0x01, 0xff)));
     assertEquals(-1, Bytes.of(0x00, 0x00, 0x01).compareTo(Bytes.of(0x00, 
0x02)));
     assertEquals(-1, Bytes.of(0x00, 0x01).compareTo(Bytes.of(0x00, 0x00, 
0x05)));
+    assertEquals(0, 
Bytes.fromHexString("0x0000").compareTo(Bytes.fromHexString("0x00")));
+    assertEquals(0, 
Bytes.fromHexString("0x00").compareTo(Bytes.fromHexString("0x0000")));
+    assertEquals(0, 
Bytes.fromHexString("0x000000").compareTo(Bytes.fromHexString("0x000000")));
+    assertEquals(-1, 
Bytes.fromHexString("0x000001").compareTo(Bytes.fromHexString("0x0001")));
   }
 
   @Test


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@tuweni.apache.org
For additional commands, e-mail: commits-h...@tuweni.apache.org

Reply via email to