[clang] [clang:static-analyzer] fix: allow construction of atomic pointers from nullptr. (PR #190131)

via cfe-commits cfe-commits at lists.llvm.org
Thu Apr 2 01:18:26 PDT 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-static-analyzer-1

Author: Carson Radtke (carsonRadtke)

<details>
<summary>Changes</summary>

fixes: https://github.com/llvm/llvm-project/issues/187925

Previous code did not handle passing AtomicType to `makeNullWithType` and this led to an assertion failure.

This change updates `makeNullWithType` to allow for atomic types to be passed in and to "unwrap" them in order to get the correct pointer/reference type for constructing the nullptr.

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


3 Files Affected:

- (modified) clang/include/clang/StaticAnalyzer/Core/PathSensitive/SValBuilder.h (+5-3) 
- (modified) clang/include/clang/StaticAnalyzer/Core/PathSensitive/SVals.h (+3) 
- (modified) clang/test/Analysis/atomics.c (+5) 


``````````diff
diff --git a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/SValBuilder.h b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/SValBuilder.h
index 2911554de9d97..2df0aa33f2cc8 100644
--- a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/SValBuilder.h
+++ b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/SValBuilder.h
@@ -346,11 +346,13 @@ class SValBuilder {
   /// \param type pointer type.
   loc::ConcreteInt makeNullWithType(QualType type) {
     // We cannot use the `isAnyPointerType()`.
-    assert((type->isPointerType() || type->isObjCObjectPointerType() ||
-            type->isBlockPointerType() || type->isNullPtrType() ||
-            type->isReferenceType()) &&
+    assert((type->isObjCObjectPointerType() || Loc::isLocType(type)) &&
            "makeNullWithType must use pointer type");
 
+    type = type->isAtomicType()
+               ? type->getAs<AtomicType>()->getValueType()
+               : type;
+
     // The `sizeof(T&)` is `sizeof(T)`, thus we replace the reference with a
     // pointer. Here we assume that references are actually implemented by
     // pointers under-the-hood.
diff --git a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/SVals.h b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/SVals.h
index aeb57b28077c6..4207432be2ae6 100644
--- a/clang/include/clang/StaticAnalyzer/Core/PathSensitive/SVals.h
+++ b/clang/include/clang/StaticAnalyzer/Core/PathSensitive/SVals.h
@@ -260,6 +260,9 @@ class Loc : public DefinedSVal {
   void dumpToStream(raw_ostream &Out) const;
 
   static bool isLocType(QualType T) {
+    T = T->isAtomicType()
+          ? T->getAs<AtomicType>()->getValueType()
+          : T;
     return T->isAnyPointerType() || T->isBlockPointerType() ||
            T->isReferenceType() || T->isNullPtrType();
   }
diff --git a/clang/test/Analysis/atomics.c b/clang/test/Analysis/atomics.c
index ef1a216c7d577..fc5ab5f589a00 100644
--- a/clang/test/Analysis/atomics.c
+++ b/clang/test/Analysis/atomics.c
@@ -101,3 +101,8 @@ void test_atomic_compare(int input) {
     // no crash
   }
 }
+
+void test_atomic_gh187925(void) {
+  void foo(_Atomic(void*));
+  foo(0); // no assertion failure
+}

``````````

</details>


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


More information about the cfe-commits mailing list