Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 0 additions & 44 deletions bot-articles/ub.md

This file was deleted.

87 changes: 87 additions & 0 deletions wiki/resources/general/ub.md
Original file line number Diff line number Diff line change
@@ -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. <br/> In other words, the program
can crash, continue or have different behaviors on different platforms. <br/> Here is an example of a UB:

```cpp
#include <iostream>

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)
1 change: 1 addition & 0 deletions wiki/resources/sidebar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" },
Expand Down
Loading