uart: Fix for failed malloc
Check malloc return in UART read. Throw if malloc fails. Signed-off-by: Noel Eck <noel.eck@intel.com> Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
This commit is contained in:
committed by
Brendan Le Foll
parent
a52ce5d5bb
commit
451dcbad6c
@@ -126,12 +126,17 @@ class Uart
|
|||||||
* Read bytes from the device into a String object
|
* Read bytes from the device into a String object
|
||||||
*
|
*
|
||||||
* @param length to read
|
* @param length to read
|
||||||
|
* @throws std::bad_alloc If there is no space left for read.
|
||||||
* @return string of data
|
* @return string of data
|
||||||
*/
|
*/
|
||||||
std::string
|
std::string
|
||||||
readStr(int length)
|
readStr(int length)
|
||||||
{
|
{
|
||||||
char* data = (char*) malloc(sizeof(char) * length);
|
char* data = (char*) malloc(sizeof(char) * length);
|
||||||
|
if (data == NULL) {
|
||||||
|
throw std::bad_alloc();
|
||||||
|
}
|
||||||
|
|
||||||
int v = mraa_uart_read(m_uart, data, (size_t) length);
|
int v = mraa_uart_read(m_uart, data, (size_t) length);
|
||||||
std::string ret(data, v);
|
std::string ret(data, v);
|
||||||
free(data);
|
free(data);
|
||||||
|
|||||||
Reference in New Issue
Block a user