[llvm] r340170 - Fix an undefined behavior when storing an empty StringRef.
Haojian Wu via llvm-commits
llvm-commits at lists.llvm.org
Mon Aug 20 06:12:55 PDT 2018
Author: hokein
Date: Mon Aug 20 06:12:54 2018
New Revision: 340170
URL: http://llvm.org/viewvc/llvm-project?rev=340170&view=rev
Log:
Fix an undefined behavior when storing an empty StringRef.
Summary: Passing a nullptr to memcpy is UB.
Reviewers: ioeric
Subscribers: llvm-commits, cfe-commits
Differential Revision: https://reviews.llvm.org/D50966
Modified:
llvm/trunk/lib/Support/StringSaver.cpp
Modified: llvm/trunk/lib/Support/StringSaver.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/StringSaver.cpp?rev=340170&r1=340169&r2=340170&view=diff
==============================================================================
--- llvm/trunk/lib/Support/StringSaver.cpp (original)
+++ llvm/trunk/lib/Support/StringSaver.cpp Mon Aug 20 06:12:54 2018
@@ -13,7 +13,8 @@ using namespace llvm;
StringRef StringSaver::save(StringRef S) {
char *P = Alloc.Allocate<char>(S.size() + 1);
- memcpy(P, S.data(), S.size());
+ if (!S.empty())
+ memcpy(P, S.data(), S.size());
P[S.size()] = '\0';
return StringRef(P, S.size());
}
More information about the llvm-commits
mailing list