borneoa created this revision.
borneoa requested review of this revision.
Herald added a project: LLDB.
Herald added a subscriber: lldb-commits.

In case of receiving, in a single packet, some junk byte followed
by a complete gdb-remote reply, e.g. "junk+$OK#9a", the current
implementation of CheckForPacket() drops the junk bytes and
returns.
The caller will call again CheckForPacket() only if it receives
another packet. But the reply is already complete and the remote
will not send anything else, so the caller will timeout.

Check for junk bytes and drop them, then decode the packet.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D116006

Files:
  lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp


Index: lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp
===================================================================
--- lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp
+++ lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp
@@ -647,6 +647,34 @@
       }
     }
 
+    // Check if we have an unexpected byte and we need to flush all bad data
+    // that is in m_bytes, so we need to find the first byte that is a '+'
+    // (ACK), '-' (NACK), \x03 (CTRL+C interrupt), or '$' character (start of
+    // packet header) or of course, the end of the data in m_bytes...
+    const size_t bytes_len = m_bytes.size();
+    bool done = false;
+    uint32_t idx;
+    for (idx = 0; !done && idx < bytes_len;) {
+      switch (m_bytes[idx]) {
+      case '+':
+      case '-':
+      case '\x03':
+      case '%':
+      case '$':
+        done = true;
+        break;
+
+      default:
+        ++idx;
+        break;
+      }
+    }
+    if (idx) {
+      LLDB_LOGF(log, "GDBRemoteCommunication::%s tossing %u junk bytes: 
'%.*s'",
+                __FUNCTION__, idx, idx, m_bytes.c_str());
+      m_bytes.erase(0, idx);
+    }
+
     switch (m_bytes[0]) {
     case '+':                            // Look for ack
     case '-':                            // Look for cancel
@@ -681,33 +709,9 @@
       }
       break;
 
-    default: {
-      // We have an unexpected byte and we need to flush all bad data that is
-      // in m_bytes, so we need to find the first byte that is a '+' (ACK), '-'
-      // (NACK), \x03 (CTRL+C interrupt), or '$' character (start of packet
-      // header) or of course, the end of the data in m_bytes...
-      const size_t bytes_len = m_bytes.size();
-      bool done = false;
-      uint32_t idx;
-      for (idx = 1; !done && idx < bytes_len;) {
-        switch (m_bytes[idx]) {
-        case '+':
-        case '-':
-        case '\x03':
-        case '%':
-        case '$':
-          done = true;
-          break;
-
-        default:
-           ++idx;
-          break;
-        }
-      }
-      LLDB_LOGF(log, "GDBRemoteCommunication::%s tossing %u junk bytes: 
'%.*s'",
-                __FUNCTION__, idx, idx, m_bytes.c_str());
-      m_bytes.erase(0, idx);
-    } break;
+    default:
+      // no more bytes after junk
+      break;
     }
 
     if (content_length == std::string::npos) {


Index: lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp
===================================================================
--- lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp
+++ lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp
@@ -647,6 +647,34 @@
       }
     }
 
+    // Check if we have an unexpected byte and we need to flush all bad data
+    // that is in m_bytes, so we need to find the first byte that is a '+'
+    // (ACK), '-' (NACK), \x03 (CTRL+C interrupt), or '$' character (start of
+    // packet header) or of course, the end of the data in m_bytes...
+    const size_t bytes_len = m_bytes.size();
+    bool done = false;
+    uint32_t idx;
+    for (idx = 0; !done && idx < bytes_len;) {
+      switch (m_bytes[idx]) {
+      case '+':
+      case '-':
+      case '\x03':
+      case '%':
+      case '$':
+        done = true;
+        break;
+
+      default:
+        ++idx;
+        break;
+      }
+    }
+    if (idx) {
+      LLDB_LOGF(log, "GDBRemoteCommunication::%s tossing %u junk bytes: '%.*s'",
+                __FUNCTION__, idx, idx, m_bytes.c_str());
+      m_bytes.erase(0, idx);
+    }
+
     switch (m_bytes[0]) {
     case '+':                            // Look for ack
     case '-':                            // Look for cancel
@@ -681,33 +709,9 @@
       }
       break;
 
-    default: {
-      // We have an unexpected byte and we need to flush all bad data that is
-      // in m_bytes, so we need to find the first byte that is a '+' (ACK), '-'
-      // (NACK), \x03 (CTRL+C interrupt), or '$' character (start of packet
-      // header) or of course, the end of the data in m_bytes...
-      const size_t bytes_len = m_bytes.size();
-      bool done = false;
-      uint32_t idx;
-      for (idx = 1; !done && idx < bytes_len;) {
-        switch (m_bytes[idx]) {
-        case '+':
-        case '-':
-        case '\x03':
-        case '%':
-        case '$':
-          done = true;
-          break;
-
-        default:
-           ++idx;
-          break;
-        }
-      }
-      LLDB_LOGF(log, "GDBRemoteCommunication::%s tossing %u junk bytes: '%.*s'",
-                __FUNCTION__, idx, idx, m_bytes.c_str());
-      m_bytes.erase(0, idx);
-    } break;
+    default:
+      // no more bytes after junk
+      break;
     }
 
     if (content_length == std::string::npos) {
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to