[AST] Initialized data after TypeSourceInfo
authorVitaly Buka <vitalybuka@google.com>
Sat, 13 May 2023 00:06:27 +0000 (17:06 -0700)
committerVitaly Buka <vitalybuka@google.com>
Thu, 25 May 2023 16:53:17 +0000 (09:53 -0700)
There is no initialization of the data between allocation
and first getBeginLoc call.

allocation: llvm-project/clang/lib/AST/ASTContext.cpp:3022
use: llvm-project/clang/lib/AST/TypeLoc.cpp:222

Msan report https://reviews.llvm.org/P8306

Reviewed By: thurston

Differential Revision: https://reviews.llvm.org/D150499

clang/include/clang/AST/Type.h
clang/include/clang/AST/TypeLoc.h
clang/lib/AST/ASTContext.cpp

index 7ce1ad9..a95728b 100644 (file)
@@ -6640,7 +6640,7 @@ class alignas(8) TypeSourceInfo {
 
   QualType Ty;
 
-  TypeSourceInfo(QualType ty) : Ty(ty) {}
+  TypeSourceInfo(QualType ty, size_t DataSize); // implemented in TypeLoc.h
 
 public:
   /// Return the type wrapped by this type source info.
index 8bf64dc..27f714b 100644 (file)
@@ -240,6 +240,11 @@ private:
   static SourceRange getLocalSourceRangeImpl(TypeLoc TL);
 };
 
+inline TypeSourceInfo::TypeSourceInfo(QualType ty, size_t DataSize) : Ty(ty) {
+  // Init data attached to the object. See getTypeLoc.
+  memset(this + 1, 0, DataSize);
+}
+
 /// Return the TypeLoc for a type source info.
 inline TypeLoc TypeSourceInfo::getTypeLoc() const {
   // TODO: is this alignment already sufficient?
index 2307c33..7758c30 100644 (file)
@@ -3018,7 +3018,7 @@ TypeSourceInfo *ASTContext::CreateTypeSourceInfo(QualType T,
 
   auto *TInfo =
     (TypeSourceInfo*)BumpAlloc.Allocate(sizeof(TypeSourceInfo) + DataSize, 8);
-  new (TInfo) TypeSourceInfo(T);
+  new (TInfo) TypeSourceInfo(T, DataSize);
   return TInfo;
 }