[Mlir-commits] [mlir] 9312cb6 - Fix Undefined Behavior in MLIR Diagnostic: don't call memcpy with a nullptr source

Mehdi Amini llvmlistbot at llvm.org
Sat Oct 2 15:12:29 PDT 2021


Author: Mehdi Amini
Date: 2021-10-02T21:32:20Z
New Revision: 9312cb6f2092f73fdc4b288cbbd29129fc83c386

URL: https://github.com/llvm/llvm-project/commit/9312cb6f2092f73fdc4b288cbbd29129fc83c386
DIFF: https://github.com/llvm/llvm-project/commit/9312cb6f2092f73fdc4b288cbbd29129fc83c386.diff

LOG: Fix Undefined Behavior in MLIR Diagnostic: don't call memcpy with a nullptr source

This happens when streaming an empty Twine as part of a diagnostic.

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

Added: 
    

Modified: 
    mlir/lib/IR/Diagnostics.cpp

Removed: 
    


################################################################################
diff  --git a/mlir/lib/IR/Diagnostics.cpp b/mlir/lib/IR/Diagnostics.cpp
index 622fe119f325a..339f5607abdb3 100644
--- a/mlir/lib/IR/Diagnostics.cpp
+++ b/mlir/lib/IR/Diagnostics.cpp
@@ -89,9 +89,11 @@ static StringRef twineToStrRef(const Twine &val,
   // Allocate memory to hold this string.
   SmallString<64> data;
   auto strRef = val.toStringRef(data);
+  if (strRef.empty())
+    return strRef;
+
   strings.push_back(std::unique_ptr<char[]>(new char[strRef.size()]));
   memcpy(&strings.back()[0], strRef.data(), strRef.size());
-
   // Return a reference to the new string.
   return StringRef(&strings.back()[0], strRef.size());
 }


        


More information about the Mlir-commits mailing list