[clang] [-Wunsafe-buffer-usage] Fix the crash introduced by the unsafe invocation of span::data warning (PR #78815)

Ziqing Luo via cfe-commits cfe-commits at lists.llvm.org
Fri Jan 19 16:57:48 PST 2024


================
@@ -2263,15 +2263,27 @@ class UnsafeBufferUsageReporter : public UnsafeBufferUsageHandler {
         MsgParam = 3;
       } else if (const auto *ECE = dyn_cast<ExplicitCastExpr>(Operation)) {
         QualType destType = ECE->getType();
-        const uint64_t dSize =
-            Ctx.getTypeSize(destType.getTypePtr()->getPointeeType());
-        if (const auto *CE = dyn_cast<CXXMemberCallExpr>(ECE->getSubExpr())) {
-          QualType srcType = CE->getType();
-          const uint64_t sSize =
-              Ctx.getTypeSize(srcType.getTypePtr()->getPointeeType());
-          if (sSize >= dSize)
+        if (!isa<PointerType>(destType))
+          return;
+
+        const Expr *subExpr = ECE->getSubExpr();
+        // Check if related to DataInvocation warning gadget.
----------------
ziqingluo-90 wrote:

nitpick: not sure if we need this check.  `DataInvocation` warning gadget is the only possible explicit-cast kind `Operation` here.    This is completely my personal taste: this function sort of implies unique correspondence between warning gadgets and `Operation` kinds if without the check.

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


More information about the cfe-commits mailing list