From caf75a68c0560bbb4c51b889503ab16a00c883d2 Mon Sep 17 00:00:00 2001 From: Alex Tereschenko Date: Sat, 1 Jul 2017 17:49:09 +0200 Subject: [PATCH] jsonplatform.c: properly reallocate memory for platform name Fixes #761. Signed-off-by: Alex Tereschenko Signed-off-by: Brendan Le Foll --- src/json/jsonplatform.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/json/jsonplatform.c b/src/json/jsonplatform.c index 4d85052..3be8cfe 100644 --- a/src/json/jsonplatform.c +++ b/src/json/jsonplatform.c @@ -709,7 +709,16 @@ mraa_init_json_platform(const char* platform_json) free(plat); // Set the new one in it's place plat = board; - platform_name = plat->platform_name; + + // This one was allocated and assigned an "Unknown platform" value by now, + // so we need to reallocate it. + free(platform_name); + platform_name = calloc(strlen(plat->platform_name) + 1, sizeof(char)); + if (platform_name == NULL) { + syslog(LOG_ERR, "init_json_platform: Could not allocate memory for platform_name"); + goto unsuccessful; + } + strncpy(platform_name, plat->platform_name, strlen(plat->platform_name) + 1); // We made it to the end without anything going wrong, just cleanup ret = MRAA_SUCCESS;