From: Bartosz Golaszewski Date: Fri, 20 Oct 2017 09:00:45 +0000 (+0200) Subject: helpers: fix a crash in gpiod_chip_find_line() X-Git-Url: http://git.maquefel.me/?a=commitdiff_plain;h=0a2586e956d4348386899ee6af949abf27cfe66b;p=qemu-gpiodev%2Flibgpiod.git helpers: fix a crash in gpiod_chip_find_line() The name of a line can be NULL in which case we must check it and skip the call to strcmp() to avoid a NULL pointer dereference. Signed-off-by: Bartosz Golaszewski --- diff --git a/src/lib/helpers.c b/src/lib/helpers.c index f977ca6..4de9143 100644 --- a/src/lib/helpers.c +++ b/src/lib/helpers.c @@ -107,13 +107,15 @@ gpiod_chip_find_line(struct gpiod_chip *chip, const char *name) { struct gpiod_line_iter iter; struct gpiod_line *line; + const char *tmp; gpiod_line_iter_init(&iter, chip); gpiod_foreach_line(&iter, line) { if (gpiod_line_iter_err(&iter)) return NULL; - if (strcmp(gpiod_line_name(line), name) == 0) + tmp = gpiod_line_name(line); + if (tmp && strcmp(tmp, name) == 0) return line; } diff --git a/tests/tests-line.c b/tests/tests-line.c index 86ebc96..220255d 100644 --- a/tests/tests-line.c +++ b/tests/tests-line.c @@ -327,6 +327,18 @@ TEST_DEFINE(line_find_not_found, "gpiod_line_find() - not found", TEST_FLAG_NAMED_LINES, { 16, 16, 32, 16 }); +static void line_find_unnamed_lines(void) +{ + TEST_CLEANUP(test_line_close_chip) struct gpiod_line *line = NULL; + + line = gpiod_line_find("gpio-mockup-C-12"); + TEST_ASSERT_NULL(line); + TEST_ASSERT_ERRNO_IS(ENOENT); +} +TEST_DEFINE(line_find_unnamed_lines, + "gpiod_line_find() - unnamed lines", + 0, { 16, 16, 32, 16 }); + static void line_direction(void) { TEST_CLEANUP(test_close_chip) struct gpiod_chip *chip = NULL;