[Lldb-commits] [PATCH] D119857: [lldb] Don't rely on unsigned integer wrapping in PutRawBytes and PutBytesAsRawHex8

Jonas Devlieghere via Phabricator via lldb-commits lldb-commits at lists.llvm.org
Tue Feb 15 09:36:21 PST 2022


JDevlieghere created this revision.
JDevlieghere added reviewers: labath, kastiglione, mib.
JDevlieghere requested review of this revision.

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.


https://reviews.llvm.org/D119857

Files:
  lldb/source/Utility/Stream.cpp


Index: lldb/source/Utility/Stream.cpp
===================================================================
--- lldb/source/Utility/Stream.cpp
+++ lldb/source/Utility/Stream.cpp
@@ -344,8 +344,8 @@
     for (size_t i = 0; i < src_len; ++i)
       _PutHex8(src[i], false);
   } else {
-    for (size_t i = src_len - 1; i < src_len; --i)
-      _PutHex8(src[i], false);
+    for (size_t i = src_len; i > 0; --i)
+      _PutHex8(src[i - 1], false);
   }
   if (!binary_was_set)
     m_flags.Clear(eBinary);
@@ -357,6 +357,7 @@
                                  ByteOrder src_byte_order,
                                  ByteOrder dst_byte_order) {
   ByteDelta delta(*this);
+
   if (src_byte_order == eByteOrderInvalid)
     src_byte_order = m_byte_order;
 
@@ -370,8 +371,8 @@
     for (size_t i = 0; i < src_len; ++i)
       _PutHex8(src[i], false);
   } else {
-    for (size_t i = src_len - 1; i < src_len; --i)
-      _PutHex8(src[i], false);
+    for (size_t i = src_len; i > 0; --i)
+      _PutHex8(src[i - 1], false);
   }
   if (binary_is_set)
     m_flags.Set(eBinary);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D119857.408923.patch
Type: text/x-patch
Size: 1077 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20220215/b0eeae9e/attachment.bin>


More information about the lldb-commits mailing list