<div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div>Merged in master and stable-2.0.</div><div>The first patch (the fix) was back-ported to stable-1.5.<br></div><div><br></div><div>Anyone interested in the discussion that lead to v8, see the following gerrit changes:<br></div><div><a href="https://review.lttng.org/c/babeltrace/+/1311" target="_blank">https://review.lttng.org/c/babeltrace/+/1311</a></div><div><a href="https://review.lttng.org/c/babeltrace/+/1305/4" target="_blank">https://review.lttng.org/c/babeltrace/+/1305/4</a></div><div><a href="https://review.lttng.org/c/babeltrace/+/1307/4" target="_blank">https://review.lttng.org/c/babeltrace/+/1307/4</a></div><div><a href="https://review.lttng.org/c/babeltrace/+/1318/1" target="_blank">https://review.lttng.org/c/babeltrace/+/1318/1</a></div><div><br></div><div>Thanks!</div><div>Jérémie<br></div><div><br></div><div><br></div><div><br></div></div></div></div></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Thu, 16 May 2019 at 16:58, Mathieu Desnoyers <<a href="mailto:mathieu.desnoyers@efficios.com" target="_blank">mathieu.desnoyers@efficios.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">bitfield.h uses the left shift operator with a left operand which<br>
may be negative. The C99 standard states that shifting a negative<br>
value is undefined.<br>
<br>
When building with -Wshift-negative-value, we get this gcc warning:<br>
<br>
In file included from /home/smarchi/src/babeltrace/include/babeltrace/ctfser-internal.h:44:0,<br>
                 from /home/smarchi/src/babeltrace/ctfser/ctfser.c:42:<br>
/home/smarchi/src/babeltrace/include/babeltrace/ctfser-internal.h: In function ‘bt_ctfser_write_unsigned_int’:<br>
/home/smarchi/src/babeltrace/include/babeltrace/bitfield-internal.h:116:24: error: left shift of negative value [-Werror=shift-negative-value]<br>
   mask = ~((~(type) 0) << (__start % ts));  \<br>
                        ^<br>
/home/smarchi/src/babeltrace/include/babeltrace/bitfield-internal.h:222:2: note: in expansion of macro ‘_bt_bitfield_write_le’<br>
  _bt_bitfield_write_le(ptr, type, _start, _length, _v)<br>
  ^~~~~~~~~~~~~~~~~~~~~<br>
