diff --git a/bot-articles/ub.md b/bot-articles/ub.md deleted file mode 100644 index c13bf88..0000000 --- a/bot-articles/ub.md +++ /dev/null @@ -1,44 +0,0 @@ - - -# Undefined Behavior - -Undefined behavior (UB) is behavior for which C++ imposes no requirements. -This could mean that your code crashes, -isn't executed when it should be, -or does other unexpected things. - -Typical causes are: reading uninitialized memory, -performing out-of-bounds memory access, -or using an object when it no longer exists. - - -## Example: Indeterminate Value -```cpp -int i; // uninitialized -while(i < 10) { - printf("%d\n", i++); -} -``` --# Note: Since C++26, -this code has erroneous behavior, not undefined behavior. - - -## Example: Out-of-Bounds Access -```cpp -int arr[4]; -for(int i = 0; i < 8; i++) { - arr[i] = 0; -} -``` - -## Why it Matters - -Compilers often do not give warnings or errors about UB, -and its existence in your code can cause surprising, -unpredictable, and buggy behavior, -which may even result in security vulnerabilities. - -## See Also - -- [cppreference: Undefined Behavior](https://en.cppreference.com/w/cpp/language/ub) -- [What is Undefined Behavior?](https://64.github.io/cpp-faq/undefined-behaviour/) diff --git a/wiki/resources/general/ub.md b/wiki/resources/general/ub.md new file mode 100644 index 0000000..cd0f2b7 --- /dev/null +++ b/wiki/resources/general/ub.md @@ -0,0 +1,87 @@ +--- +alias: ub +bot_article: | + # Undefined Behavior + + Undefined behavior (UB) is behavior for which the C/C++ standard imposes no requirements. + + Typical causes are: reading uninitialized memory, + out-of-bounds memory access, + or using an object when it no longer exists. + + ## Why it Matters + + Compilers are not required to give warnings or errors for undefined behaviors and having them in your code can lead to + non-portable code, strange bugs and security vulnerabilities. +--- + +# Undefined Behavior + +Undefined Behavior is behavior for which the C/C++ standard imposes no requirements.
In other words, the program +can crash, continue or have different behaviors on different platforms.
Here is an example of a UB: + +```cpp +#include + +int main() { + int a; + // UB in C and pre-C++26: uninitialized memory access + if (a >= 0) { // [!code warning] + std::cout << "non negative" << std::endl; + } else { + std::cout << "negative" << std::endl; + } + return 0; +} +``` + +Uninitialized memory has what is called indeterminate values, values which are unspecified. The actual value of `a` +depends on the platform and the compiler. + +## Why it Matters? + +Compilers are not required to give warnings or errors for undefined behaviors and having them in your code can lead to +non-portable behaviors, strange bugs and security vulnerabilities. + +## Common UBs + +- **Signed integer overflow** + + ```cpp + int x = INT_MAX; + // UB + int y = x + 1; // [!code warning] + ``` + + ::: info + + Unsigned integer overflow is defined. `UINT_MAX + 1 == 0` is true. + + ::: + +- **Out-of-bound access** + + ```cpp + int a[5] = {1,2,3,4,5}; + // UB + int x = a[5]; // [!code warning] + ``` + +- **Uninitialized memory access** + + ```cpp + int x; + // UB pre-C++26 + int y = x; // [!code warning] + ``` + + ::: info + + Since **C++26**: this is classified as _erroneous behavior_, not _undefined behavior_. + + ::: + +## See also + +- [Undefined behavior C (cppreference)](https://en.cppreference.com/c/language/behavior) +- [Undefined behavior C++ (cppreference)](https://en.cppreference.com/cpp/language/ub) diff --git a/wiki/resources/sidebar.ts b/wiki/resources/sidebar.ts index 515b2c6..c6d5743 100644 --- a/wiki/resources/sidebar.ts +++ b/wiki/resources/sidebar.ts @@ -48,6 +48,7 @@ const sidebar = [ items: [ { text: "Address Sanitizer", link: "/resources/general/asan" }, { text: "Ownership", link: "/resources/general/ownership" }, + { text: "Undefined Behavior", link: "/resources/general/ub" }, { text: "Standards", link: "/resources/general/standards" }, { text: "Project Ideas", link: "/resources/general/project-ideas" }, { text: "Floating Point Numbers", link: "/resources/general/floating-point" },