tests: fix potential memory corruption
authorBartosz Golaszewski <bartekgola@gmail.com>
Thu, 23 Feb 2017 15:03:12 +0000 (16:03 +0100)
committerBartosz Golaszewski <bartekgola@gmail.com>
Thu, 23 Feb 2017 15:03:12 +0000 (16:03 +0100)
Valgrind is complaining about invalid reads and writes from the two
line test cases. The culprit is the order in which the cleanup
functions are called.

Declare the chip pointers above the line handles so that we don't
free the resources associated with the chip before releasing the
lines.

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

index ef0db628128fc06af0cac1de11ced8690c2b7a48..411ba9f4ea504f21b9bb2fffcf6e8d21e516642d 100644 (file)
@@ -12,9 +12,9 @@
 
 static void line_request_output(void)
 {
+       GU_CLEANUP(gu_close_chip) struct gpiod_chip *chip = NULL;
        GU_CLEANUP(gu_release_line) struct gpiod_line *line_0 = NULL;
        GU_CLEANUP(gu_release_line) struct gpiod_line *line_1 = NULL;
-       GU_CLEANUP(gu_close_chip) struct gpiod_chip *chip = NULL;
        int status;
 
        chip = gpiod_chip_open(gu_chip_path(0));
@@ -39,8 +39,8 @@ GU_DEFINE_TEST(line_request_output,
 
 static void line_set_value(void)
 {
-       GU_CLEANUP(gu_release_line) struct gpiod_line *line = NULL;
        GU_CLEANUP(gu_close_chip) struct gpiod_chip *chip = NULL;
+       GU_CLEANUP(gu_release_line) struct gpiod_line *line = NULL;
        int status;
 
        chip = gpiod_chip_open(gu_chip_path(0));