23 Eylül 2019 Pazartesi

Undefined Behavior

C ve C++'taki undefined behavior geçmişin bir mirası. Java ve C# gibi daha yeni dillerde undefined behavior'a ver yok.

İlginç bir şekilde undefined behavior kendi içinde sınıflandırılmış.

Ada ve C Karşılaştırması
Açıklaması şöyle.
Ada has a number of safety features built into the language. (Compare with C, where the standard has twelve pages that summarize the undefined behaviors built into the language. Compare with C++, where the undefined behaviors are so numerous that they don't even list them.) Those Ada safety features can have a significant performance cost, and because of this, Ada also supplies the ability to selectively disable those safety features.
Sık Karşılaşılan Undefined Behavior'lar
1. İlklendirilmemiş Değişken
Default Initialization yazısına bakabilirsiniz.

2. Signed Integer Overflow
Signed Integer Overflow yazısına bakabilirsiniz.

Bounded Undefined Behaviour
Açıklaması şöyle
Bounded UB is undefined behavior that cannot perform an illegal memory write, although it may trap and may produce or store indeterminate values.
Bellekte rastgele yerlere yazamaz. Şu davranışlar Bounded UB olarak tanımlı
  • multithreaded data races
  • use of a indeterminate values with automatic storage duration
  • strict aliasing violations
  • misaligned object access
  • signed integer overflow
  • unsequenced side-effects modify the same scalar or modify and read the same scalar
  • floating-to-integer or pointer-to-integer conversion overflow
  • bitwise shift by a negative or too large bit count
  • integer division by zero
  • use of a void expression
  • direct assignment or memcpy of inexactly-overlapped objects
  • restrict violations
  • etc.. ALL undefined behavior that's not in the critical list.
Criticial Undefined Behaviour
Açıklaması şöyle.
Critical UB is undefined behavior that might perform a memory write or a volatile memory read out of bounds of any object. A program that has critical undefined behavior may be susceptible to security exploits.
Şu davranışlar Critical UB olarak tanımlı.
  • access to an object outside of its lifetime (e.g. through a dangling pointer)
  • write to an object whose declarations are not compatible
  • function call through a function pointer whose type is not compatible with the type of the function it points to
  • lvalue expression is evaluated, but does not designate an object attempted modification of a string literal
  • dereferencing an invalid (null, indeterminate, etc) or past-the-end pointer
  • modification of a const object through a non-const pointer
  • call to a standard library function or macro with an invalid argument
  • call to a variadic standard library function with unexpected argument type (e.g. call to printf with an argument of the type that doesn't match its conversion specifier)
  • longjmp where there is no setjmp up the calling scope, across threads, or from within the scope of a VM type.
  • any use of the pointer that was deallocated by free or realloc
  • any string or wide string library function accesses an array out of bounds

Hiç yorum yok:

Yorum Gönder