From: Bartosz Golaszewski Date: Sun, 5 Nov 2017 20:04:52 +0000 (+0100) Subject: iter: fix a crash occurring if no GPIO chips are present X-Git-Url: http://git.maquefel.me/?a=commitdiff_plain;h=f4c15c32ac3b87e1c41534735d91f0f65165bf7e;p=qemu-gpiodev%2Flibgpiod.git iter: fix a crash occurring if no GPIO chips are present If no GPIO chips are present in the system, we bail out from gpiod_chip_iter_new() before allocating the chips array. Add a check in gpiod_chip_iter_free() to avoid calling free() for iter->chips in that case. Signed-off-by: Bartosz Golaszewski --- diff --git a/src/lib/iter.c b/src/lib/iter.c index 62a4165..b9ae310 100644 --- a/src/lib/iter.c +++ b/src/lib/iter.c @@ -58,8 +58,10 @@ struct gpiod_chip_iter * gpiod_chip_iter_new(void) iter->num_chips = num_chips; iter->offset = 0; - if (num_chips == 0) + if (num_chips == 0) { + iter->chips = NULL; return iter; + } iter->chips = calloc(num_chips, sizeof(*iter->chips)); if (!iter->chips) @@ -108,7 +110,9 @@ void gpiod_chip_iter_free_noclose(struct gpiod_chip_iter *iter) gpiod_chip_close(iter->chips[i]); } - free(iter->chips); + if (iter->chips) + free(iter->chips); + free(iter); }