Skip to content

Commit 94e6d26

Browse files
committed
[ORC] Fix serialization / deserialization of default-constructed StringRef.
Avoids accessing the data field on zero-length strings. This is the StringRef counterpart to the ArrayRef<char> fix in 67220c2. rdar://97285294
1 parent b8d54d1 commit 94e6d26

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

llvm/include/llvm/ExecutionEngine/Orc/Shared/SimplePackedSerialization.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,8 @@ template <> class SPSSerializationTraits<SPSString, StringRef> {
479479
static bool serialize(SPSOutputBuffer &OB, StringRef S) {
480480
if (!SPSArgList<uint64_t>::serialize(OB, static_cast<uint64_t>(S.size())))
481481
return false;
482+
if (S.empty()) // Empty StringRef may have null data, so bail out early.
483+
return true;
482484
return OB.write(S.data(), S.size());
483485
}
484486

@@ -490,7 +492,7 @@ template <> class SPSSerializationTraits<SPSString, StringRef> {
490492
Data = IB.data();
491493
if (!IB.skip(Size))
492494
return false;
493-
S = StringRef(Data, Size);
495+
S = StringRef(Size ? Data : nullptr, Size);
494496
return true;
495497
}
496498
};

llvm/unittests/ExecutionEngine/Orc/SimplePackedSerializationTest.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,3 +208,24 @@ TEST(SimplePackedSerializationTest, ArrayRefEmpty) {
208208
EXPECT_EQ(AIn.data(), nullptr);
209209
EXPECT_EQ(AIn.size(), 0U);
210210
}
211+
212+
TEST(SimplePackedSerializationTest, StringRefEmpty) {
213+
// Make sure that empty StringRefs serialize and deserialize as expected.
214+
// Empty StringRefs should not succeed even when the data field is null, and
215+
// should deserialize to a default-constructed StringRef, not a pointer into
216+
// the stream.
217+
constexpr unsigned BufferSize = sizeof(uint64_t);
218+
char Buffer[BufferSize];
219+
memset(Buffer, 0, BufferSize);
220+
221+
StringRef SROut;
222+
SPSOutputBuffer OB(Buffer, BufferSize);
223+
EXPECT_TRUE(SPSArgList<SPSSequence<char>>::serialize(OB, SROut));
224+
225+
StringRef SRIn;
226+
SPSInputBuffer IB(Buffer, BufferSize);
227+
EXPECT_TRUE(SPSArgList<SPSSequence<char>>::deserialize(IB, SRIn));
228+
229+
EXPECT_EQ(SRIn.data(), nullptr);
230+
EXPECT_EQ(SRIn.size(), 0U);
231+
}

0 commit comments

Comments
 (0)