Skip to content

Revert "[analyzer] Make it a noop when initializing a field of empty record" #138951

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 7, 2025

Conversation

steakhal
Copy link
Contributor

@steakhal steakhal commented May 7, 2025

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:static analyzer labels May 7, 2025
@steakhal steakhal added skip-precommit-approval PR for CI feedback, not intended for review and removed clang Clang issues not falling into any other category clang:static analyzer labels May 7, 2025
@steakhal steakhal merged commit 9048c2d into main May 7, 2025
10 of 13 checks passed
@steakhal steakhal deleted the revert-138594-ziqingluo/PR-146753089 branch May 7, 2025 19:52
@llvmbot
Copy link
Member

llvmbot commented May 7, 2025

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

@llvm/pr-subscribers-clang

Author: Balazs Benics (steakhal)

Changes

Reverts llvm/llvm-project#138594

Crashes, see: https://lab.llvm.org/buildbot/#/builders/144/builds/24534


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

2 Files Affected:

  • (modified) clang/lib/StaticAnalyzer/Core/ExprEngineCXX.cpp (+1-6)
  • (removed) clang/test/Analysis/issue-137252.cpp (-50)
diff --git a/clang/lib/StaticAnalyzer/Core/ExprEngineCXX.cpp b/clang/lib/StaticAnalyzer/Core/ExprEngineCXX.cpp
index ff07402a29bba..92ce3fa2225c8 100644
--- a/clang/lib/StaticAnalyzer/Core/ExprEngineCXX.cpp
+++ b/clang/lib/StaticAnalyzer/Core/ExprEngineCXX.cpp
@@ -10,7 +10,6 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "clang/AST/ASTContext.h"
 #include "clang/AST/AttrIterator.h"
 #include "clang/AST/DeclCXX.h"
 #include "clang/AST/ParentMap.h"
@@ -716,11 +715,7 @@ void ExprEngine::handleConstructor(const Expr *E,
         // actually make things worse. Placement new makes this tricky as well,
         // since it's then possible to be initializing one part of a multi-
         // dimensional array.
-        const CXXRecordDecl *TargetHeldRecord =
-            cast<CXXRecordDecl>(CE->getType()->getAsRecordDecl());
-
-        if (!TargetHeldRecord || !TargetHeldRecord->isEmpty())
-          State = State->bindDefaultZero(Target, LCtx);
+        State = State->bindDefaultZero(Target, LCtx);
       }
 
       Bldr.generateNode(CE, N, State, /*tag=*/nullptr,
diff --git a/clang/test/Analysis/issue-137252.cpp b/clang/test/Analysis/issue-137252.cpp
deleted file mode 100644
index 6ca3e20ccbbca..0000000000000
--- a/clang/test/Analysis/issue-137252.cpp
+++ /dev/null
@@ -1,50 +0,0 @@
-// RUN: %clang_analyze_cc1 -analyzer-checker=cplusplus -verify %s
-// RUN: %clang_analyze_cc1 -analyzer-checker=cplusplus -verify %s -DEMPTY_CLASS
-// UNSUPPORTED: system-windows
-// expected-no-diagnostics
-
-// This test reproduces the issue that previously the static analyzer
-// initialized an [[no_unique_address]] empty field to zero,
-// over-writing a non-empty field with the same offset.
-
-namespace std {
-#ifdef EMPTY_CLASS
-
-  struct default_delete {};
-  template <class _Tp, class _Dp = default_delete >
-#else
-  // Class with methods and static members is still empty:
-  template <typename T>
-  class default_delete {
-    T dump();
-    static T x;
-  };
-  template <class _Tp, class _Dp = default_delete<_Tp> >
-#endif
-  class unique_ptr {
-    [[no_unique_address]]  _Tp * __ptr_;
-    [[no_unique_address]] _Dp __deleter_;
-
-  public:
-    explicit unique_ptr(_Tp* __p) noexcept
-      : __ptr_(__p),
-        __deleter_() {}
-
-    ~unique_ptr() {
-      delete __ptr_;
-    }
-  };
-}
-
-struct X {};
-
-int main()
-{
-  // Previously a leak falsely reported here.  It was because the
-  // Static Analyzer engine simulated the initialization of
-  // `__deleter__` incorrectly.  The engine assigned zero to
-  // `__deleter__`--an empty record sharing offset with `__ptr__`.
-  // The assignment over wrote `__ptr__`.
-  std::unique_ptr<X> a(new X()); 
-  return 0;
-}

ziqingluo-90 added a commit that referenced this pull request May 7, 2025
… record" (#138951)

The original commit assumes that
`CXXConstructExpr->getType()->getAsRecordDecl()` is always a
`CXXRecordDecl` but it is not true for ObjC programs.

This relanding changes
`cast<CXXRecordDecl>(CXXConstructExpr->getType()->getAsRecordDecl())`
to
`dyn_cast_or_null<CXXRecordDecl>(CXXConstructExpr->getType()->getAsRecordDecl())`

This reverts commit 9048c2d.
rdar://146753089
ziqingluo-90 added a commit to swiftlang/llvm-project that referenced this pull request May 7, 2025
… record" (llvm#138951)

The original commit assumes that
`CXXConstructExpr->getType()->getAsRecordDecl()` is always a
`CXXRecordDecl` but it is not true for ObjC programs.

This relanding changes
`cast<CXXRecordDecl>(CXXConstructExpr->getType()->getAsRecordDecl())`
to
`dyn_cast_or_null<CXXRecordDecl>(CXXConstructExpr->getType()->getAsRecordDecl())`

This reverts commit 9048c2d.
rdar://146753089
petrhosek pushed a commit to petrhosek/llvm-project that referenced this pull request May 8, 2025
… record" (llvm#138951)

The original commit assumes that
`CXXConstructExpr->getType()->getAsRecordDecl()` is always a
`CXXRecordDecl` but it is not true for ObjC programs.

This relanding changes
`cast<CXXRecordDecl>(CXXConstructExpr->getType()->getAsRecordDecl())`
to
`dyn_cast_or_null<CXXRecordDecl>(CXXConstructExpr->getType()->getAsRecordDecl())`

This reverts commit 9048c2d.
rdar://146753089
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
skip-precommit-approval PR for CI feedback, not intended for review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants