helpers: fix a crash in gpiod_chip_find_line()
authorBartosz Golaszewski <bartekgola@gmail.com>
Fri, 20 Oct 2017 09:00:45 +0000 (11:00 +0200)
committerBartosz Golaszewski <bartekgola@gmail.com>
Fri, 20 Oct 2017 09:12:50 +0000 (11:12 +0200)
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 <bartekgola@gmail.com>
src/lib/helpers.c
tests/tests-line.c

index f977ca6ff0ac294ff050ebeda01bdbbdee8b4ef3..4de9143e19d97da30e9a7e5a55718a1a5c165840 100644 (file)
@@ -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;
        }
 
index 86ebc9613ac403d1898cd02e3f0aec1e68d8945a..220255df829cd5720fb2269575ae83c636c2d04a 100644 (file)
@@ -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;