tests: fix a potential segfault
authorBartosz Golaszewski <bartekgola@gmail.com>
Thu, 23 Feb 2017 10:47:57 +0000 (11:47 +0100)
committerBartosz Golaszewski <bartekgola@gmail.com>
Thu, 23 Feb 2017 10:47:57 +0000 (11:47 +0100)
We should only sort the mockup chip pointers after determining that
all gpiochip device files have actually been created. Otherwise we
might sort invalid memory since we don't create the mockup_chip
structures for nonexistent devices.

Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
tests/unit/gpiod-unit.c

index 44dc763bbb089df25ec13dab3717c36090473080..51b59455a16f5f328c89fe7a15145d2a2669ceaa 100644 (file)
@@ -380,11 +380,11 @@ static void test_prepare(struct gu_chip_descr *descr)
                }
        }
 
-       qsort(ctx->chips, ctx->num_chips, sizeof(*ctx->chips), chipcmp);
-
        if (descr->num_chips != current)
                die("number of requested and detected mockup gpiochips is not the same");
 
+       qsort(ctx->chips, ctx->num_chips, sizeof(*ctx->chips), chipcmp);
+
        closedir(dir);
 }