[llvm] [orc-rt] Fix WrapperFunctionBuffer empty range construction (PR #209751)

Lang Hames via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 15 05:38:16 PDT 2026


https://github.com/lhames created https://github.com/llvm/llvm-project/pull/209751

orc_rt_CreateWrapperFunctionBufferFromRange(const char *, size_t) left the constructed buffer's data member uninitialized when the size argument was zero. This could result in the returned buffer having a non-null data field and a zero size field, which is a (malformed) out-of-band error value, not an empty buffer.

Update orc_rt_CreateWrapperFunctionBufferFromRange to zero-initialize the data field so that the size == 0 case yields a correctly formed empty buffer.

>From 9e7a6f55cff1a4bbcdcd8d951eb0bc4330cf5148 Mon Sep 17 00:00:00 2001
From: Lang Hames <lhames at gmail.com>
Date: Wed, 15 Jul 2026 22:18:58 +1000
Subject: [PATCH] [orc-rt] Fix WrapperFunctionBuffer empty range construction

orc_rt_CreateWrapperFunctionBufferFromRange(const char *, size_t) left
the constructed buffer's data member uninitialized when the size
argument was zero. This could result in the returned buffer having a
non-null data field and a zero size field, which is a (malformed)
out-of-band error value, not an empty buffer.

Update orc_rt_CreateWrapperFunctionBufferFromRange to zero-initialize
the data field so that the size == 0 case yields a correctly formed
empty buffer.
---
 orc-rt/include/orc-rt-c/WrapperFunction.h      | 4 +++-
 orc-rt/test/unit/WrapperFunctionBufferTest.cpp | 6 ++++++
 2 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/orc-rt/include/orc-rt-c/WrapperFunction.h b/orc-rt/include/orc-rt-c/WrapperFunction.h
index aa5f50fae87f5..d1159eb12d157 100644
--- a/orc-rt/include/orc-rt-c/WrapperFunction.h
+++ b/orc-rt/include/orc-rt-c/WrapperFunction.h
@@ -101,11 +101,13 @@ static inline orc_rt_WrapperFunctionBuffer
 orc_rt_CreateWrapperFunctionBufferFromRange(const char *Data, size_t Size) {
   orc_rt_WrapperFunctionBuffer B;
   B.Size = Size;
+  // If Size is 0 ValuePtr must be 0 or it is considered an out-of-band error.
+  B.Data.ValuePtr = 0;
   if (B.Size > sizeof(B.Data.Value)) {
     char *Tmp = (char *)malloc(Size);
     memcpy(Tmp, Data, Size);
     B.Data.ValuePtr = Tmp;
-  } else
+  } else if (Size != 0)
     memcpy(B.Data.Value, Data, Size);
   return B;
 }
diff --git a/orc-rt/test/unit/WrapperFunctionBufferTest.cpp b/orc-rt/test/unit/WrapperFunctionBufferTest.cpp
index 912022c7c6683..17824ada3e35d 100644
--- a/orc-rt/test/unit/WrapperFunctionBufferTest.cpp
+++ b/orc-rt/test/unit/WrapperFunctionBufferTest.cpp
@@ -45,6 +45,12 @@ TEST(WrapperFunctionUtilsTest, WrapperFunctionBufferFromRange) {
   EXPECT_EQ(B.getOutOfBandError(), nullptr);
 }
 
+TEST(WrapperFunctionUtilsTest, WrapperFunctionBufferFromEmptyRangeIsEmpty) {
+  const char *Empty = "";
+  auto B = WrapperFunctionBuffer::copyFrom(Empty, 0);
+  EXPECT_TRUE(B.empty());
+}
+
 TEST(WrapperFunctionUtilsTest, WrapperFunctionBufferFromCString) {
   auto B = WrapperFunctionBuffer::copyFrom(TestString);
   EXPECT_EQ(B.size(), strlen(TestString) + 1);



More information about the llvm-commits mailing list