This is an archive of the discontinued LLVM Phabricator instance.

[NFC][LLVM-MT] Fix buffer-overflow when comparing buffers
AbandonedPublic

Authored by jmmartinez on Dec 5 2022, 5:29 AM.

Details

Reviewers
abrachet

Diff Detail

Event Timeline

jmmartinez created this revision.Dec 5 2022, 5:29 AM
Herald added a project: Restricted Project. · View Herald TranscriptDec 5 2022, 5:29 AM
jmmartinez requested review of this revision.Dec 5 2022, 5:29 AM
Herald added a project: Restricted Project. · View Herald TranscriptDec 5 2022, 5:29 AM

I encounter this issue when running the LLVM test suite with the address sanitizer enabled.

abrachet added inline comments.Dec 5 2022, 2:03 PM
llvm/tools/llvm-mt/llvm-mt.cpp
153–156

That should work too. WDYT?

jmmartinez added inline comments.Dec 6 2022, 1:48 AM
llvm/tools/llvm-mt/llvm-mt.cpp
153–156

I didn't know about that overload. I'll change it use your version.

jmmartinez updated this revision to Diff 480399.Dec 6 2022, 2:17 AM

Updated to use 4 iterator version of std::equals

jmmartinez updated this revision to Diff 480402.Dec 6 2022, 2:31 AM

In fact, bring back the size comparison to speed up the comparison in the case the sizes do not match

jmmartinez marked an inline comment as done.Dec 6 2022, 2:31 AM
abrachet added inline comments.Dec 6 2022, 1:43 PM
llvm/tools/llvm-mt/llvm-mt.cpp
153–156

Ah, sorry I didn't mean to keep SameSize in my suggested change. Just this should be sufficient

Same = std::equal(OutputBuffer->getBufferStart(),
                                    OutputBuffer->getBufferEnd(),
                                    FileBuffer->getBufferStart());
                                    FileBuffer->getBufferStart(),
                                    FileBuffer->getBufferEnd());
jmmartinez added inline comments.Dec 7 2022, 12:21 AM
llvm/tools/llvm-mt/llvm-mt.cpp
153–156

I know, but checking the size first gives a shortcut in case the sizes are different (instead of scanning for a difference). Do you think we can keep it in this case ?

jmmartinez added inline comments.Dec 7 2022, 4:39 AM
llvm/tools/llvm-mt/llvm-mt.cpp
153–156

I just realized that with the right iterator type, std::equals computes the distance and does the shortcut on its own. I'm removing the size comparison.

jmmartinez updated this revision to Diff 480860.Dec 7 2022, 4:52 AM

Remove size comparison

jmmartinez marked an inline comment as done.Dec 7 2022, 4:57 AM
jmmartinez abandoned this revision.Dec 12 2022, 1:10 AM

Duplicated with https://reviews.llvm.org/D139457 (which got merged).