From: Chandler Carruth Date: Tue, 4 Aug 2015 03:53:01 +0000 (+0000) Subject: [UB] Guard two calls to memcpy in generated attribute code to handle X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=49ebe63bb6b31fd653ede4a5b8a8c74b082f433a;p=clang [UB] Guard two calls to memcpy in generated attribute code to handle null StringRef objects as inputs. Found by UBSan. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@243949 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/utils/TableGen/ClangAttrEmitter.cpp b/utils/TableGen/ClangAttrEmitter.cpp index d8ecd2b650..14ffbe0115 100644 --- a/utils/TableGen/ClangAttrEmitter.cpp +++ b/utils/TableGen/ClangAttrEmitter.cpp @@ -326,7 +326,8 @@ namespace { OS << " " << getLowerName() << "Length = S.size();\n"; OS << " this->" << getLowerName() << " = new (C, 1) char [" << getLowerName() << "Length];\n"; - OS << " std::memcpy(this->" << getLowerName() << ", S.data(), " + OS << " if (!S.empty())\n"; + OS << " std::memcpy(this->" << getLowerName() << ", S.data(), " << getLowerName() << "Length);\n"; OS << " }"; } @@ -337,7 +338,8 @@ namespace { OS << "A->get" << getUpperName() << "()"; } void writeCtorBody(raw_ostream &OS) const override { - OS << " std::memcpy(" << getLowerName() << ", " << getUpperName() + OS << " if (!" << getUpperName() << ".empty())\n"; + OS << " std::memcpy(" << getLowerName() << ", " << getUpperName() << ".data(), " << getLowerName() << "Length);"; } void writeCtorInitializers(raw_ostream &OS) const override {