Skip to content

Commit 740afb0

Browse files
committed
Committing my patch from #IO-101, fixing an <int> overrun in readSwappedLong. Many thanks to Jose Pinto for finding this
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/io/trunk@482841 13f79535-47bb-0310-9956-ffa450edef68
1 parent a354d0a commit 740afb0

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

src/java/org/apache/commons/io/EndianUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,12 +218,12 @@ public static long readSwappedLong(byte[] data, int offset) {
218218
( ( data[ offset + 0 ] & 0xff ) << 0 ) +
219219
( ( data[ offset + 1 ] & 0xff ) << 8 ) +
220220
( ( data[ offset + 2 ] & 0xff ) << 16 ) +
221-
( ( data[ offset + 3 ] & 0xff ) << 24 );
221+
( ( (long) ( data[ offset + 3 ] & 0xff ) ) << 24 );
222222
long high =
223223
( ( data[ offset + 4 ] & 0xff ) << 0 ) +
224224
( ( data[ offset + 5 ] & 0xff ) << 8 ) +
225225
( ( data[ offset + 6 ] & 0xff ) << 16 ) +
226-
( ( data[ offset + 7 ] & 0xff ) << 24 );
226+
( ( (long) ( data[ offset + 7 ] & 0xff ) ) << 24 );
227227
return low + (high << 32);
228228
}
229229

src/test/org/apache/commons/io/EndianUtilsTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,4 +242,25 @@ public void testWriteSwappedDouble() throws IOException {
242242
assertEquals( 0x01, bytes[7] );
243243
}
244244

245+
// tests #IO-101
246+
public void testSymmetryOfLong() throws IOException {
247+
248+
double[] tests = new double[] {34.345, -345.5645, 545.12, 10.043, 7.123456789123};
249+
for (int i = 0; i< tests.length ;i++) {
250+
251+
// testing the real problem
252+
byte[] buffer = new byte[8];
253+
long ln1 = Double.doubleToLongBits( tests[i] );
254+
EndianUtils.writeSwappedLong(buffer, 0, ln1);
255+
long ln2 = EndianUtils.readSwappedLong(buffer, 0);
256+
assertEquals( ln1, ln2 );
257+
258+
// testing the bug report
259+
buffer = new byte[8];
260+
EndianUtils.writeSwappedDouble(buffer, 0, tests[i]);
261+
double val = EndianUtils.readSwappedDouble(buffer, 0);
262+
assertEquals( tests[i], val, 0 );
263+
}
264+
}
265+
245266
}

0 commit comments

Comments
 (0)