Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. commit 19e7aca212afc7360f69f93100a157c0e659861d
  2. Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
  3. Date: Tue May 14 11:18:34 2019 -0400
  4.  
  5. Fix: bitfield: shift undefined/implementation defined behaviors (v3)
  6.  
  7. bitfield.h uses the left shift operator with a left operand which
  8. may be negative. The C99 standard states that shifting a negative
  9. value is undefined.
  10.  
  11. When building with -Wshift-negative-value, we get this gcc warning:
  12.  
  13. In file included from /home/smarchi/src/babeltrace/include/babeltrace/ctfser-internal.h:44:0,
  14. from /home/smarchi/src/babeltrace/ctfser/ctfser.c:42:
  15. /home/smarchi/src/babeltrace/include/babeltrace/ctfser-internal.h: In function ‘bt_ctfser_write_unsigned_int’:
  16. /home/smarchi/src/babeltrace/include/babeltrace/bitfield-internal.h:116:24: error: left shift of negative value [-Werror=shift-negative-value]
  17. mask = ~((~(type) 0) << (__start % ts)); \
  18. ^
  19. /home/smarchi/src/babeltrace/include/babeltrace/bitfield-internal.h:222:2: note: in expansion of macro ‘_bt_bitfield_write_le’
  20. _bt_bitfield_write_le(ptr, type, _start, _length, _v)
  21. ^~~~~~~~~~~~~~~~~~~~~
  22. /home/smarchi/src/babeltrace/include/babeltrace/ctfser-internal.h:418:3: note: in expansion of macro ‘bt_bitfield_write_le’
  23. bt_bitfield_write_le(mmap_align_addr(ctfser->base_mma) +
  24. ^~~~~~~~~~~~~~~~~~~~
  25.  
  26. This boils down to the fact that the expression ~((uint8_t)0) has type
  27. "signed int", which is used as an operand of the left shift. This is due
  28. to the integer promotion rules of C99 (6.3.3.1):
  29.  
  30. If an int can represent all values of the original type, the value is
  31. converted to an int; otherwise, it is converted to an unsigned int.
  32. These are called the integer promotions. All other types are unchanged
  33. by the integer promotions.
  34.  
  35. We also need to cast the result explicitly into the left hand
  36. side type to deal with:
  37.  
  38. warning: large integer implicitly truncated to unsigned type [-Woverflow]
  39.  
  40. The C99 standard states that a right shift has implementation-defined
  41. behavior when shifting a signed negative value. Add a preprocessor check
  42. that the compiler provides the expected behavior, else provide an
  43. alternative implementation which guarantees the intended behavior.
  44.  
  45. A preprocessor check is also added to ensure that the compiler
  46. representation for signed values is two's complement, which is expected
  47. by this header.
  48.  
  49. Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
  50. Change-Id: I17ba38952ddc4c27d770649f782fd249f6ea69be
  51. ---
  52. Changes since v1:
  53. - Fix wrong use of _$prefix$bt_make_mask() in _$prefix$bt_bitfield_write_be.
  54. Should be _$prefix$bt_make_mask_complement().
  55.  
  56. Changes since v2:
  57. - Documentation and style updates.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement