]> granicus.if.org Git - clang/commitdiff
[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.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@243949 91177308-0d34-0410-b5e6-96231b3b80d8

utils/TableGen/ClangAttrEmitter.cpp

index d8ecd2b65005484798c3b897359567416aaec707..14ffbe0115a8cc0c6982a1b738f64f530f9b881c 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 {