lib:util: Fix undefined behavior in asn1 parser
authorAndreas Schneider <asn@samba.org>
Thu, 22 Nov 2018 13:45:20 +0000 (14:45 +0100)
committerGary Lockyer <gary@samba.org>
Thu, 22 Nov 2018 21:13:27 +0000 (22:13 +0100)
lib/util/asn1.c:969 runtime error: left shift of negative value -1

Signed-off-by: Andreas Schneider <asn@samba.org>
Reviewed-by: Gary Lockyer <gary@catalyst.net.nz>
lib/util/asn1.c

index d3b46aac857155f729d542d8ff8bef57a8ead97b..60ddfa09bcf439cec707ad915feb0e1ff2de4b28 100644 (file)
@@ -953,21 +953,24 @@ bool asn1_read_ContextSimple(struct asn1_data *data, TALLOC_CTX *mem_ctx, uint8_
 bool asn1_read_implicit_Integer(struct asn1_data *data, int *i)
 {
        uint8_t b;
+       uint32_t x = 0;
        bool first_byte = true;
+
        *i = 0;
 
        while (!data->has_error && asn1_tag_remaining(data)>0) {
                if (!asn1_read_uint8(data, &b)) return false;
                if (first_byte) {
                        if (b & 0x80) {
-                               /* Number is negative.
-                                  Set i to -1 for sign extend. */
-                               *i = -1;
+                               /* Number is negative. */
+                               x = (uint32_t)-1;
                        }
                        first_byte = false;
                }
-               *i = (*i << 8) + b;
+               x = (x << 8) + b;
        }
+       *i = (int)x;
+
        return !data->has_error;
 }