[llvm-branch-commits] [llvm] release/22.x: [MC] Try to fix ubsan bot (PR #177907)

via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Mon Jan 26 00:29:58 PST 2026


https://github.com/llvmbot created https://github.com/llvm/llvm-project/pull/177907

Backport d064f395af7ac226dec3f8e90516a26e96e2acf1

Requested by: @nikic

>From e6fda14f954eeb3a06f6b684742db21935bc1b74 Mon Sep 17 00:00:00 2001
From: Nikita Popov <nikita.ppv at gmail.com>
Date: Fri, 23 Jan 2026 22:10:09 +0100
Subject: [PATCH] [MC] Try to fix ubsan bot

Check that the size is non-zero to make sure we don't call
memcpy with null pointers. This is well-defined now, but ubsan
may still warn about it.

(cherry picked from commit d064f395af7ac226dec3f8e90516a26e96e2acf1)
---
 llvm/lib/MC/MCObjectStreamer.cpp | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/MC/MCObjectStreamer.cpp b/llvm/lib/MC/MCObjectStreamer.cpp
index 5fd30eccb45c5..0c64d89d7b491 100644
--- a/llvm/lib/MC/MCObjectStreamer.cpp
+++ b/llvm/lib/MC/MCObjectStreamer.cpp
@@ -111,7 +111,8 @@ void MCObjectStreamer::appendContents(ArrayRef<char> Contents) {
   assert(FragSpace >= Contents.size());
   // As this is performance-sensitive code, explicitly use std::memcpy.
   // Optimization of std::copy to memmove is unreliable.
-  std::memcpy(getCurFragEnd(), Contents.begin(), Contents.size());
+  if (!Contents.empty())
+    std::memcpy(getCurFragEnd(), Contents.begin(), Contents.size());
   CurFrag->FixedSize += Contents.size();
   FragSpace -= Contents.size();
 }



More information about the llvm-branch-commits mailing list