[llvm] [Support] Properly zero initialize CPU set when querying affinity (PR #142924)

via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 5 02:08:41 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-support

Author: Christian Ulmann (Dinistro)

<details>
<summary>Changes</summary>

This commit resolves a potential issue of working with uninitialized memory when querying the CPU's affinity. The man page of `sched_getaffinity` does not guarantee that the memory will be fully overwritten, so this change should ensure that issues are avoided.

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


1 Files Affected:

- (modified) llvm/lib/Support/Unix/Threading.inc (+1) 


``````````diff
diff --git a/llvm/lib/Support/Unix/Threading.inc b/llvm/lib/Support/Unix/Threading.inc
index 15a5b008604c3..a553230de054c 100644
--- a/llvm/lib/Support/Unix/Threading.inc
+++ b/llvm/lib/Support/Unix/Threading.inc
@@ -316,6 +316,7 @@ static int computeHostNumHardwareThreads() {
     return CPU_COUNT(&mask);
 #elif defined(__linux__)
   cpu_set_t Set;
+  CPU_ZERO(&Set);
   if (sched_getaffinity(0, sizeof(Set), &Set) == 0)
     return CPU_COUNT(&Set);
 #endif

``````````

</details>


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


More information about the llvm-commits mailing list