From 451dcbad6c1989c21c19e8800702ba14ae59f031 Mon Sep 17 00:00:00 2001 From: Noel Eck Date: Wed, 16 Mar 2016 09:36:39 -0700 Subject: [PATCH] uart: Fix for failed malloc Check malloc return in UART read. Throw if malloc fails. Signed-off-by: Noel Eck Signed-off-by: Brendan Le Foll --- api/mraa/uart.hpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/api/mraa/uart.hpp b/api/mraa/uart.hpp index 73154ce..6f2843f 100644 --- a/api/mraa/uart.hpp +++ b/api/mraa/uart.hpp @@ -126,12 +126,17 @@ class Uart * Read bytes from the device into a String object * * @param length to read + * @throws std::bad_alloc If there is no space left for read. * @return string of data */ std::string readStr(int 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); std::string ret(data, v); free(data);