diff --git a/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerPlatform.cpp b/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerPlatform.cpp index 5285ec1d3db4e..65f1cc12ba307 100644 --- a/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerPlatform.cpp +++ b/lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunicationServerPlatform.cpp @@ -46,11 +46,13 @@ using namespace lldb_private; GDBRemoteCommunicationServerPlatform::PortMap::PortMap(uint16_t min_port, uint16_t max_port) { + assert(min_port); for (; min_port < max_port; ++min_port) m_port_map[min_port] = LLDB_INVALID_PROCESS_ID; } void GDBRemoteCommunicationServerPlatform::PortMap::AllowPort(uint16_t port) { + assert(port); // Do not modify existing mappings m_port_map.insert({port, LLDB_INVALID_PROCESS_ID}); } diff --git a/lldb/tools/lldb-server/lldb-platform.cpp b/lldb/tools/lldb-server/lldb-platform.cpp index cfd0a3797d810..7148a1d2a3094 100644 --- a/lldb/tools/lldb-server/lldb-platform.cpp +++ b/lldb/tools/lldb-server/lldb-platform.cpp @@ -313,9 +313,11 @@ int main_platform(int argc, char *argv[]) { GDBRemoteCommunicationServerPlatform::PortMap portmap_for_child; llvm::Expected available_port = gdbserver_portmap.GetNextAvailablePort(); - if (available_port) - portmap_for_child.AllowPort(*available_port); - else { + if (available_port) { + // GetNextAvailablePort() may return 0 if gdbserver_portmap is empty. + if (*available_port) + portmap_for_child.AllowPort(*available_port); + } else { llvm::consumeError(available_port.takeError()); fprintf(stderr, "no available gdbserver port for connection - dropping...\n"); @@ -352,7 +354,7 @@ int main_platform(int argc, char *argv[]) { if (platform.IsConnected()) { if (inferior_arguments.GetArgumentCount() > 0) { lldb::pid_t pid = LLDB_INVALID_PROCESS_ID; - std::optional port = 0; + std::optional port; std::string socket_name; Status error = platform.LaunchGDBServer(inferior_arguments, "", // hostname