[PATCH] Warn on mismatching types to sizeof for memset and friends where length is of the form sizeof(Type) * factor.

Oliver Chang ochang at google.com
Tue Apr 7 16:21:54 PDT 2015


clang-format and remove header file dependency from test.


http://reviews.llvm.org/D8881

Files:
  lib/Sema/SemaChecking.cpp
  test/SemaCXX/warn-memset-bad-sizeof.cpp

Index: lib/Sema/SemaChecking.cpp
===================================================================
--- lib/Sema/SemaChecking.cpp
+++ lib/Sema/SemaChecking.cpp
@@ -4707,7 +4707,7 @@
 /// \brief If E is a sizeof expression, returns its argument type.
 static QualType getSizeOfArgType(const Expr *E, bool RequireType = false) {
   if (const UnaryExprOrTypeTraitExpr *SizeOf =
-      dyn_cast<UnaryExprOrTypeTraitExpr>(E))
+          dyn_cast<UnaryExprOrTypeTraitExpr>(E))
     if (SizeOf->getKind() == clang::UETT_SizeOf &&
         (!RequireType || SizeOf->isArgumentType()))
       return SizeOf->getTypeOfArgument();
@@ -4755,20 +4755,19 @@
 static bool isSizeOfArgFactorTypeCompatible(ASTContext &Context,
                                             QualType &SizeOfArgFactorTy,
                                             QualType &EltType) {
-    QualType LHSType =
-        Context.getCanonicalType(SizeOfArgFactorTy).getUnqualifiedType();
-    QualType RHSType =
-        Context.getCanonicalType(EltType).getUnqualifiedType();
+  QualType LHSType =
+      Context.getCanonicalType(SizeOfArgFactorTy).getUnqualifiedType();
+  QualType RHSType = Context.getCanonicalType(EltType).getUnqualifiedType();
 
-    if (LHSType->hasSignedIntegerRepresentation() &&
-        LHSType != Context.getWCharType())
-      LHSType = Context.getCorrespondingUnsignedType(LHSType);
+  if (LHSType->hasSignedIntegerRepresentation() &&
+      LHSType != Context.getWCharType())
+    LHSType = Context.getCorrespondingUnsignedType(LHSType);
 
-    if (RHSType->hasSignedIntegerRepresentation() &&
-        RHSType != Context.getWCharType())
-      RHSType = Context.getCorrespondingUnsignedType(RHSType);
+  if (RHSType->hasSignedIntegerRepresentation() &&
+      RHSType != Context.getWCharType())
+    RHSType = Context.getCorrespondingUnsignedType(RHSType);
 
-    return typesAreCompatibleIgnoreQualifiers(Context, LHSType, RHSType);
+  return typesAreCompatibleIgnoreQualifiers(Context, LHSType, RHSType);
 }
 
 /// \brief Check for dangerous or invalid arguments to memset().
@@ -4804,7 +4803,7 @@
   const Expr *SizeOfArg = getSizeOfExprArg(LenExpr);
 
   QualType SizeOfArgFactorTy;
-  const Expr* OtherSizeOfFactor;
+  const Expr *OtherSizeOfFactor;
   bool SizeOfIsFactor =
       getSizeOfArgTypeProduct(LenExpr, SizeOfArgFactorTy, OtherSizeOfFactor);
 
Index: test/SemaCXX/warn-memset-bad-sizeof.cpp
===================================================================
--- test/SemaCXX/warn-memset-bad-sizeof.cpp
+++ test/SemaCXX/warn-memset-bad-sizeof.cpp
@@ -1,7 +1,7 @@
 // RUN: %clang_cc1 -fsyntax-only -verify -Wno-sizeof-array-argument %s
 //
 
-#include <stddef.h>
+typedef __SIZE_TYPE__ size_t;
 
 extern "C" void *memset(void *, int, unsigned);
 extern "C" void *memmove(void *s1, const void *s2, unsigned n);

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D8881.23379.patch
Type: text/x-patch
Size: 2820 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20150407/9f3ece92/attachment.bin>


More information about the cfe-commits mailing list