diff --git a/src/main/java/de/rub/nds/modifiablevariable/util/DataConverter.java b/src/main/java/de/rub/nds/modifiablevariable/util/DataConverter.java index 70af909f..db56b052 100644 --- a/src/main/java/de/rub/nds/modifiablevariable/util/DataConverter.java +++ b/src/main/java/de/rub/nds/modifiablevariable/util/DataConverter.java @@ -169,7 +169,7 @@ public static byte[] intToBytes(int value, int size) { int shift = 0; int finalPosition = size > Integer.BYTES ? size - Integer.BYTES : 0; for (int i = size - 1; i >= finalPosition; i--) { - result[i] = (byte) (value >>> shift); + result[i] = (byte) ((value >>> shift) & 0xFF); shift += 8; } @@ -192,7 +192,7 @@ public static byte[] longToBytes(long value, int size) { int shift = 0; int finalPosition = size > Long.BYTES ? size - Long.BYTES : 0; for (int i = size - 1; i >= finalPosition; i--) { - result[i] = (byte) (value >>> shift); + result[i] = (byte) ((value >>> shift) & 0xFF); shift += 8; } diff --git a/src/test/java/de/rub/nds/modifiablevariable/util/DataConverterTest.java b/src/test/java/de/rub/nds/modifiablevariable/util/DataConverterTest.java index 727e8c94..545d20ea 100644 --- a/src/test/java/de/rub/nds/modifiablevariable/util/DataConverterTest.java +++ b/src/test/java/de/rub/nds/modifiablevariable/util/DataConverterTest.java @@ -1173,6 +1173,77 @@ void testIntegerReconversion() { } } + @Test + void testIntToBytesWithNegativeValues() { + // Test negative int values to ensure unsigned right shift is handled correctly + int negativeValue = -1; + byte[] result = DataConverter.intToBytes(negativeValue, 4); + assertArrayEquals( + new byte[] {(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF}, + result, + "Negative -1 should produce all FF bytes"); + + // Test another negative value + negativeValue = -256; // 0xFFFFFF00 + result = DataConverter.intToBytes(negativeValue, 4); + assertArrayEquals( + new byte[] {(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0x00}, + result, + "Negative -256 should produce correct bytes"); + + // Test with size larger than int + negativeValue = -1; + result = DataConverter.intToBytes(negativeValue, 6); + assertArrayEquals( + new byte[] {0x00, 0x00, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF}, + result, + "Negative -1 with size 6 should be padded with zeros"); + } + + @Test + void testLongToBytesWithNegativeValues() { + // Test negative long values to ensure unsigned right shift is handled correctly + long negativeValue = -1L; + byte[] result = DataConverter.longToBytes(negativeValue, 8); + assertArrayEquals( + new byte[] { + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF + }, + result, + "Negative -1L should produce all FF bytes"); + + // Test another negative value + negativeValue = -256L; // 0xFFFFFFFFFFFFFF00 + result = DataConverter.longToBytes(negativeValue, 8); + assertArrayEquals( + new byte[] { + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, + (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0x00 + }, + result, + "Negative -256L should produce correct bytes"); + + // Test with size larger than long + negativeValue = -1L; + result = DataConverter.longToBytes(negativeValue, 10); + assertArrayEquals( + new byte[] { + 0x00, + 0x00, + (byte) 0xFF, + (byte) 0xFF, + (byte) 0xFF, + (byte) 0xFF, + (byte) 0xFF, + (byte) 0xFF, + (byte) 0xFF, + (byte) 0xFF + }, + result, + "Negative -1L with size 10 should be padded with zeros"); + } + /** Test of indexOf method, of class DataConverter. */ @Test void testIndexOf() {