Parse remote protocol integers without throwing - #1166
Conversation
std::stoi/stol/stoull raise std::invalid_argument or std::out_of_range on malformed input. The RSP/GDB adapters called them directly on data received from the remote debug stub, so a malformed packet could crash the process with an uncaught exception. Add RspConnector::ParseInt, a std::from_chars-based helper that returns a fallback value instead, and use it at all unguarded call sites. Also guard the packet substr/index accesses in PacketToUnorderedMap against short packets. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
|
Why is it ok for all these sites to just default to |
Usually when this happens, the communication already deviated from the expected way in an irrecoverable way (e.g., the GDB stub and our adapter has a different assumption of the RSP protocol), so the actual value does not matter as long we we do not crash ourselves. I think we can add a warning when this happens so as to assist troubleshooting, do you want that to be done? |
|
This kind feels like the opposite of what you should be doing. It seems like instead of just silently accepting corrupted rsp messages you need to throw and exception and you just need to have a more principled approach to what you do when this occurs. So instead of preventing exceptions you just need to catch them in the correct locations so you can do proper teardown. |
Add
RspConnector::ParseInt(based onstd::from_chars) and use it instead ofstd::stoi/stoullwhen parsing data from the remote debug stub, so malformed packets can no longer crash the process. Also guard short-packetsubstr/index accesses inPacketToUnorderedMap.Fixes #1164