[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
Fri Jun 7 23:30:32 PDT 2024


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

This PR 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

>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] [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;
 }



More information about the lldb-commits mailing list