From b1a49f0d3c3b543336b8fe6bf98b292da049acbc Mon Sep 17 00:00:00 2001 From: Noel Eck Date: Mon, 27 Nov 2017 15:40:03 -0800 Subject: [PATCH] rhusb: Memory leak fix in sendCommand Fix for case where dataAvailable always returns true. Previously, if this ever happened (eg mock platform), string resp is resized until the system is out of memory. Signed-off-by: Noel Eck --- src/rhusb/rhusb.cxx | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/src/rhusb/rhusb.cxx b/src/rhusb/rhusb.cxx index c0575996..e6e5fa7a 100644 --- a/src/rhusb/rhusb.cxx +++ b/src/rhusb/rhusb.cxx @@ -127,18 +127,14 @@ string RHUSB::sendCommand(string cmd) { throw std::invalid_argument(std::string(__FUNCTION__) + ": cmd is empty!"); - return ""; } - // make sure string is CR terminated - if (cmd.at(cmd.size() - 1) != '\r') - cmd.append("\r"); - - writeStr(cmd); + // Terminate the command with a carriage return + writeStr(cmd + "\r"); string resp; // I see random timeouts with wait values below 250ms - while (dataAvailable(250)) + while (dataAvailable(250) && (resp.size() < maxBuffer) ) { resp += readStr(maxBuffer); } @@ -146,16 +142,14 @@ string RHUSB::sendCommand(string cmd) if (resp.empty()) { throw std::runtime_error(std::string(__FUNCTION__) + - ": timed out waiting for response"); - return ""; + ": " + cmd + " response timed out"); } // check that the last character is the prompt if (resp.at(resp.size() - 1) != '>') { throw std::runtime_error(std::string(__FUNCTION__) + - ": read from device corrupted"); - return ""; + ": " + cmd + " response corrupt"); } // delete the last 3 characters, which should be '\r\n>'