This is an archive of the discontinued LLVM Phabricator instance.

[lldb] Don't rely on unsigned integer wrapping in PutRawBytes and PutBytesAsRawHex8
ClosedPublic

Authored by JDevlieghere on Feb 15 2022, 9:36 AM.

Details

Summary

I was looking at Stream::PutRawBytes and thought I spotted a bug because both looks are using i < src_len as the loop condition despite them iterating "in different directions". On closer inspection, the existing code is correct, because it relies on well-defined unsigned integer wrapping of size_t. Correct doesn't mean readable, so this patch changes the loop condition to compare against 0 when decrementing i while still covering the edge case of src_len potentially being 0 itself.

Diff Detail

Event Timeline

JDevlieghere requested review of this revision.Feb 15 2022, 9:36 AM
JDevlieghere created this revision.
JDevlieghere added inline comments.
lldb/source/Utility/Stream.cpp
360

Intentional newline for consistency with Stream::PutRawBytes above.

mib accepted this revision.Feb 15 2022, 3:19 PM

LGTM

This revision is now accepted and ready to land.Feb 15 2022, 3:19 PM
shafik added a subscriber: shafik.Feb 15 2022, 4:55 PM

Do we have a test that covers this edge case?

  • Add unit test for edge case
This revision was landed with ongoing or failed builds.Feb 15 2022, 8:38 PM
This revision was automatically updated to reflect the committed changes.
Herald added a project: Restricted Project. · View Herald TranscriptFeb 15 2022, 8:38 PM

Another option would be to go full-STL and do something like

ArrayRef<uint8_t> data(ptr, len);
if (reverse)
  for (uint8_t byte: llvm::reverse(data))
...