From b24e90db0cceed81680b1a398d2a8b19be5a3fbb Mon Sep 17 00:00:00 2001 From: Simo Kuusela Date: Tue, 14 Mar 2017 11:27:30 +0200 Subject: [PATCH] mraa.c: Fix strsep crash Variable 'dup' doesn't get freed correctly as the pointer to it changes after 'strsep' function is used. Also 'token' variable doesn't need to be freed as freeing the original 'dup' already frees the same memory. Signed-off-by: Simo Kuusela Signed-off-by: Brendan Le Foll --- src/mraa.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/mraa.c b/src/mraa.c index 42c5bb9..e713a6d 100644 --- a/src/mraa.c +++ b/src/mraa.c @@ -947,7 +947,8 @@ mraa_find_i2c_bus_pci(const char* pci_device, const char *pci_id, const char* ad } else { while (n--) { - char* dup = strdup(namelist[n]->d_name); + char* dup = strdup(namelist[n]->d_name); + char* orig_dup = dup; if (dup == NULL) { syslog(LOG_ERR, "Ran out of memory!"); break; @@ -961,22 +962,20 @@ mraa_find_i2c_bus_pci(const char* pci_device, const char *pci_id, const char* ad if (token != NULL) { int ret = -1; if (mraa_atoi(token, &ret) == MRAA_SUCCESS) { - free(dup); + free(orig_dup); free(namelist[n]); free(namelist); syslog(LOG_NOTICE, "Adding i2c bus found on i2c-%d on adapter %s", ret, adapter_name); return ret; } - free(dup); + free(orig_dup); free(namelist[n]); free(namelist); return -1; } } - free(token); - } else { - free(dup); } + free(orig_dup); free(namelist[n]); } free(namelist);