[all-commits] [llvm/llvm-project] 710983: [Support][MemBuffer] Prevent UB on empty StringRefs

kadir çetinkaya via All-commits all-commits at lists.llvm.org
Tue Mar 14 04:59:10 PDT 2023


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: 710983ab540fa372e846d47e9fa80b7d8b96afb1
      https://github.com/llvm/llvm-project/commit/710983ab540fa372e846d47e9fa80b7d8b96afb1
  Author: Kadir Cetinkaya <kadircet at google.com>
  Date:   2023-03-14 (Tue, 14 Mar 2023)

  Changed paths:
    M llvm/lib/Support/MemoryBuffer.cpp
    M llvm/unittests/Support/MemoryBufferTest.cpp

  Log Message:
  -----------
  [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




More information about the All-commits mailing list