From 4c09830deb4881887ee6dd326fb0f61ad23f638e Mon Sep 17 00:00:00 2001 From: Kent Gibson Date: Mon, 18 Nov 2019 15:41:04 +0100 Subject: [PATCH] core: allow gpiod_line_set_value_bulk() to accept null values Change gpiod_line_set_value_bulk to interpret a NULL values pointer as an array of zero, as per gpiod_line_request_bulk, gpiod_line_set_config_bulk, and gpiod_line_set_direction_bulk_output. The old behaviour was to segfault. Add a corresponding test case for gpiod_line_set_value_bulk(). Signed-off-by: Kent Gibson Signed-off-by: Bartosz Golaszewski --- lib/core.c | 6 ++++-- tests/tests-line.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/lib/core.c b/lib/core.c index f05e595..0bab09d 100644 --- a/lib/core.c +++ b/lib/core.c @@ -742,8 +742,10 @@ int gpiod_line_set_value_bulk(struct gpiod_line_bulk *bulk, const int *values) memset(&data, 0, sizeof(data)); - for (i = 0; i < gpiod_line_bulk_num_lines(bulk); i++) - data.values[i] = (uint8_t)!!values[i]; + if (values) { + for (i = 0; i < gpiod_line_bulk_num_lines(bulk); i++) + data.values[i] = (uint8_t)!!values[i]; + } line = gpiod_line_bulk_get_line(bulk, 0); fd = line_get_fd(line); diff --git a/tests/tests-line.c b/tests/tests-line.c index 4792211..8411132 100644 --- a/tests/tests-line.c +++ b/tests/tests-line.c @@ -276,6 +276,59 @@ GPIOD_TEST_CASE(set_value, 0, { 8 }) g_assert_cmpint(gpiod_test_chip_get_value(0, 2), ==, 0); } +GPIOD_TEST_CASE(set_value_bulk, 0, { 8 }) +{ + g_autoptr(gpiod_chip_struct) chip = NULL; + struct gpiod_line_bulk bulk = GPIOD_LINE_BULK_INITIALIZER; + struct gpiod_line *line0, *line1, *line2; + int values[3]; + gint ret; + + chip = gpiod_chip_open(gpiod_test_chip_path(0)); + g_assert_nonnull(chip); + gpiod_test_return_if_failed(); + + line0 = gpiod_chip_get_line(chip, 0); + line1 = gpiod_chip_get_line(chip, 1); + line2 = gpiod_chip_get_line(chip, 2); + + g_assert_nonnull(line0); + g_assert_nonnull(line1); + g_assert_nonnull(line2); + gpiod_test_return_if_failed(); + + gpiod_line_bulk_add(&bulk, line0); + gpiod_line_bulk_add(&bulk, line1); + gpiod_line_bulk_add(&bulk, line2); + + values[0] = 0; + values[1] = 1; + values[2] = 2; + + ret = gpiod_line_request_bulk_output(&bulk, + GPIOD_TEST_CONSUMER, values); + g_assert_cmpint(ret, ==, 0); + g_assert_cmpint(gpiod_test_chip_get_value(0, 0), ==, 0); + g_assert_cmpint(gpiod_test_chip_get_value(0, 1), ==, 1); + g_assert_cmpint(gpiod_test_chip_get_value(0, 2), ==, 1); + + values[0] = 2; + values[1] = 1; + values[2] = 0; + + ret = gpiod_line_set_value_bulk(&bulk, values); + g_assert_cmpint(ret, ==, 0); + g_assert_cmpint(gpiod_test_chip_get_value(0, 0), ==, 1); + g_assert_cmpint(gpiod_test_chip_get_value(0, 1), ==, 1); + g_assert_cmpint(gpiod_test_chip_get_value(0, 2), ==, 0); + + ret = gpiod_line_set_value_bulk(&bulk, NULL); + g_assert_cmpint(ret, ==, 0); + g_assert_cmpint(gpiod_test_chip_get_value(0, 0), ==, 0); + g_assert_cmpint(gpiod_test_chip_get_value(0, 1), ==, 0); + g_assert_cmpint(gpiod_test_chip_get_value(0, 2), ==, 0); +} + GPIOD_TEST_CASE(get_value_different_chips, 0, { 8, 8 }) { g_autoptr(gpiod_chip_struct) chipA = NULL; -- 2.30.2