From 54829bb3ff750cf8061fcd82db46f3596fa9cbac Mon Sep 17 00:00:00 2001 From: Haojian Wu Date: Mon, 20 Aug 2018 13:12:54 +0000 Subject: [PATCH] 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 --- llvm/lib/Support/StringSaver.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/llvm/lib/Support/StringSaver.cpp b/llvm/lib/Support/StringSaver.cpp index 1ded2bdb09de..bf0ac8de9821 100644 --- a/llvm/lib/Support/StringSaver.cpp +++ b/llvm/lib/Support/StringSaver.cpp @@ -13,7 +13,8 @@ using namespace llvm; StringRef StringSaver::save(StringRef S) { char *P = Alloc.Allocate(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()); }