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:
@@ -215,7 +215,13 @@ mraa_uart_init_raw(const char* path)
|
|||||||
status = MRAA_ERROR_NO_RESOURCES;
|
status = MRAA_ERROR_NO_RESOURCES;
|
||||||
goto init_raw_cleanup;
|
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)) {
|
if (IS_FUNC_DEFINED(dev, uart_init_raw_replace)) {
|
||||||
status = dev->advance_func->uart_init_raw_replace(dev, path);
|
status = dev->advance_func->uart_init_raw_replace(dev, path);
|
||||||
@@ -264,6 +270,9 @@ init_raw_cleanup:
|
|||||||
if (dev->fd >= 0) {
|
if (dev->fd >= 0) {
|
||||||
close(dev->fd);
|
close(dev->fd);
|
||||||
}
|
}
|
||||||
|
if (dev->path != NULL) {
|
||||||
|
free(dev->path);
|
||||||
|
}
|
||||||
free(dev);
|
free(dev);
|
||||||
}
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
@@ -284,6 +293,9 @@ mraa_uart_stop(mraa_uart_context dev)
|
|||||||
if (dev->fd >= 0) {
|
if (dev->fd >= 0) {
|
||||||
close(dev->fd);
|
close(dev->fd);
|
||||||
}
|
}
|
||||||
|
if (dev->path != NULL) {
|
||||||
|
free(dev->path);
|
||||||
|
}
|
||||||
|
|
||||||
free(dev);
|
free(dev);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user