This is an archive of the discontinued LLVM Phabricator instance.

[llvm/Support] Don't crash on empty nullptr ranges when decoding LEBs
ClosedPublic

Authored by labath on Apr 2 2020, 6:44 AM.

Details

Summary

If the decoding functions are called with both start and end pointers
being nullptr, the function will crash due to a nullptr dereference.
This happens because the function does not recognise nullptr as a valid
end pointer.

Obviously, nobody is going to pass null pointers here deliberately, but
it can happen indirectly (as it did for me), when calling these
functions on an ArrayRef, as a default-initialized empty ArrayRef will
have both begin() and end() pointers equal to nullptr.

The fix is to simply remove the nullptr check. Passing nullptr for "end"
with a valid "begin" pointer will still work, as one cannot reach
nullptr by incrementing a valid pointer without triggerring UB.

Diff Detail

Event Timeline

labath created this revision.Apr 2 2020, 6:44 AM
Herald added a project: Restricted Project. · View Herald TranscriptApr 2 2020, 6:44 AM
labath updated this revision to Diff 254510.Apr 2 2020, 6:47 AM

Upload the diff properly

Harbormaster completed remote builds in B51475: Diff 254510.
dblaikie accepted this revision.Apr 2 2020, 4:02 PM
dblaikie added inline comments.
llvm/include/llvm/Support/LEB128.h
137

Is it possible this functionality originally existed for accepting unbounded buffers? (like the classic unix gets function) by passing nullptr as the end bound it'd just keep reading from the buffer until it decoded a ULEB128?

While such functionality would be pretty problematic from a memory safety perspective - worth checking if any users might be relying on that? I guess it's more likely this was just copied in from somewhere else/some example implementation and retained the "Bonus" functionality without it being intentional, an if all the tests pass I'm fine removing this quirk/feature.

This revision is now accepted and ready to land.Apr 2 2020, 4:02 PM
labath marked 2 inline comments as done.Apr 6 2020, 4:39 AM
labath added inline comments.
llvm/include/llvm/Support/LEB128.h
137

Sounds good. I'll try to create a patch for that.

This revision was automatically updated to reflect the committed changes.
labath marked an inline comment as done.