You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@iotdb.apache.org by GitBox <gi...@apache.org> on 2022/05/12 11:06:14 UTC

[GitHub] [iotdb] MinaQin opened a new issue, #5882: Prevent "int" promotion by adding "& 0xff" to this expression.

MinaQin opened a new issue, #5882:
URL: https://github.com/apache/iotdb/issues/5882

   Code Path: consensus/src/main/java/org/apache/iotdb/consensus/ratis/Utils.java
   <img width="838" alt="image" src="https://user-images.githubusercontent.com/55030443/168056920-d68efb6d-09fa-4a60-9e7c-92e80b8bab78.png">
   When reading bytes in order to build other primitive values such as ints or longs, the byte values are automatically promoted, but that promotion can have unexpected results.
   
   For instance, the binary representation of the integer 640 is 0b0000_0010_1000_0000, which can also be written with the array of (unsigned) bytes [2, 128]. However, since Java uses two’s complement, the representation of the integer in signed bytes will be [2, -128] (because the byte 0b1000_0000 is promoted to the int 0b1111_1111_1111_1111_1111_1111_1000_0000). Consequently, trying to reconstruct the initial integer by shifting and adding the values of the bytes without taking care of the sign will not produce the expected result.
   
   To prevent such accidental value conversion, use bitwise and (&) to combine the byte value with 0xff (255) and turn all the higher bits back off.
   
   This rule raises an issue any time a byte value is used as an operand without & 0xff, when combined with shifts.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@iotdb.apache.org.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [iotdb] HTHou closed issue #5882: Prevent "int" promotion by adding "& 0xff" to this expression.

Posted by GitBox <gi...@apache.org>.
HTHou closed issue #5882: Prevent "int" promotion by adding "& 0xff" to this expression.
URL: https://github.com/apache/iotdb/issues/5882


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@iotdb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org