I think that the memory buffer used to serialize peer responses should limit the total length to the maximum the peer is willing to accept. This can help prevent easily DoS-able requests from being added in the future and bringing down every Bitcoin Core node on the network, rather the relying on a soft limit of how big a response can actually be.
Opening this PR so more eyes can look at this, but I'm not able to test whether it actually works as intended. Don't merge until the error case is actually hit and the behavior is as intended. In particular, I don't know if that exception is actually caught anywhere, but I have received confirmation that the patch at least compiles.