[UB] Guard two calls to memcpy in generated attribute code to handle
authorChandler Carruth <chandlerc@gmail.com>
Tue, 4 Aug 2015 03:53:01 +0000 (03:53 +0000)
committerChandler Carruth <chandlerc@gmail.com>
Tue, 4 Aug 2015 03:53:01 +0000 (03:53 +0000)
null StringRef objects as inputs.

Found by UBSan.

llvm-svn: 243949

clang/utils/TableGen/ClangAttrEmitter.cpp

index d8ecd2b..14ffbe0 100644 (file)
@@ -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 {