From 9bb88a1d498d07900f3054eab691812867e5579a Mon Sep 17 00:00:00 2001 From: ngtv Date: Wed, 24 Jun 2026 17:03:07 +0700 Subject: [PATCH 1/5] Port ub.md --- bot-articles/ub.md | 44 ---------------- wiki/resources/general/ub.md | 97 ++++++++++++++++++++++++++++++++++++ wiki/resources/sidebar.ts | 1 + 3 files changed, 98 insertions(+), 44 deletions(-) delete mode 100644 bot-articles/ub.md create mode 100644 wiki/resources/general/ub.md 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..a972bc1 --- /dev/null +++ b/wiki/resources/general/ub.md @@ -0,0 +1,97 @@ +--- +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, + performing out-of-bounds memory access, + or using an object when it no longer exists. + + ## Why it Matters + + Since the standard does not restrict what could happen, the program may crash, + continue execution, have different behaviors on different platforms. + This can lead to strange bugs and security issues. +--- + +# Undefined Behavior + +For the convinience of optimization, the C/C++ standard left out a few undefined cases known as _Undefined Behavior_ +(UB). Undefined Behavior is behavior for which the C/C++ standard imposes no restrictions.
In other words, the +program can crash, continue or have different behaviors on different platforms. None of which are guaranteed.
Here +is an example of a UB: + +```cpp +#include + +int main() { + int a; + // UB: uninitialized memory access + if (a >= 0) { // [!code warning] + std::cout << "non negative" << std::endl; + } else { + std::cout << "negative" << std::endl; + } + return 0; +} +``` + +Most commonly, uninitialized access results in what is known as garbage values. Those are values that are arbitrary and +unpredictable. This program outputs either `non negative` or `negative` and subsequent runs can be different. + +::: warning + +Because this is an undefined behavior, the behavior above is not guaranteed. It can print either strings or crash +entirely. + +::: + +## Why it Matters? + +Compilers are not required to give warnings or errors for undefined behaviors and the presence of 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" }, From bd8d3299446c0025972dd3da3a0fb3c9613cb6b0 Mon Sep 17 00:00:00 2001 From: "N. Tuan Vy" Date: Fri, 26 Jun 2026 19:45:03 +0700 Subject: [PATCH 2/5] change terminology from 'garbage' to 'indeterminate' --- wiki/resources/general/ub.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/wiki/resources/general/ub.md b/wiki/resources/general/ub.md index a972bc1..c473428 100644 --- a/wiki/resources/general/ub.md +++ b/wiki/resources/general/ub.md @@ -38,7 +38,7 @@ int main() { } ``` -Most commonly, uninitialized access results in what is known as garbage values. Those are values that are arbitrary and +Most commonly, uninitialized access results in what is known as indeterminate values. Those are values that are arbitrary and unpredictable. This program outputs either `non negative` or `negative` and subsequent runs can be different. ::: warning From 8ae9ff04e4914e659463c51c5aafacc26276538f Mon Sep 17 00:00:00 2001 From: ngtv Date: Fri, 26 Jun 2026 20:46:42 +0700 Subject: [PATCH 3/5] reduce clutter and jargons --- wiki/resources/general/ub.md | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/wiki/resources/general/ub.md b/wiki/resources/general/ub.md index c473428..37d2b12 100644 --- a/wiki/resources/general/ub.md +++ b/wiki/resources/general/ub.md @@ -6,22 +6,21 @@ bot_article: | Undefined behavior (UB) is behavior for which the C/C++ standard imposes no requirements. Typical causes are: reading uninitialized memory, - performing out-of-bounds memory access, + out-of-bounds memory access, or using an object when it no longer exists. ## Why it Matters - Since the standard does not restrict what could happen, the program may crash, + Because the standard does not restrict what could happen, the program may crash, continue execution, have different behaviors on different platforms. This can lead to strange bugs and security issues. --- # Undefined Behavior -For the convinience of optimization, the C/C++ standard left out a few undefined cases known as _Undefined Behavior_ -(UB). Undefined Behavior is behavior for which the C/C++ standard imposes no restrictions.
In other words, the -program can crash, continue or have different behaviors on different platforms. None of which are guaranteed.
Here -is an example of a UB: +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. None of which are guaranteed.
Here is an +example of a UB: ```cpp #include @@ -38,20 +37,20 @@ int main() { } ``` -Most commonly, uninitialized access results in what is known as indeterminate values. Those are values that are arbitrary and -unpredictable. This program outputs either `non negative` or `negative` and subsequent runs can be different. +Most commonly, uninitialized access results in what is called indeterminate values. Those are values that are arbitrary +and unpredictable. This program outputs either `non negative` or `negative` and the result of each runs can be +different. ::: warning -Because this is an undefined behavior, the behavior above is not guaranteed. It can print either strings or crash -entirely. +Because this is an undefined behavior, the behavior above is not guaranteed. It can print either strings or just crash. ::: ## Why it Matters? -Compilers are not required to give warnings or errors for undefined behaviors and the presence of them in your code can -lead to non-portable behaviors, strange bugs and security vulnerabilities. +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 From f576fd5e051eac85837c0b3544d4f719c870f9c4 Mon Sep 17 00:00:00 2001 From: ngtv Date: Mon, 29 Jun 2026 06:52:47 +0700 Subject: [PATCH 4/5] improve wording --- wiki/resources/general/ub.md | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/wiki/resources/general/ub.md b/wiki/resources/general/ub.md index 37d2b12..9c92cac 100644 --- a/wiki/resources/general/ub.md +++ b/wiki/resources/general/ub.md @@ -11,15 +11,14 @@ bot_article: | ## Why it Matters - Because the standard does not restrict what could happen, the program may crash, - continue execution, have different behaviors on different platforms. - This can lead to strange bugs and security issues. + 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. None of which are guaranteed.
Here is an +can crash, continue or have different behaviors on different platforms.
Here is an example of a UB: ```cpp @@ -27,7 +26,7 @@ example of a UB: int main() { int a; - // UB: uninitialized memory access + // UB in C and pre-C++26: uninitialized memory access if (a >= 0) { // [!code warning] std::cout << "non negative" << std::endl; } else { @@ -37,15 +36,8 @@ int main() { } ``` -Most commonly, uninitialized access results in what is called indeterminate values. Those are values that are arbitrary -and unpredictable. This program outputs either `non negative` or `negative` and the result of each runs can be -different. - -::: warning - -Because this is an undefined behavior, the behavior above is not guaranteed. It can print either strings or just crash. - -::: +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? From 1b5aaa91e3468fa77929ee7cfa361be03daae8c3 Mon Sep 17 00:00:00 2001 From: ngtv Date: Thu, 2 Jul 2026 21:49:03 +0700 Subject: [PATCH 5/5] fix format --- wiki/resources/general/ub.md | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/wiki/resources/general/ub.md b/wiki/resources/general/ub.md index 9c92cac..cd0f2b7 100644 --- a/wiki/resources/general/ub.md +++ b/wiki/resources/general/ub.md @@ -18,8 +18,7 @@ bot_article: | # 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: +can crash, continue or have different behaviors on different platforms.
Here is an example of a UB: ```cpp #include @@ -36,8 +35,8 @@ int main() { } ``` -Uninitialized memory has what is called indeterminate values, values which are unspecified. -The actual value of `a` depends on the platform and the compiler. +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?