This is an archive of the discontinued LLVM Phabricator instance.

[Support][MemBuffer] Prevent UB on empty StringRefs
ClosedPublic

Authored by kadircet on Feb 24 2023, 12:44 AM.

Details

Summary

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.

Diff Detail

Event Timeline

kadircet created this revision.Feb 24 2023, 12:44 AM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 24 2023, 12:44 AM
Herald added a subscriber: hiraditya. · View Herald Transcript
kadircet requested review of this revision.Feb 24 2023, 12:44 AM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 24 2023, 12:44 AM
vitalybuka accepted this revision.Feb 24 2023, 1:31 AM
This revision is now accepted and ready to land.Feb 24 2023, 1:31 AM

adding Chandler, as he's the code owner here. I'll let this patch sit for a couple (business) days to make sure people with more (historical) context can make comments

sammccall added inline comments.
llvm/lib/Support/MemoryBuffer.cpp
141–143

instead of the check + comment, you can use std::copy which doesn't have this deficiency

kadircet added inline comments.Feb 24 2023, 2:34 AM
llvm/lib/Support/MemoryBuffer.cpp
141–143

thanks that makes sense, i'd be happy to change it to use std::copy if there are no objections anytime soon

kadircet updated this revision to Diff 505010.Mar 14 2023, 3:30 AM
kadircet marked an inline comment as done.

Use std::copy

This revision was landed with ongoing or failed builds.Mar 14 2023, 4:59 AM
This revision was automatically updated to reflect the committed changes.