tests: add a test case for incorrect retrieving of edge events from buffer
authorBartosz Golaszewski <bartosz.golaszewski@linaro.org>
Wed, 15 Mar 2023 19:54:52 +0000 (20:54 +0100)
committerBartosz Golaszewski <bartosz.golaszewski@linaro.org>
Wed, 15 Mar 2023 19:56:21 +0000 (20:56 +0100)
Add a test case that makes sure we don't crash when trying to access an
out-of-bounds edge event in an edge event buffer.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
tests/tests-edge-event.c

index 0ff7dc88dea7e10914c370acd5ba7d7b7271f915..e79f4579ccf18873be34bd72cdba49689300ac2b 100644 (file)
@@ -639,3 +639,45 @@ GPIOD_TEST_CASE(null_buffer)
        g_assert_cmpint(ret, ==, -1);
        gpiod_test_expect_errno(EINVAL);
 }
+
+GPIOD_TEST_CASE(get_edge_event_index_out_of_bounds)
+{
+       static const guint offset = 2;
+
+       g_autoptr(GPIOSimChip) sim = g_gpiosim_chip_new("num-lines", 8, NULL);
+       g_autoptr(struct_gpiod_chip) chip = NULL;
+       g_autoptr(struct_gpiod_line_settings) settings = NULL;
+       g_autoptr(struct_gpiod_line_config) line_cfg = NULL;
+       g_autoptr(struct_gpiod_line_request) request = NULL;
+       g_autoptr(struct_gpiod_edge_event_buffer) buffer = NULL;
+       struct gpiod_edge_event *event;
+       gint ret;
+
+       chip = gpiod_test_open_chip_or_fail(g_gpiosim_chip_get_dev_path(sim));
+       settings = gpiod_test_create_line_settings_or_fail();
+       line_cfg = gpiod_test_create_line_config_or_fail();
+       buffer = gpiod_test_create_edge_event_buffer_or_fail(64);
+
+       gpiod_line_settings_set_direction(settings, GPIOD_LINE_DIRECTION_INPUT);
+       gpiod_line_settings_set_edge_detection(settings, GPIOD_LINE_EDGE_BOTH);
+
+       gpiod_test_line_config_add_line_settings_or_fail(line_cfg, &offset, 1,
+                                                        settings);
+
+       request = gpiod_test_request_lines_or_fail(chip, NULL, line_cfg);
+
+       g_gpiosim_chip_set_pull(sim, 2, G_GPIOSIM_PULL_UP);
+       g_usleep(500);
+       g_gpiosim_chip_set_pull(sim, 2, G_GPIOSIM_PULL_DOWN);
+       g_usleep(500);
+       g_gpiosim_chip_set_pull(sim, 2, G_GPIOSIM_PULL_UP);
+       g_usleep(500);
+
+       ret = gpiod_line_request_read_edge_events(request, buffer, 3);
+       g_assert_cmpint(ret, ==, 3);
+       gpiod_test_return_if_failed();
+
+       event = gpiod_edge_event_buffer_get_event(buffer, 5);
+       g_assert_null(event);
+       gpiod_test_expect_errno(EINVAL);
+}