From 1c180e393c3e7814b13626e9d201005d5ec69c20 Mon Sep 17 00:00:00 2001 From: Brendan Le Foll Date: Wed, 30 Nov 2016 11:33:38 +0100 Subject: [PATCH] uart.c: Allocate mem for dev->path to fix getDevicePath in raw mode When mraa_init_uart_raw is called it places the path arg in dev->path. This works in non raw mode because the path is statically in the device configuration but in raw mode this is a dynamic address meaning that we need to copy it. Fix this by simply copying the device path rather than relying on the user keeping that string path in memory. Signed-off-by: Brendan Le Foll --- src/uart/uart.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/uart/uart.c b/src/uart/uart.c index d41a7a8..486beb2 100644 --- a/src/uart/uart.c +++ b/src/uart/uart.c @@ -215,7 +215,13 @@ mraa_uart_init_raw(const char* path) status = MRAA_ERROR_NO_RESOURCES; goto init_raw_cleanup; } - dev->path = path; + dev->path = (char*) calloc(strlen(path)+1, sizeof(char)); + if (dev->path == NULL) { + syslog(LOG_ERR, "uart: Failed to allocate memory for path"); + status = MRAA_ERROR_NO_RESOURCES; + goto init_raw_cleanup; + } + strncpy(dev->path, path, strlen(path)); if (IS_FUNC_DEFINED(dev, uart_init_raw_replace)) { status = dev->advance_func->uart_init_raw_replace(dev, path); @@ -264,6 +270,9 @@ init_raw_cleanup: if (dev->fd >= 0) { close(dev->fd); } + if (dev->path != NULL) { + free(dev->path); + } free(dev); } return NULL; @@ -284,6 +293,9 @@ mraa_uart_stop(mraa_uart_context dev) if (dev->fd >= 0) { close(dev->fd); } + if (dev->path != NULL) { + free(dev->path); + } free(dev);