diff options
author | Bartosz Golaszewski <bartosz.golaszewski@linaro.org> | 2023-03-15 20:54:52 +0100 |
---|---|---|
committer | Bartosz Golaszewski <bartosz.golaszewski@linaro.org> | 2023-03-15 20:56:21 +0100 |
commit | 8bf7b0c46f019136dea39f078737de2361ca2e95 (patch) | |
tree | de7113dd9d9d4d05395cc30a46ad59c0fc0a4f1c | |
parent | 10febcbe0b4d70099af8dfd11d398772a69ac6bd (diff) | |
download | libgpiod-8bf7b0c46f019136dea39f078737de2361ca2e95.tar.gz |
tests: add a test case for incorrect retrieving of edge events from buffer
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>
-rw-r--r-- | tests/tests-edge-event.c | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/tests/tests-edge-event.c b/tests/tests-edge-event.c index 0ff7dc88..e79f4579 100644 --- a/tests/tests-edge-event.c +++ b/tests/tests-edge-event.c @@ -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); +} |