[Lldb-commits] [lldb] [lldb] Adjust the for loop condition to prevent unintended increments in ExpandRLE (NFC) (PR #94844)

Shivam Gupta via lldb-commits lldb-commits at lists.llvm.org
Thu Jul 25 06:59:31 PDT 2024


https://github.com/xgupta updated https://github.com/llvm/llvm-project/pull/94844

>From 5fe77213524d05581eca70b8a0d25e03fe8df793 Mon Sep 17 00:00:00 2001
From: Shivam Gupta <shivam98.tkg at gmail.com>
Date: Sat, 8 Jun 2024 11:52:08 +0530
Subject: [PATCH 1/2] [lldb] Adjust the for loop condition to prevent
 unintended increments in ExpandRLE (NFC)

Address the issue reported by static analyser cppcheck regarding missing bounds check for extra iterator increment in a loop.
This could lead to accessing out-of-bounds memory. To fix this we have adjusted the loop conditions to not incrementing iterator c there..

Caught by cppcheck -
lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp:1300:75: warning: Missing bounds check for extra iterator increment in loop. [StlMissingComparison]

Fix #91225
---
 .../Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp      | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp b/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp
index 8a47eed3d7cbe..81644d6248a83 100644
--- a/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp
+++ b/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp
@@ -1297,7 +1297,7 @@ std::string GDBRemoteCommunication::ExpandRLE(std::string packet) {
   // Reserve enough byte for the most common case (no RLE used).
   std::string decoded;
   decoded.reserve(packet.size());
-  for (std::string::const_iterator c = packet.begin(); c != packet.end(); ++c) {
+  for (std::string::const_iterator c = packet.begin(); c != packet.end();) {
     if (*c == '*') {
       // '*' indicates RLE. Next character will give us the repeat count and
       // previous character is what is to be repeated.
@@ -1316,6 +1316,7 @@ std::string GDBRemoteCommunication::ExpandRLE(std::string packet) {
     } else {
       decoded.push_back(*c);
     }
+    c++;
   }
   return decoded;
 }

>From b655f644e4744c98087ab0c8aee8bc38b3502c70 Mon Sep 17 00:00:00 2001
From: xgupta <shivma98.tkg at gmail.com>
Date: Thu, 25 Jul 2024 15:59:17 +0200
Subject: [PATCH 2/2] address review comment

---
 .../Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp       | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp b/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp
index 81644d6248a83..926310fa22328 100644
--- a/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp
+++ b/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp
@@ -1298,7 +1298,7 @@ std::string GDBRemoteCommunication::ExpandRLE(std::string packet) {
   std::string decoded;
   decoded.reserve(packet.size());
   for (std::string::const_iterator c = packet.begin(); c != packet.end();) {
-    if (*c == '*') {
+    if (*c == '*' && std::next(c) != packet.end()) {
       // '*' indicates RLE. Next character will give us the repeat count and
       // previous character is what is to be repeated.
       char char_to_repeat = decoded.back();



More information about the lldb-commits mailing list