Showing posts with label cpp11_ub. Show all posts
Showing posts with label cpp11_ub. Show all posts

May 1, 2014

[C++] Undefined Behavior, UB


  • Signed integer overflow (but not unsigned!)
  • Dereferencing NULL pointer or result of malloc(0) // 0 point to virtual memory 0 position. Also a reason why program start memory not 0.
  • Shift greater than (or equal to) the width of the operand
  • Reading from uninitialized variables
  • Modifying a variable more than once in an expression : Sequence point
  • Buffer overflow
  • Comparing pointers into two different data structures
  • Pointer overflow : GCC and pointer overflows
  • Modifying a const object (C++) or a string literal
  • Negating INT_MIN  : Why does -INT_MIN = INT_MIN in a signed, two's complement representation?
  • Data races
  • Mismatch between new and delete
  • Calling a library routine w/o fulfilling the prerequisites
  • memcpy with overlapping buffers // copy/copy_if , The source and destination ranges cannot overlap.
  • atomic_is_lock_free requires passing in obj shall not be nullptr
  • If during the evaluation of an expression, the result is not mathematically defined or not in the range of representable values for its type, the behavior is undefined.
clang -fsanitize=undefined

Dec 4, 2013

[C++][NOTE][BIGINNER] signed int overflow

Reference:
Signed Overflow
why-does-integer-overflow-on-x86-with-gcc-cause-an-infinite-loop
Signed integer overflow is undefined behaviour according to the standard §5.4:


"If during the evaluation of an expression, the result is not mathematically

defined or not in the range of representable values for its type, the behavior is undefined."

Most implementations will just wrap around, so if you try it out on your machine,

you will probably see the same as if you had done

std::cout << std::numeric_limits<int>::min();
Relying on such undefined behaviour is however _not_ safe.