On 06/12/2017 01:13 PM, Lance Richardson wrote:
A left shift that would produce a result that is not representable
by the type of the expression's result has "undefined behavior"
according to the C language standard. Avoid this by casting values
that could set the upper bit to unsigned types.

Found via gcc's undefined behavior sanitizer.

Signed-off-by: Lance Richardson <lrich...@redhat.com>
---
  lib/byte-order.h | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/lib/byte-order.h b/lib/byte-order.h
index e864658..782439f 100644
--- a/lib/byte-order.h
+++ b/lib/byte-order.h
@@ -105,9 +105,9 @@ uint32_byteswap(uint32_t crc) {
      (OVS_FORCE ovs_be32)((uint32_t)(B1) << 16 | (B2))
  #else
  #define BYTES_TO_BE32(B1, B2, B3, B4) \
-    (OVS_FORCE ovs_be32)((uint32_t)(B1) | (B2) << 8 | (B3) << 16 | (B4) << 24)
+    (OVS_FORCE ovs_be32)((B1) | (B2) << 8 | (B3) << 16 | (uint32_t)(B4) << 24)

if B2 is a unsigned char then what is the value of this expression?
B2 << 8

Same here.  If B3 is an unsigned char what is the value of this expression?
B3 << 16

  #define BE16S_TO_BE32(B1, B2) \
-    (OVS_FORCE ovs_be32)((uint32_t)(B1) | (B2) << 16)
+    (OVS_FORCE ovs_be32)((B1) | (uint32_t)(B2) << 16)
  #endif

  /* These functions zero-extend big-endian values to longer ones,

I don't these macros.  There is no type checking so I think they could be 
improved.

I'd suggest turning them into inline functions so you get type checking, etc.

My $0.02$

Thanks,

- Greg
_______________________________________________
dev mailing list
d...@openvswitch.org
https://mail.openvswitch.org/mailman/listinfo/ovs-dev

Reply via email to