Skip to content

Commit 998511c

Browse files
authored
[debugserver] Fix mutex scope in RNBRemote::CommDataReceived (#131077)
The mutex in RNBRemote::CommDataReceived protects m_rx_packets and should extend to the end of the function to cover the read where we check if the list is empty.
1 parent 08d15e3 commit 998511c

File tree

1 file changed

+70
-72
lines changed

1 file changed

+70
-72
lines changed

lldb/tools/debugserver/source/RNBRemote.cpp

Lines changed: 70 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,84 +1054,82 @@ rnb_err_t RNBRemote::HandleReceivedPacket(PacketEnum *type) {
10541054
void RNBRemote::CommDataReceived(const std::string &new_data) {
10551055
// DNBLogThreadedIf (LOG_RNB_REMOTE, "%8d RNBRemote::%s called",
10561056
// (uint32_t)m_comm.Timer().ElapsedMicroSeconds(true), __FUNCTION__);
1057-
{
1058-
// Put the packet data into the buffer in a thread safe fashion
1059-
PThreadMutex::Locker locker(m_mutex);
1060-
1061-
std::string data;
1062-
// See if we have any left over data from a previous call to this
1063-
// function?
1064-
if (!m_rx_partial_data.empty()) {
1065-
// We do, so lets start with that data
1066-
data.swap(m_rx_partial_data);
1067-
}
1068-
// Append the new incoming data
1069-
data += new_data;
1070-
1071-
// Parse up the packets into gdb remote packets
1072-
size_t idx = 0;
1073-
const size_t data_size = data.size();
1074-
1075-
while (idx < data_size) {
1076-
// end_idx must be one past the last valid packet byte. Start
1077-
// it off with an invalid value that is the same as the current
1078-
// index.
1079-
size_t end_idx = idx;
1080-
1081-
switch (data[idx]) {
1082-
case '+': // Look for ack
1083-
case '-': // Look for cancel
1084-
case '\x03': // ^C to halt target
1085-
end_idx = idx + 1; // The command is one byte long...
1086-
break;
10871057

1088-
case '$':
1089-
// Look for a standard gdb packet?
1090-
end_idx = data.find('#', idx + 1);
1091-
if (end_idx == std::string::npos || end_idx + 3 > data_size) {
1092-
end_idx = std::string::npos;
1093-
} else {
1094-
// Add two for the checksum bytes and 1 to point to the
1095-
// byte just past the end of this packet
1096-
end_idx += 3;
1097-
}
1098-
break;
1058+
// Put the packet data into the buffer in a thread safe fashion
1059+
PThreadMutex::Locker locker(m_mutex);
1060+
1061+
std::string data;
1062+
// See if we have any left over data from a previous call to this
1063+
// function?
1064+
if (!m_rx_partial_data.empty()) {
1065+
// We do, so lets start with that data
1066+
data.swap(m_rx_partial_data);
1067+
}
1068+
// Append the new incoming data
1069+
data += new_data;
1070+
1071+
// Parse up the packets into gdb remote packets
1072+
size_t idx = 0;
1073+
const size_t data_size = data.size();
1074+
1075+
while (idx < data_size) {
1076+
// end_idx must be one past the last valid packet byte. Start
1077+
// it off with an invalid value that is the same as the current
1078+
// index.
1079+
size_t end_idx = idx;
1080+
1081+
switch (data[idx]) {
1082+
case '+': // Look for ack
1083+
case '-': // Look for cancel
1084+
case '\x03': // ^C to halt target
1085+
end_idx = idx + 1; // The command is one byte long...
1086+
break;
10991087

1100-
default:
1101-
break;
1088+
case '$':
1089+
// Look for a standard gdb packet?
1090+
end_idx = data.find('#', idx + 1);
1091+
if (end_idx == std::string::npos || end_idx + 3 > data_size) {
1092+
end_idx = std::string::npos;
1093+
} else {
1094+
// Add two for the checksum bytes and 1 to point to the
1095+
// byte just past the end of this packet
1096+
end_idx += 3;
11021097
}
1098+
break;
11031099

1104-
if (end_idx == std::string::npos) {
1105-
// Not all data may be here for the packet yet, save it for
1106-
// next time through this function.
1107-
m_rx_partial_data += data.substr(idx);
1108-
// DNBLogThreadedIf (LOG_RNB_MAX, "%8d RNBRemote::%s saving data for
1109-
// later[%u, npos):
1110-
// '%s'",(uint32_t)m_comm.Timer().ElapsedMicroSeconds(true),
1111-
// __FUNCTION__, idx, m_rx_partial_data.c_str());
1112-
idx = end_idx;
1113-
} else if (idx < end_idx) {
1114-
m_packets_recvd++;
1115-
// Hack to get rid of initial '+' ACK???
1116-
if (m_packets_recvd == 1 && (end_idx == idx + 1) && data[idx] == '+') {
1117-
// DNBLogThreadedIf (LOG_RNB_REMOTE, "%8d RNBRemote::%s throwing first
1118-
// ACK away....[%u, npos):
1119-
// '+'",(uint32_t)m_comm.Timer().ElapsedMicroSeconds(true),
1120-
// __FUNCTION__, idx);
1121-
} else {
1122-
// We have a valid packet...
1123-
m_rx_packets.push_back(data.substr(idx, end_idx - idx));
1124-
DNBLogThreadedIf(LOG_RNB_PACKETS, "getpkt: %s",
1125-
m_rx_packets.back().c_str());
1126-
}
1127-
idx = end_idx;
1100+
default:
1101+
break;
1102+
}
1103+
1104+
if (end_idx == std::string::npos) {
1105+
// Not all data may be here for the packet yet, save it for
1106+
// next time through this function.
1107+
m_rx_partial_data += data.substr(idx);
1108+
// DNBLogThreadedIf (LOG_RNB_MAX, "%8d RNBRemote::%s saving data for
1109+
// later[%u, npos):
1110+
// '%s'",(uint32_t)m_comm.Timer().ElapsedMicroSeconds(true),
1111+
// __FUNCTION__, idx, m_rx_partial_data.c_str());
1112+
idx = end_idx;
1113+
} else if (idx < end_idx) {
1114+
m_packets_recvd++;
1115+
// Hack to get rid of initial '+' ACK???
1116+
if (m_packets_recvd == 1 && (end_idx == idx + 1) && data[idx] == '+') {
1117+
// DNBLogThreadedIf (LOG_RNB_REMOTE, "%8d RNBRemote::%s throwing first
1118+
// ACK away....[%u, npos):
1119+
// '+'",(uint32_t)m_comm.Timer().ElapsedMicroSeconds(true),
1120+
// __FUNCTION__, idx);
11281121
} else {
1129-
DNBLogThreadedIf(LOG_RNB_MAX,
1130-
"%8d RNBRemote::%s tossing junk byte at %c",
1131-
(uint32_t)m_comm.Timer().ElapsedMicroSeconds(true),
1132-
__FUNCTION__, data[idx]);
1133-
idx = idx + 1;
1122+
// We have a valid packet...
1123+
m_rx_packets.push_back(data.substr(idx, end_idx - idx));
1124+
DNBLogThreadedIf(LOG_RNB_PACKETS, "getpkt: %s",
1125+
m_rx_packets.back().c_str());
11341126
}
1127+
idx = end_idx;
1128+
} else {
1129+
DNBLogThreadedIf(LOG_RNB_MAX, "%8d RNBRemote::%s tossing junk byte at %c",
1130+
(uint32_t)m_comm.Timer().ElapsedMicroSeconds(true),
1131+
__FUNCTION__, data[idx]);
1132+
idx = idx + 1;
11351133
}
11361134
}
11371135

0 commit comments

Comments
 (0)