1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-23 03:02:36 +01:00

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

llvm-svn: 340170
This commit is contained in:
Haojian Wu 2018-08-20 13:12:54 +00:00
parent 156400eb42
commit 4473c4ce87

View File

@ -13,7 +13,8 @@ using namespace llvm;
StringRef StringSaver::save(StringRef S) { StringRef StringSaver::save(StringRef S) {
char *P = Alloc.Allocate<char>(S.size() + 1); 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'; P[S.size()] = '\0';
return StringRef(P, S.size()); return StringRef(P, S.size());
} }