[llvm] [NFC] Address cppcheck legacyUninitvar (PR #175417)

via llvm-commits llvm-commits at lists.llvm.org
Sun Jan 11 00:57:48 PST 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-support

Author: Konrad Kleine (kwk)

<details>
<summary>Changes</summary>

This is the first in line of more "fixes" to
silence some static analysis warnings. I first
wanted to see how the community reacts to those.

```
 Error: CPPCHECK_WARNING (CWE-457): [#def65]
 llvm-project-21.1.8.src/llvm/include/llvm/Support/MathExtras.h:620: error[legacyUninitvar]: Uninitialized variable: Dummy
 #  618|   SaturatingAdd(T X, T Y, bool *ResultOverflowed = nullptr) {
 #  619|     bool Dummy;
 #  620|->   bool &Overflowed = ResultOverflowed ? *ResultOverflowed : Dummy;
 #  621|     // Hacker's Delight, p. 29
 #  622|     T Z = X + Y;
```

(Source: https://svashisht.fedorapeople.org/openscanhub/mass-scans/f44-08-Jan-2026/llvm-21.1.8-1.fc44/scan-results.html#def65)

The `Overflowed` variable is first written to.
That means the initial value in `Dummy` doesn't
matter.

[compiler-explorer](https://godbolt.org/z/zKf3P3jah) shows that an additional

```
mov BYTE PTR [rbp-13], 0 # Dummy,
```

is required when `Dummy` is initialized.

Alternative 1
-------------

Alternatively to initializing `Dummy` we could
add this to the line before:

```c++
// cppcheckIgnore legacyUninitvar
```

But it would require that `cppcheck` runs with
`--inline-suppr` which it currently isn't in
[OpenScanHub](https://openscanhub.dev/).
This option isn't turned on by default in `cppcheck`.

Alternative 2
-------------

Add the finding here https://github.com/openscanhub/known-false-positives.

The problem with this alternative is that others
who run cppcheck will still run into the same
warning.

---
Full diff: https://github.com/llvm/llvm-project/pull/175417.diff


1 Files Affected:

- (modified) llvm/include/llvm/Support/MathExtras.h (+1-1) 


``````````diff
diff --git a/llvm/include/llvm/Support/MathExtras.h b/llvm/include/llvm/Support/MathExtras.h
index 0a253efc2abcb..391c932c87ed2 100644
--- a/llvm/include/llvm/Support/MathExtras.h
+++ b/llvm/include/llvm/Support/MathExtras.h
@@ -607,7 +607,7 @@ constexpr T AbsoluteDifference(U X, V Y) {
 template <typename T>
 std::enable_if_t<std::is_unsigned_v<T>, T>
 SaturatingAdd(T X, T Y, bool *ResultOverflowed = nullptr) {
-  bool Dummy;
+  bool Dummy = false;
   bool &Overflowed = ResultOverflowed ? *ResultOverflowed : Dummy;
   // Hacker's Delight, p. 29
   T Z = X + Y;

``````````

</details>


https://github.com/llvm/llvm-project/pull/175417


More information about the llvm-commits mailing list