Private
Public Access
2
0

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 <brendan.le.foll@intel.com>
This commit is contained in:
Brendan Le Foll
2016-11-30 11:33:38 +01:00
parent 250590e1ed
commit 1c180e393c

View File

@@ -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);