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 <noel.eck@intel.com>
This commit is contained in:
Noel Eck 2017-11-27 15:40:03 -08:00
parent ac89a4a130
commit b1a49f0d3c

View File

@ -127,18 +127,14 @@ string RHUSB::sendCommand(string cmd)
{ {
throw std::invalid_argument(std::string(__FUNCTION__) + throw std::invalid_argument(std::string(__FUNCTION__) +
": cmd is empty!"); ": cmd is empty!");
return "";
} }
// make sure string is CR terminated // Terminate the command with a carriage return
if (cmd.at(cmd.size() - 1) != '\r') writeStr(cmd + "\r");
cmd.append("\r");
writeStr(cmd);
string resp; string resp;
// I see random timeouts with wait values below 250ms // I see random timeouts with wait values below 250ms
while (dataAvailable(250)) while (dataAvailable(250) && (resp.size() < maxBuffer) )
{ {
resp += readStr(maxBuffer); resp += readStr(maxBuffer);
} }
@ -146,16 +142,14 @@ string RHUSB::sendCommand(string cmd)
if (resp.empty()) if (resp.empty())
{ {
throw std::runtime_error(std::string(__FUNCTION__) + throw std::runtime_error(std::string(__FUNCTION__) +
": timed out waiting for response"); ": " + cmd + " response timed out");
return "";
} }
// check that the last character is the prompt // check that the last character is the prompt
if (resp.at(resp.size() - 1) != '>') if (resp.at(resp.size() - 1) != '>')
{ {
throw std::runtime_error(std::string(__FUNCTION__) + throw std::runtime_error(std::string(__FUNCTION__) +
": read from device corrupted"); ": " + cmd + " response corrupt");
return "";
} }
// delete the last 3 characters, which should be '\r\n>' // delete the last 3 characters, which should be '\r\n>'