Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

In Java, data is stored in big-endian format (also called network order). That is, all data is represented sequentially starting from the most significant bit to the least significant. JDK versions prior to JDK 1.4 required definition of custom methods that manage reversing byte order to maintain compatibility with little-endian systems. Correct handling of byte order-related issues is critical when exchanging data in a networked environment that includes both big-endian and little-endian machines or when working with other languages using JNI. Failure to handle byte-ordering issues can cause unexpected program behavior.

...

This compliant solution uses methods provided by class ByteBuffer (see [API 2006] ByteBuffer) to correctly extract an int from the original input value. It wraps the input byte array with a ByteBuffer, sets the byte order to little-endian, and extracts the int. The result is stored in the integer serialNumber. Class ByteBuffer provides analogous get and put methods for other numeric types.

...

MITRE CWE

CWE-198. Use of incorrect byte ordering

Bibliography

[API 2006]

Class ByteBuffer: Methods wrap and order. Class Integer: method reverseBytes

[Cohen 1981]

On Holy Wars and a Plea for Peace

[Harold 1997]

Chapter 2, Primitive Data Types, Cross-Platform Issues

...

      Rule 13: Input Output (FIO)Image Added