Author: Yu Hao
Date: 2026-11-17T07:22:29+05:30
New Revision: 83082a7ad4c5081613c2745d8370b7175438294c

URL: 
https://github.com/llvm/llvm-project/commit/83082a7ad4c5081613c2745d8370b7175438294c
DIFF: 
https://github.com/llvm/llvm-project/commit/83082a7ad4c5081613c2745d8370b7175438294c.diff

LOG: [-Wunsafe-buffer-usage][NFC] Move the cast check for data() call into 
`DataInvocationGadget` (#234814)

`DataInvocationGadget` matches explicit casts of the pointer returned by
`data()`, but whether such a cast is actually unsafe (e.g. a cast to a
pointer to a larger type) was decided by `UnsafeBufferUsageReporter` in
`AnalysisBasedWarnings.cpp`, which silently dropped the safe casts.

Deciding what is unsafe is the job of the analysis, not the reporter: a
matched WarningGadget is supposed to warrant a warning. Move the check
into `DataInvocationGadget`, so the reporter only needs to look up the
invoked method to name it in the warning.

This is a code cleanup; no functional change is intended.

---
*Disclosure: AI assistance was used in the development and
implementation of this change.*

Added: 
    

Modified: 
    clang/lib/Analysis/UnsafeBufferUsage.cpp
    clang/lib/Sema/AnalysisBasedWarnings.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Analysis/UnsafeBufferUsage.cpp 
b/clang/lib/Analysis/UnsafeBufferUsage.cpp
index b68fc2f20dc56f..1eb20c7d88f687 100644
--- a/clang/lib/Analysis/UnsafeBufferUsage.cpp
+++ b/clang/lib/Analysis/UnsafeBufferUsage.cpp
@@ -2405,21 +2405,16 @@ class DataInvocationGadget : public WarningGadget {
     auto *CE = dyn_cast<ExplicitCastExpr>(S);
     if (!CE)
       return false;
-    for (auto *Child : CE->children()) {
-      if (auto *MCE = dyn_cast<CXXMemberCallExpr>(Child);
-          MCE && isDataFunction(MCE)) {
-        Result.addNode(OpTag, DynTypedNode::create(*CE));
-        return true;
-      }
-      if (auto *Paren = dyn_cast<ParenExpr>(Child)) {
-        if (auto *MCE = dyn_cast<CXXMemberCallExpr>(Paren->getSubExpr());
-            MCE && isDataFunction(MCE)) {
-          Result.addNode(OpTag, DynTypedNode::create(*CE));
-          return true;
-        }
-      }
-    }
-    return false;
+    const Expr *Operand = CE->getSubExpr();
+    if (auto *Paren = dyn_cast<ParenExpr>(Operand))
+      Operand = Paren->getSubExpr();
+    auto *MCE = dyn_cast<CXXMemberCallExpr>(Operand);
+    if (!MCE || !isDataFunction(MCE))
+      return false;
+    if (!isUnsafePointerCast(CE, Ctx))
+      return false;
+    Result.addNode(OpTag, DynTypedNode::create(*CE));
+    return true;
   }
 
   void handleUnsafeOperation(UnsafeBufferUsageHandler &Handler,
@@ -2447,6 +2442,33 @@ class DataInvocationGadget : public WarningGadget {
     return false;
   }
 
+  // Returns true if `CE` casts the pointer returned by `data()` to a pointer 
to
+  // a larger type, or if the size of either pointee type cannot be determined.
+  static bool isUnsafePointerCast(const ExplicitCastExpr *CE,
+                                  const ASTContext &Ctx) {
+    QualType DestType = CE->getType();
+    if (!isa<PointerType>(DestType))
+      return false;
+    QualType DestPointeeType = DestType->getPointeeType();
+
+    // If destination type is incomplete or dependent, it is unsafe to cast
+    // to anyway, no need to check its type:
+    if (DestPointeeType->isIncompleteType() ||
+        DestPointeeType->isDependentType())
+      return true;
+
+    QualType SrcType = CE->getSubExpr()->getType();
+    if (!SrcType->isPointerType())
+      return false;
+    QualType SrcPointeeType = SrcType->getPointeeType();
+
+    // Check if source type is incomplete or dependent as well:
+    if (SrcPointeeType->isIncompleteType() || 
SrcPointeeType->isDependentType())
+      return true;
+
+    return Ctx.getTypeSize(SrcPointeeType) < Ctx.getTypeSize(DestPointeeType);
+  }
+
   SmallVector<const Expr *, 1> getUnsafePtrs() const override { return {}; }
 };
 

diff  --git a/clang/lib/Sema/AnalysisBasedWarnings.cpp 
b/clang/lib/Sema/AnalysisBasedWarnings.cpp
index 308051b1d8ba3e..151167aa461d5e 100644
--- a/clang/lib/Sema/AnalysisBasedWarnings.cpp
+++ b/clang/lib/Sema/AnalysisBasedWarnings.cpp
@@ -2582,31 +2582,6 @@ class UnsafeBufferUsageReporter : public 
UnsafeBufferUsageHandler {
         D = ME->getMemberDecl();
         MsgParam = 5;
       } else if (const auto *ECE = dyn_cast<ExplicitCastExpr>(Operation)) {
-        QualType destType = ECE->getType();
-
-        if (!isa<PointerType>(destType))
-          return;
-        destType = destType.getTypePtr()->getPointeeType();
-
-        // If destination type is incomplete or dependent, it is unsafe to cast
-        // to anyway, no need to check its type:
-        if (!destType->isIncompleteType() && !destType->isDependentType()) {
-          const uint64_t dSize = Ctx.getTypeSize(destType);
-          QualType srcType = ECE->getSubExpr()->getType();
-
-          assert(srcType->isPointerType());
-
-          QualType srcPointeeType = srcType.getTypePtr()->getPointeeType();
-
-          // Check if source type is incomplete or dependent as well:
-          if (!srcPointeeType->isIncompleteType() &&
-              !srcPointeeType->isDependentType()) {
-            const uint64_t sSize = Ctx.getTypeSize(srcPointeeType);
-
-            if (sSize >= dSize)
-              return;
-          }
-        }
         if (const auto *CE = dyn_cast<CXXMemberCallExpr>(
                 ECE->getSubExpr()->IgnoreParens())) {
           D = CE->getMethodDecl();


        
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to