[esnacc-dev] [PATCH 2/2] c-lib/asn-int: Shift with defined behavior

Aaron Conole aconole at bytheb.org
Thu Sep 8 16:34:36 UTC 2016


Left-shifting with a negative value is not well-defined in C.  This means
that any compiler is free to implement the consequences of that shift in
whatever manner it sees fit, including doing something wrong.

Since we really just want to set the upper bits to indicate the value is
negative, we instead start with a bitflipped 0.

Suggested-by: Arpad Tigyi <tigyi.arpad at gmail.com>
Signed-off-by: Aaron Conole <aconole at bytheb.org>
---
 c-lib/src/asn-int.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/c-lib/src/asn-int.c b/c-lib/src/asn-int.c
index 171e925..63e5abf 100644
--- a/c-lib/src/asn-int.c
+++ b/c-lib/src/asn-int.c
@@ -206,7 +206,7 @@ BDecAsnIntContent PARAMS ((b, tagId, len, result, bytesDecoded, env),
     byte = (unsigned long ) BufGetByte (b);
 
     if (byte & 0x80)   /* top bit of first byte is sign bit */
-        retVal = (-1 << 8) | byte;
+        retVal = (~0U << 8) | byte;
     else
         retVal = byte;
 
-- 
2.7.4




More information about the dev mailing list