From 9ea6cd2dc6585a6fecea4356d24a0bcc8fecd05b Mon Sep 17 00:00:00 2001 From: Ivan Mikhaylov Date: Wed, 23 Mar 2022 16:23:58 +0000 Subject: [PATCH] gpio: fix incorrect assignment of *cinfos in mraa_get_chip_infos *cinfos in mraa_get_chip_infos is not set in case of mraa_get_chip_info_by_name failure which happens on access of /dev/gpiochip* files which leads to memory free by invalid pointer in *cinfos. As example, it can be easy recreated with non-root run of mraa-gpio: test@iot2050-debian:~$ mraa-gpio free(): invalid pointer Aborted Signed-off-by: Ivan Mikhaylov --- src/gpio/gpio_chardev.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/gpio/gpio_chardev.c b/src/gpio/gpio_chardev.c index 7f1f6e0..260a2ea 100644 --- a/src/gpio/gpio_chardev.c +++ b/src/gpio/gpio_chardev.c @@ -431,6 +431,8 @@ mraa_get_chip_infos(mraa_gpiod_chip_info*** cinfos) return -1; } + *cinfos = cinfo; + /* Get chip info for all gpiochips present in the platform */ for (i = 0; i < num_chips; i++) { cinfo[i] = mraa_get_chip_info_by_name(dirs[i]->d_name); @@ -440,7 +442,5 @@ mraa_get_chip_infos(mraa_gpiod_chip_info*** cinfos) } } - *cinfos = cinfo; - return num_chips; }