/home/smarchi/src/babeltrace/include/babeltrace/ctfser-internal.h:418:3: note: in expansion of macro ‘bt_bitfield_write_le’<br>
   bt_bitfield_write_le(mmap_align_addr(ctfser->base_mma) +<br>
   ^~~~~~~~~~~~~~~~~~~~<br>
<br>
This boils down to the fact that the expression ~((uint8_t)0) has type<br>
"signed int", which is used as an operand of the left shift.  This is due<br>
to the integer promotion rules of C99 (6.3.3.1):<br>
<br>
    If an int can represent all values of the original type, the value is<br>
    converted to an int; otherwise, it is converted to an unsigned int.<br>
    These are called the integer promotions. All other types are unchanged<br>
    by the integer promotions.<br>
<br>
We also need to cast the result explicitly into the left hand<br>
side type to deal with:<br>
<br>
warning: large integer implicitly truncated to unsigned type [-Woverflow]<br>
<br>
The C99 standard states that a right shift has implementation-defined<br>
behavior when shifting a signed negative value. Add a preprocessor check<br>
that the compiler provides the expected behavior, else provide an<br>
alternative implementation which guarantees the intended behavior.<br>
<br>
A preprocessor check is also added to ensure that the compiler<br>
representation for signed values is two's complement, which is expected<br>
by this header.<br>
<br>
Document that this header strictly respects the C99 standard, with<br>
the exception of its use of __typeof__.<br>
<br>
Adapt use of _bt_piecewise_lshift in plugins/text/pretty/print.c<br>
to the new API.<br>
<br>
Signed-off-by: Mathieu Desnoyers <<a href="mailto:mathieu.desnoyers@efficios.com" target="_blank">mathieu.desnoyers@efficios.com</a>><br>
Acked-by: Simon Marchi <<a href="mailto:simon.marchi@efficios.com" target="_blank">simon.marchi@efficios.com</a>><br>
Change-Id: I47d2655cfe671baf0fc18813883adf7ce11dfd6a<br>
---<br>
Changes since v1:<br>
- Generate compile-time error if the type argument passed to<br>
  _bt_unsigned_cast() is larger than sizeof(uint64_t), this<br>
  allows removing _bt_check_max_64bit,<br>
- Introduce _br_fill_mask, which replaces _bt_bitwise_not,<br>
- Clarify _bt_unsigned_cast comment,<br>
- Expand explanation of the issue within the patch commit message.<br>
<br>
Changes since v2:<br>
- Fix unwanted sign extension when generating masks,<br>
- Introduce macro helpers to clarify code:<br>
  - _bt_cast_value_to_unsigned()<br>
  - _bt_cast_value_to_unsigned_type(),<br>
  - _bt_make_mask_complement(),<br>
  - _bt_make_mask().<br>
<br>
Changes since v3:<br>
- Fix additional left shift undefined behavior. Identified with<br>
  -fsanitize=undefined.<br>
- Create fallback for right shift implementation-defined behavior<br>
  if the behavior is found not to be as intended. Detect the behavior<br>
  with a preprocessor check.<br>
- Ensure that the compiler represents signed types with two's complement<br>
  with a preprocessor check.<br>
- Add _bt_rshift() and _bt_lshift() shift helpers to take care of<br>
  C99's undefined behaviors related to shifting signed types.<br>
- Remove statement-expressions to allow building with -std=c99.<br>
- Use __typeof__ instead of typeof to allow building with -std=c99.<br>
  Strictly speaking, the use of __typeof__ is specific to the compiler.<br>
  Therefore, porting to a strict ansi C compiler would require removing<br>
  those __typeof__.<br>
- Remove use of gnu extension: replace (a ? : b) expressions by (a ? a :<br>
  b) to please compiling with -std=c99 -Wpedantic.<br>
<br>
Changes since v4:<br>
- Document strict C89 compliance, except for use of __typeof__<br>
  compiler-specific extension.<br>
- Adapt use of _bt_piecewise_lshift in plugins/text/pretty/print.c<br>
  to the new API.<br>
<br>
Changes since v5:<br>
- Bump standard compliance from C89 to C99, because we use<br>
  C99 types.h.<br>
<br>
Changes since v6:<br>
- Fix wrong use of _bt_make_mask() in _bt_bitfield_write_be. Should<br>
  be _bt_make_mask_complement. Caught by testing on powerpc.<br>
<br>
Changes since v7:<br>
- Style fixes.<br>
---<br>
 include/babeltrace/bitfield-internal.h | 312 ++++++++++++++++++++++-----------<br>
 plugins/text/pretty/print.c            |   4 +-<br>
 2 files changed, 208 insertions(+), 108 deletions(-)<br>
<br>
diff --git a/include/babeltrace/bitfield-internal.h b/include/babeltrace/bitfield-internal.h<br>
index c5d5eccd..5ecde05e 100644<br>
--- a/include/babeltrace/bitfield-internal.h<br>
+++ b/include/babeltrace/bitfield-internal.h<br>
@@ -2,7 +2,7 @@<br>
 #define _BABELTRACE_BITFIELD_H<br>
<br>
 /*<br>
- * Copyright 2010 - Mathieu Desnoyers <<a href="mailto:mathieu.desnoyers@efficios.com" target="_blank">mathieu.desnoyers@efficios.com</a>><br>
+ * Copyright 2010-2019 - Mathieu Desnoyers <<a href="mailto:mathieu.desnoyers@efficios.com" target="_blank">mathieu.desnoyers@efficios.com</a>><br>
  *<br>
  * Permission is hereby granted, free of charge, to any person obtaining a copy<br>
  * of this software and associated documentation files (the "Software"), to deal<br>
@@ -27,49 +27,157 @@<br>
 #include <babeltrace/compat/limits-internal.h> /* C99 5.2.4.2 Numerical limits */<br>
 #include <babeltrace/endian-internal.h>        /* Non-standard BIG_ENDIAN, LITTLE_ENDIAN, BYTE_ORDER */<br>
<br>
-/* We can't shift a int from 32 bit, >> 32 and << 32 on int is undefined */<br>
-#define _bt_piecewise_rshift(_v, _shift)                               \<br>
-({                                                                     \<br>
-       typeof(_v) ___v = (_v);                                         \<br>
-       typeof(_shift) ___shift = (_shift);                             \<br>
-       unsigned long sb = (___shift) / (sizeof(___v) * CHAR_BIT - 1);  \<br>
-       unsigned long final = (___shift) % (sizeof(___v) * CHAR_BIT - 1); \<br>
-                                                                       \<br>
-       for (; sb; sb--)                                                \<br>
-               ___v >>= sizeof(___v) * CHAR_BIT - 1;                   \<br>
-       ___v >>= final;                                                 \<br>
-})<br>
-<br>
-#define _bt_piecewise_lshift(_v, _shift)                               \<br>
-({                                                                     \<br>
-       typeof(_v) ___v = (_v);                                         \<br>
-       typeof(_shift) ___shift = (_shift);                             \<br>
-       unsigned long sb = (___shift) / (sizeof(___v) * CHAR_BIT - 1);  \<br>
-       unsigned long final = (___shift) % (sizeof(___v) * CHAR_BIT - 1); \<br>
-                                                                       \<br>
-       for (; sb; sb--)                                                \<br>
-               ___v <<= sizeof(___v) * CHAR_BIT - 1;                   \<br>
-       ___v <<= final;                                                 \<br>
-})<br>
+/*<br>
+ * This header strictly follows the C99 standard, except for use of the<br>
+ * compiler-specific __typeof__.<br>
+ */<br>
+<br>
+/*<br>
+ * This bitfield header requires the compiler representation of signed<br>
+ * integers to be two's complement.<br>
+ */<br>
+#if (-1 != ~0)<br>
+#error "bitfield.h requires the compiler representation of signed integers to be two's complement."<br>
+#endif<br>
<br>
 #define _bt_is_signed_type(type)       ((type) -1 < (type) 0)<br>
<br>
 /*<br>
- * NOTE: The cast to (uint64_t) below ensures that we're not casting a<br>
- * negative value, which is undefined in C. However, this limits the<br>
- * maximum type size of `type` and `v` to 64-bit. The<br>
- * _bt_check_max_64bit() is used to check that the users of this header<br>
- * do not use types with a size greater than 64-bit.<br>
+ * Produce a build-time error if the condition `cond` is non-zero.<br>
+ * Evaluates as a size_t expression.<br>
+ */<br>
+#define _BT_BUILD_ASSERT(cond)                                 \<br>
+       sizeof(struct { int f:(2 * !!(cond) - 1); })<br>
+<br>
+/*<br>
+ * Cast value `v` to an unsigned integer of the same size as `v`.<br>
+ */<br>
+#define _bt_cast_value_to_unsigned(v)                                  \<br>
+       (sizeof(v) == sizeof(uint8_t) ? (uint8_t) (v) :                 \<br>
+       sizeof(v) == sizeof(uint16_t) ? (uint16_t) (v) :                \<br>
+       sizeof(v) == sizeof(uint32_t) ? (uint32_t) (v) :                \<br>
+       sizeof(v) == sizeof(uint64_t) ? (uint64_t) (v) :                \<br>
+       _BT_BUILD_ASSERT(sizeof(v) <= sizeof(uint64_t)))<br>
+<br>
+/*<br>
+ * Cast value `v` to an unsigned integer type of the size of type `type`<br>
+ * *without* sign-extension.<br>
+ *<br>
+ * The unsigned cast ensures that we're not shifting a negative value,<br>
+ * which is undefined in C. However, this limits the maximum type size<br>
+ * of `type` to 64-bit. Generate a compile-time error if the size of<br>
+ * `type` is larger than 64-bit.<br>
+ */<br>
+#define _bt_cast_value_to_unsigned_type(type, v)                       \<br>
+       (sizeof(type) == sizeof(uint8_t) ?                              \<br>
+               (uint8_t) _bt_cast_value_to_unsigned(v) :               \<br>
+       sizeof(type) == sizeof(uint16_t) ?                              \<br>
+               (uint16_t) _bt_cast_value_to_unsigned(v) :              \<br>
+       sizeof(type) == sizeof(uint32_t) ?                              \<br>
+               (uint32_t) _bt_cast_value_to_unsigned(v) :              \<br>
+       sizeof(type) == sizeof(uint64_t) ?                              \<br>
+               (uint64_t) _bt_cast_value_to_unsigned(v) :              \<br>
+       _BT_BUILD_ASSERT(sizeof(v) <= sizeof(uint64_t)))<br>
+<br>
+/*<br>
+ * _bt_fill_mask evaluates to a "type" integer with all bits set.<br>
+ */<br>
+#define _bt_fill_mask(type)    ((type) ~(type) 0)<br>
+<br>
+/*<br>
+ * Left shift a value `v` of `shift` bits.<br>
+ *<br>
+ * The type of `v` can be signed or unsigned integer.<br>
+ * The value of `shift` must be less than the size of `v` (in bits),<br>
+ * otherwise the behavior is undefined.<br>
+ * Evaluates to the result of the shift operation.<br>
+ *<br>
+ * According to the C99 standard, left shift of a left hand-side signed<br>
+ * type is undefined if it has a negative value or if the result cannot<br>
+ * be represented in the result type. This bitfield header discards the<br>
+ * bits that are left-shifted beyond the result type representation,<br>
+ * which is the behavior of an unsigned type left shift operation.<br>
+ * Therefore, always perform left shift on an unsigned type.<br>
+ *<br>
+ * This macro should not be used if `shift` can be greater or equal than<br>
+ * the bitwidth of `v`. See `_bt_safe_lshift`.<br>
+ */<br>
+#define _bt_lshift(v, shift)                                           \<br>
+       ((__typeof__(v)) (_bt_cast_value_to_unsigned(v) << (shift)))<br>
+<br>
+/*<br>
+ * Generate a mask of type `type` with the `length` least significant bits<br>
+ * cleared, and the most significant bits set.<br>
  */<br>
-#define _bt_unsigned_cast(type, v)                                     \<br>
-({                                                                     \<br>
-       (sizeof(v) < sizeof(type)) ?                                    \<br>
-               ((type) (v)) & ((type) (~(~(uint64_t) 0 << (sizeof(v) * CHAR_BIT)))) : \<br>
-               (type) (v);                                             \<br>
-})<br>
+#define _bt_make_mask_complement(type, length)                         \<br>
+       _bt_lshift(_bt_fill_mask(type), length)<br>
<br>
-#define _bt_check_max_64bit(type)                                      \<br>
-       char _max_64bit_assertion[sizeof(type) <= sizeof(uint64_t) ? 1 : -1] __attribute__((unused))<br>
+/*<br>
+ * Generate a mask of type `type` with the `length` least significant bits<br>
+ * set, and the most significant bits cleared.<br>
+ */<br>
+#define _bt_make_mask(type, length)                                    \<br>
+       ((type) ~_bt_make_mask_complement(type, length))<br>
+<br>
+/*<br>
+ * Right shift a value `v` of `shift` bits.<br>
+ *<br>
+ * The type of `v` can be signed or unsigned integer.<br>
+ * The value of `shift` must be less than the size of `v` (in bits),<br>
+ * otherwise the behavior is undefined.<br>
+ * Evaluates to the result of the shift operation.<br>
+ *<br>
+ * According to the C99 standard, right shift of a left hand-side signed<br>
+ * type which has a negative value is implementation defined. This<br>
+ * bitfield header relies on the right shift implementation carrying the<br>
+ * sign bit. If the compiler implementation has a different behavior,<br>
+ * emulate carrying the sign bit.<br>
+ *<br>
+ * This macro should not be used if `shift` can be greater or equal than<br>
+ * the bitwidth of `v`. See `_bt_safe_rshift`.<br>
+ */<br>
+#if ((-1 >> 1) == -1)<br>
+#define _bt_rshift(v, shift)   ((v) >> (shift))<br>
+#else<br>
+#define _bt_rshift(v, shift)                                           \<br>
+       ((__typeof__(v)) ((_bt_cast_value_to_unsigned(v) >> (shift)) |  \<br>
+               ((v) < 0 ? _bt_make_mask_complement(__typeof__(v),      \<br>
+                       sizeof(v) * CHAR_BIT - (shift)) : 0)))<br>
+#endif<br>
+<br>
+/*<br>
+ * Right shift a signed or unsigned integer with `shift` value being an<br>
+ * arbitrary number of bits. `v` is modified by this macro. The shift<br>
+ * is transformed into a sequence of `_nr_partial_shifts` consecutive<br>
+ * shift operations, each of a number of bits smaller than the bitwidth<br>
+ * of `v`, ending with a shift of the number of left over bits.<br>
+ */<br>
+#define _bt_safe_rshift(v, shift)                                      \<br>
+do {                                                                   \<br>
+       unsigned long _nr_partial_shifts = (shift) / (sizeof(v) * CHAR_BIT - 1); \<br>
+       unsigned long _leftover_bits = (shift) % (sizeof(v) * CHAR_BIT - 1); \<br>
+                                                                       \<br>
+       for (; _nr_partial_shifts; _nr_partial_shifts--)                \<br>
+               (v) = _bt_rshift(v, sizeof(v) * CHAR_BIT - 1);          \<br>
+       (v) = _bt_rshift(v, _leftover_bits);                            \<br>
+} while (0)<br>
+<br>
+/*<br>
+ * Left shift a signed or unsigned integer with `shift` value being an<br>
+ * arbitrary number of bits. `v` is modified by this macro. The shift<br>
+ * is transformed into a sequence of `_nr_partial_shifts` consecutive<br>
+ * shift operations, each of a number of bits smaller than the bitwidth<br>
+ * of `v`, ending with a shift of the number of left over bits.<br>
+ */<br>
+#define _bt_safe_lshift(v, shift)                                      \<br>
+do {                                                                   \<br>
+       unsigned long _nr_partial_shifts = (shift) / (sizeof(v) * CHAR_BIT - 1); \<br>
+       unsigned long _leftover_bits = (shift) % (sizeof(v) * CHAR_BIT - 1); \<br>
+                                                                       \<br>
+       for (; _nr_partial_shifts; _nr_partial_shifts--)                \<br>
+               (v) = _bt_lshift(v, sizeof(v) * CHAR_BIT - 1);          \<br>
+       (v) = _bt_lshift(v, _leftover_bits);                            \<br>
+} while (0)<br>
<br>
 /*<br>
  * bt_bitfield_write - write integer to a bitfield in native endianness<br>
@@ -91,7 +199,7 @@<br>
<br>
 #define _bt_bitfield_write_le(_ptr, type, _start, _length, _v)         \<br>
 do {                                                                   \<br>
-       typeof(_v) __v = (_v);                                          \<br>
+       __typeof__(_v) __v = (_v);                                      \<br>
        type *__ptr = (void *) (_ptr);                                  \<br>
        unsigned long __start = (_start), __length = (_length);         \<br>
        type mask, cmask;                                               \<br>
@@ -108,15 +216,15 @@ do {                                                                      \<br>
                                                                        \<br>
        /* Trim v high bits */                                          \<br>
        if (__length < sizeof(__v) * CHAR_BIT)                          \<br>
-               __v &= ~((~(typeof(__v)) 0) << __length);               \<br>
+               __v &= _bt_make_mask(__typeof__(__v), __length);        \<br>
                                                                        \<br>
        /* We can now append v with a simple "or", shift it piece-wise */ \<br>
        this_unit = start_unit;                                         \<br>
        if (start_unit == end_unit - 1) {                               \<br>
-               mask = ~((~(type) 0) << (__start % ts));                \<br>
+               mask = _bt_make_mask(type, __start % ts);               \<br>
                if (end % ts)                                           \<br>
-                       mask |= (~(type) 0) << (end % ts);              \<br>
-               cmask = (type) __v << (__start % ts);                   \<br>
+                       mask |= _bt_make_mask_complement(type, end % ts); \<br>
+               cmask = _bt_lshift((type) (__v), __start % ts);         \<br>
                cmask &= ~mask;                                         \<br>
                __ptr[this_unit] &= mask;                               \<br>
                __ptr[this_unit] |= cmask;                              \<br>
@@ -124,22 +232,22 @@ do {                                                                      \<br>
        }                                                               \<br>
        if (__start % ts) {                                             \<br>
                cshift = __start % ts;                                  \<br>
-               mask = ~((~(type) 0) << cshift);                        \<br>
-               cmask = (type) __v << cshift;                           \<br>
+               mask = _bt_make_mask(type, cshift);                     \<br>
+               cmask = _bt_lshift((type) (__v), cshift);               \<br>
                cmask &= ~mask;                                         \<br>
                __ptr[this_unit] &= mask;                               \<br>
                __ptr[this_unit] |= cmask;                              \<br>
-               __v = _bt_piecewise_rshift(__v, ts - cshift);           \<br>
+               _bt_safe_rshift(__v, ts - cshift);                      \<br>
                __start += ts - cshift;                                 \<br>
                this_unit++;                                            \<br>
        }                                                               \<br>
        for (; this_unit < end_unit - 1; this_unit++) {                 \<br>
                __ptr[this_unit] = (type) __v;                          \<br>
-               __v = _bt_piecewise_rshift(__v, ts);                    \<br>
+               _bt_safe_rshift(__v, ts);                               \<br>
                __start += ts;                                          \<br>
        }                                                               \<br>
        if (end % ts) {                                                 \<br>
-               mask = (~(type) 0) << (end % ts);                       \<br>
+               mask = _bt_make_mask_complement(type, end % ts);        \<br>
                cmask = (type) __v;                                     \<br>
                cmask &= ~mask;                                         \<br>
                __ptr[this_unit] &= mask;                               \<br>
@@ -150,7 +258,7 @@ do {                                                                        \<br>
<br>
 #define _bt_bitfield_write_be(_ptr, type, _start, _length, _v)         \<br>
 do {                                                                   \<br>
-       typeof(_v) __v = (_v);                                          \<br>
+       __typeof__(_v) __v = (_v);                                      \<br>
        type *__ptr = (void *) (_ptr);                                  \<br>
        unsigned long __start = (_start), __length = (_length);         \<br>
        type mask, cmask;                                               \<br>
@@ -167,15 +275,15 @@ do {                                                                      \<br>
                                                                        \<br>
        /* Trim v high bits */                                          \<br>
        if (__length < sizeof(__v) * CHAR_BIT)                          \<br>
-               __v &= ~((~(typeof(__v)) 0) << __length);               \<br>
+               __v &= _bt_make_mask(__typeof__(__v), __length);        \<br>
                                                                        \<br>
        /* We can now append v with a simple "or", shift it piece-wise */ \<br>
        this_unit = end_unit - 1;                                       \<br>
        if (start_unit == end_unit - 1) {                               \<br>
-               mask = ~((~(type) 0) << ((ts - (end % ts)) % ts));      \<br>
+               mask = _bt_make_mask(type, (ts - (end % ts)) % ts);     \<br>
                if (__start % ts)                                       \<br>
-                       mask |= (~((type) 0)) << (ts - (__start % ts)); \<br>
-               cmask = (type) __v << ((ts - (end % ts)) % ts);         \<br>
+                       mask |= _bt_make_mask_complement(type, ts - (__start % ts)); \<br>
+               cmask = _bt_lshift((type) (__v), (ts - (end % ts)) % ts); \<br>
                cmask &= ~mask;                                         \<br>
                __ptr[this_unit] &= mask;                               \<br>
                __ptr[this_unit] |= cmask;                              \<br>
@@ -183,22 +291,22 @@ do {                                                                      \<br>
        }                                                               \<br>
        if (end % ts) {                                                 \<br>
                cshift = end % ts;                                      \<br>
-               mask = ~((~(type) 0) << (ts - cshift));                 \<br>
-               cmask = (type) __v << (ts - cshift);                    \<br>
+               mask = _bt_make_mask(type, ts - cshift);                \<br>
+               cmask = _bt_lshift((type) (__v), ts - cshift);          \<br>
                cmask &= ~mask;                                         \<br>
                __ptr[this_unit] &= mask;                               \<br>
                __ptr[this_unit] |= cmask;                              \<br>
-               __v = _bt_piecewise_rshift(__v, cshift);                \<br>
+               _bt_safe_rshift(__v, cshift);                           \<br>
                end -= cshift;                                          \<br>
                this_unit--;                                            \<br>
        }                                                               \<br>
        for (; (long) this_unit >= (long) start_unit + 1; this_unit--) { \<br>
                __ptr[this_unit] = (type) __v;                          \<br>
-               __v = _bt_piecewise_rshift(__v, ts);                    \<br>
+               _bt_safe_rshift(__v, ts);                               \<br>
                end -= ts;                                              \<br>
        }                                                               \<br>
        if (__start % ts) {                                             \<br>
-               mask = (~(type) 0) << (ts - (__start % ts));            \<br>
+               mask = _bt_make_mask_complement(type, ts - (__start % ts)); \<br>
                cmask = (type) __v;                                     \<br>
                cmask &= ~mask;                                         \<br>
                __ptr[this_unit] &= mask;                               \<br>
@@ -243,8 +351,8 @@ do {                                                                        \<br>
<br>
 #define _bt_bitfield_read_le(_ptr, type, _start, _length, _vptr)       \<br>
 do {                                                                   \<br>
-       typeof(*(_vptr)) *__vptr = (_vptr);                             \<br>
-       typeof(*__vptr) __v;                                            \<br>
+       __typeof__(*(_vptr)) *__vptr = (_vptr);                         \<br>
+       __typeof__(*__vptr) __v;                                        \<br>
        type *__ptr = (void *) (_ptr);                                  \<br>
        unsigned long __start = (_start), __length = (_length);         \<br>
        type mask, cmask;                                               \<br>
@@ -252,10 +360,6 @@ do {                                                                       \<br>
        unsigned long start_unit, end_unit, this_unit;                  \<br>
        unsigned long end, cshift; /* cshift is "complement shift" */   \<br>
                                                                        \<br>
-       { _bt_check_max_64bit(type); }                                  \<br>
-       { _bt_check_max_64bit(typeof(*_vptr)); }                        \<br>
-       { _bt_check_max_64bit(typeof(*_ptr)); }                         \<br>
-                                                                       \<br>
        if (!__length) {                                                \<br>
                *__vptr = 0;                                            \<br>
                break;                                                  \<br>
@@ -266,56 +370,56 @@ do {                                                                      \<br>
        end_unit = (end + (ts - 1)) / ts;                               \<br>
                                                                        \<br>
        this_unit = end_unit - 1;                                       \<br>
-       if (_bt_is_signed_type(typeof(__v))                             \<br>
-           && (__ptr[this_unit] & ((type) 1 << ((end % ts ? : ts) - 1)))) \<br>
-               __v = ~(typeof(__v)) 0;                                 \<br>
+       if (_bt_is_signed_type(__typeof__(__v))                         \<br>
+           && (__ptr[this_unit] & _bt_lshift((type) 1, (end % ts ? end % ts : ts) - 1))) \<br>
+               __v = ~(__typeof__(__v)) 0;                             \<br>
        else                                                            \<br>
                __v = 0;                                                \<br>
        if (start_unit == end_unit - 1) {                               \<br>
                cmask = __ptr[this_unit];                               \<br>
-               cmask >>= (__start % ts);                               \<br>
+               cmask = _bt_rshift(cmask, __start % ts);                \<br>
                if ((end - __start) % ts) {                             \<br>
-                       mask = ~((~(type) 0) << (end - __start));       \<br>
+                       mask = _bt_make_mask(type, end - __start);      \<br>
                        cmask &= mask;                                  \<br>
                }                                                       \<br>
-               __v = _bt_piecewise_lshift(__v, end - __start);         \<br>
-               __v |= _bt_unsigned_cast(typeof(__v), cmask);           \<br>
+               _bt_safe_lshift(__v, end - __start);                    \<br>
+               __v |= _bt_cast_value_to_unsigned_type(__typeof__(__v), cmask); \<br>
                *__vptr = __v;                                          \<br>
                break;                                                  \<br>
        }                                                               \<br>
        if (end % ts) {                                                 \<br>
                cshift = end % ts;                                      \<br>
-               mask = ~((~(type) 0) << cshift);                        \<br>
+               mask = _bt_make_mask(type, cshift);                     \<br>
                cmask = __ptr[this_unit];                               \<br>
                cmask &= mask;                                          \<br>
-               __v = _bt_piecewise_lshift(__v, cshift);                \<br>
-               __v |= _bt_unsigned_cast(typeof(__v), cmask);           \<br>
+               _bt_safe_lshift(__v, cshift);                           \<br>
+               __v |= _bt_cast_value_to_unsigned_type(__typeof__(__v), cmask); \<br>
                end -= cshift;                                          \<br>
                this_unit--;                                            \<br>
        }                                                               \<br>
        for (; (long) this_unit >= (long) start_unit + 1; this_unit--) { \<br>
-               __v = _bt_piecewise_lshift(__v, ts);                    \<br>
-               __v |= _bt_unsigned_cast(typeof(__v), __ptr[this_unit]);\<br>
+               _bt_safe_lshift(__v, ts);                               \<br>
+               __v |= _bt_cast_value_to_unsigned_type(__typeof__(__v), __ptr[this_unit]); \<br>
                end -= ts;                                              \<br>
        }                                                               \<br>
        if (__start % ts) {                                             \<br>
-               mask = ~((~(type) 0) << (ts - (__start % ts)));         \<br>
+               mask = _bt_make_mask(type, ts - (__start % ts));        \<br>
                cmask = __ptr[this_unit];                               \<br>
-               cmask >>= (__start % ts);                               \<br>
+               cmask = _bt_rshift(cmask, __start % ts);                \<br>
                cmask &= mask;                                          \<br>
-               __v = _bt_piecewise_lshift(__v, ts - (__start % ts));   \<br>
-               __v |= _bt_unsigned_cast(typeof(__v), cmask);           \<br>
+               _bt_safe_lshift(__v, ts - (__start % ts));              \<br>
+               __v |= _bt_cast_value_to_unsigned_type(__typeof__(__v), cmask); \<br>
        } else {                                                        \<br>
-               __v = _bt_piecewise_lshift(__v, ts);                    \<br>
-               __v |= _bt_unsigned_cast(typeof(__v), __ptr[this_unit]);\<br>
+               _bt_safe_lshift(__v, ts);                               \<br>
+               __v |= _bt_cast_value_to_unsigned_type(__typeof__(__v), __ptr[this_unit]); \<br>
        }                                                               \<br>
        *__vptr = __v;                                                  \<br>
 } while (0)<br>
<br>
 #define _bt_bitfield_read_be(_ptr, type, _start, _length, _vptr)       \<br>
 do {                                                                   \<br>
-       typeof(*(_vptr)) *__vptr = (_vptr);                             \<br>
-       typeof(*__vptr) __v;                                            \<br>
+       __typeof__(*(_vptr)) *__vptr = (_vptr);                         \<br>
+       __typeof__(*__vptr) __v;                                        \<br>
        type *__ptr = (void *) (_ptr);                                  \<br>
        unsigned long __start = (_start), __length = (_length);         \<br>
        type mask, cmask;                                               \<br>
@@ -323,10 +427,6 @@ do {                                                                       \<br>
        unsigned long start_unit, end_unit, this_unit;                  \<br>
        unsigned long end, cshift; /* cshift is "complement shift" */   \<br>
                                                                        \<br>
-       { _bt_check_max_64bit(type); }                                  \<br>
-       { _bt_check_max_64bit(typeof(*_vptr)); }                        \<br>
-       { _bt_check_max_64bit(typeof(*_ptr)); }                         \<br>
-                                                                       \<br>
        if (!__length) {                                                \<br>
                *__vptr = 0;                                            \<br>
                break;                                                  \<br>
@@ -337,48 +437,48 @@ do {                                                                      \<br>
        end_unit = (end + (ts - 1)) / ts;                               \<br>
                                                                        \<br>
        this_unit = start_unit;                                         \<br>
-       if (_bt_is_signed_type(typeof(__v))                             \<br>
-           && (__ptr[this_unit] & ((type) 1 << (ts - (__start % ts) - 1)))) \<br>
-               __v = ~(typeof(__v)) 0;                                 \<br>
+       if (_bt_is_signed_type(__typeof__(__v))                         \<br>
+           && (__ptr[this_unit] & _bt_lshift((type) 1, ts - (__start % ts) - 1))) \<br>
+               __v = ~(__typeof__(__v)) 0;                             \<br>
        else                                                            \<br>
                __v = 0;                                                \<br>
        if (start_unit == end_unit - 1) {                               \<br>
                cmask = __ptr[this_unit];                               \<br>
-               cmask >>= (ts - (end % ts)) % ts;                       \<br>
+               cmask = _bt_rshift(cmask, (ts - (end % ts)) % ts);      \<br>
                if ((end - __start) % ts) {                             \<br>
-                       mask = ~((~(type) 0) << (end - __start));       \<br>
+                       mask = _bt_make_mask(type, end - __start);      \<br>
                        cmask &= mask;                                  \<br>
                }                                                       \<br>
-               __v = _bt_piecewise_lshift(__v, end - __start);         \<br>
-               __v |= _bt_unsigned_cast(typeof(__v), cmask);           \<br>
+               _bt_safe_lshift(__v, end - __start);            \<br>
+               __v |= _bt_cast_value_to_unsigned_type(__typeof__(__v), cmask); \<br>
                *__vptr = __v;                                          \<br>
                break;                                                  \<br>
        }                                                               \<br>
        if (__start % ts) {                                             \<br>
                cshift = __start % ts;                                  \<br>
-               mask = ~((~(type) 0) << (ts - cshift));                 \<br>
+               mask = _bt_make_mask(type, ts - cshift);                \<br>
                cmask = __ptr[this_unit];                               \<br>
                cmask &= mask;                                          \<br>
-               __v = _bt_piecewise_lshift(__v, ts - cshift);           \<br>
-               __v |= _bt_unsigned_cast(typeof(__v), cmask);           \<br>
+               _bt_safe_lshift(__v, ts - cshift);                      \<br>
+               __v |= _bt_cast_value_to_unsigned_type(__typeof__(__v), cmask); \<br>
                __start += ts - cshift;                                 \<br>
                this_unit++;                                            \<br>
        }                                                               \<br>
        for (; this_unit < end_unit - 1; this_unit++) {                 \<br>
-               __v = _bt_piecewise_lshift(__v, ts);                    \<br>
-               __v |= _bt_unsigned_cast(typeof(__v), __ptr[this_unit]);\<br>
+               _bt_safe_lshift(__v, ts);                               \<br>
+               __v |= _bt_cast_value_to_unsigned_type(__typeof__(__v), __ptr[this_unit]); \<br>
                __start += ts;                                          \<br>
        }                                                               \<br>
        if (end % ts) {                                                 \<br>
-               mask = ~((~(type) 0) << (end % ts));                    \<br>
+               mask = _bt_make_mask(type, end % ts);                   \<br>
                cmask = __ptr[this_unit];                               \<br>
-               cmask >>= ts - (end % ts);                              \<br>
+               cmask = _bt_rshift(cmask, ts - (end % ts));             \<br>
                cmask &= mask;                                          \<br>
-               __v = _bt_piecewise_lshift(__v, end % ts);              \<br>
-               __v |= _bt_unsigned_cast(typeof(__v), cmask);           \<br>
+               _bt_safe_lshift(__v, end % ts);                         \<br>
+               __v |= _bt_cast_value_to_unsigned_type(__typeof__(__v), cmask); \<br>
        } else {                                                        \<br>
-               __v = _bt_piecewise_lshift(__v, ts);                    \<br>
-               __v |= _bt_unsigned_cast(typeof(__v), __ptr[this_unit]);\<br>
+               _bt_safe_lshift(__v, ts);                               \<br>
+               __v |= _bt_cast_value_to_unsigned_type(__typeof__(__v), __ptr[this_unit]); \<br>
        }                                                               \<br>
        *__vptr = __v;                                                  \<br>
 } while (0)<br>
diff --git a/plugins/text/pretty/print.c b/plugins/text/pretty/print.c<br>
index 95189d5e..c36a6e81 100644<br>
--- a/plugins/text/pretty/print.c<br>
+++ b/plugins/text/pretty/print.c<br>
@@ -546,10 +546,10 @@ int print_integer(struct pretty_component *pretty,<br>
<br>
                len = bt_field_class_integer_get_field_value_range(int_fc);<br>
                g_string_append(pretty->string, "0b");<br>
-               v.u = _bt_piecewise_lshift(v.u, 64 - len);<br>
+               _bt_safe_lshift(v.u, 64 - len);<br>
                for (bitnr = 0; bitnr < len; bitnr++) {<br>
                        g_string_append_printf(pretty->string, "%u", (v.u & (1ULL << 63)) ? 1 : 0);<br>
-                       v.u = _bt_piecewise_lshift(v.u, 1);<br>
+                       _bt_safe_lshift(v.u, 1);<br>
                }<br>
                break;<br>
        }<br>
-- <br>
2.11.0<br>
<br>
</blockquote></div><br clear="all"><br>-- <br><div dir="ltr" class="gmail-m_-6309759432213676215gmail-m_-5365181620922452054gmail_signature">Jérémie Galarneau<br>EfficiOS Inc.<br><a href="http://www.efficios.com" target="_blank">http://www.efficios.com</a></div>