aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Support/MemoryBuffer.cpp
diff options
context:
space:
mode:
authorKadir Cetinkaya <kadircet@google.com>2023-02-24 09:36:07 +0100
committerKadir Cetinkaya <kadircet@google.com>2023-03-14 12:58:37 +0100
commit710983ab540fa372e846d47e9fa80b7d8b96afb1 (patch)
treeca5dc5c97979f4537d36e5305acf34a7da774d8f /llvm/lib/Support/MemoryBuffer.cpp
parent2ebbcfa07edd9f16bf28cbf1deaaa302503fd330 (diff)
downloadllvm-710983ab540fa372e846d47e9fa80b7d8b96afb1.zip
llvm-710983ab540fa372e846d47e9fa80b7d8b96afb1.tar.gz
llvm-710983ab540fa372e846d47e9fa80b7d8b96afb1.tar.bz2
[Support][MemBuffer] Prevent UB on empty StringRefs
Empty StringRefs are usually identified by their length being zero, and sometimes they'll have Data==nullptr (e.g. default constructed, or derived from an operation like split/copy and result turned out to be empty). If such StringRef objects are passed to llvm::MemoryBuffer::getMemBufferCopy, it'll result in UB as neither src nor dst can be null, even if size is zero. This patch prevents that UB by not issuing a copy whenever StringRef is empty. Differential Revision: https://reviews.llvm.org/D144706
Diffstat (limited to 'llvm/lib/Support/MemoryBuffer.cpp')
-rw-r--r--llvm/lib/Support/MemoryBuffer.cpp9
1 files changed, 7 insertions, 2 deletions
diff --git a/llvm/lib/Support/MemoryBuffer.cpp b/llvm/lib/Support/MemoryBuffer.cpp
index 0bb1172..4cc4fe0 100644
--- a/llvm/lib/Support/MemoryBuffer.cpp
+++ b/llvm/lib/Support/MemoryBuffer.cpp
@@ -11,6 +11,7 @@
//===----------------------------------------------------------------------===//
#include "llvm/Support/MemoryBuffer.h"
+#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/Config/config.h"
#include "llvm/Support/Alignment.h"
@@ -22,6 +23,7 @@
#include "llvm/Support/Process.h"
#include "llvm/Support/Program.h"
#include "llvm/Support/SmallVectorMemoryBuffer.h"
+#include <algorithm>
#include <cassert>
#include <cstring>
#include <new>
@@ -132,10 +134,13 @@ MemoryBuffer::getMemBuffer(MemoryBufferRef Ref, bool RequiresNullTerminator) {
static ErrorOr<std::unique_ptr<WritableMemoryBuffer>>
getMemBufferCopyImpl(StringRef InputData, const Twine &BufferName) {
- auto Buf = WritableMemoryBuffer::getNewUninitMemBuffer(InputData.size(), BufferName);
+ auto Buf =
+ WritableMemoryBuffer::getNewUninitMemBuffer(InputData.size(), BufferName);
if (!Buf)
return make_error_code(errc::not_enough_memory);
- memcpy(Buf->getBufferStart(), InputData.data(), InputData.size());
+ // Calling memcpy with null src/dst is UB, and an empty StringRef is
+ // represented with {nullptr, 0}.
+ llvm::copy(InputData, Buf->getBufferStart());
return std::move(Buf);
}