Skip to content

Commit 9312cb6

Browse files
committed
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
1 parent 57d9ade commit 9312cb6

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

mlir/lib/IR/Diagnostics.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,11 @@ static StringRef twineToStrRef(const Twine &val,
8989
// Allocate memory to hold this string.
9090
SmallString<64> data;
9191
auto strRef = val.toStringRef(data);
92+
if (strRef.empty())
93+
return strRef;
94+
9295
strings.push_back(std::unique_ptr<char[]>(new char[strRef.size()]));
9396
memcpy(&strings.back()[0], strRef.data(), strRef.size());
94-
9597
// Return a reference to the new string.
9698
return StringRef(&strings.back()[0], strRef.size());
9799
}

0 commit comments

Comments
 (0)