diff options
author | Fergus Dall <sidereal@google.com> | 2021-07-09 17:52:01 +1000 |
---|---|---|
committer | Daniel Stone <daniels@collabora.com> | 2021-07-21 11:42:42 +0000 |
commit | 80164ef3005e8bb5f785082b97a75cab15444f82 (patch) | |
tree | 27b27a8978fd941332eec986265277a33c261411 /tests | |
parent | ada25fbd526dec116026cb2567a912b47890ad05 (diff) | |
download | wayland-80164ef3005e8bb5f785082b97a75cab15444f82.tar.gz |
util: Avoid undefined behaviour in for_each_helper
for_each_helper tries to calculate a one-past-the-end pointer for its
wl_array input. This is fine when the array has one or more entries, but we
initialize arrays by setting wl_array.data to NULL. Pointer arithmetic is
only defined when both the pointer operand and the result point to the same
allocation, or one-past-the-end of that allocation. As NULL points to no
allocation, no pointer arithmetic can be performed on it, not even adding 0,
even if the result is never dereferenced.
This is caught by clang's ubsan from version 10.
Many tests already hit this case, but I added an explicit test for iterating
over an empty wl_map.
Signed-off-by: Fergus Dall <sidereal@google.com>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/map-test.c | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/tests/map-test.c b/tests/map-test.c index 8ecc1aa..03568ea 100644 --- a/tests/map-test.c +++ b/tests/map-test.c @@ -119,3 +119,19 @@ TEST(map_flags) wl_map_release(&map); } + +static enum wl_iterator_result never_run(void *element, void *data, uint32_t flags) +{ + assert(0); +} + +TEST(map_iter_empty) +{ + struct wl_map map; + + wl_map_init(&map, WL_MAP_SERVER_SIDE); + + wl_map_for_each(&map, never_run, NULL); + + wl_map_release(&map); +} |