diff options
author | Lennart Poettering <lennart@poettering.net> | 2022-03-21 14:23:38 +0100 |
---|---|---|
committer | Yu Watanabe <watanabe.yu+github@gmail.com> | 2022-03-23 21:46:08 +0900 |
commit | 50996f04ad1b2db36bb210c1bd1c3526f861ea45 (patch) | |
tree | 79412bcb4f686519c85c500f9d6d4a76d8b299f5 /src/test/test-macro.c | |
parent | 4053d11006367d689ae4c9852de5ef5fc4125b35 (diff) | |
download | systemd-50996f04ad1b2db36bb210c1bd1c3526f861ea45.tar.gz |
macro: add macro that simplifies going backwards through an array via pointers
Inspired by #22797, let's avoid some UB when iterating through arrays.
Diffstat (limited to 'src/test/test-macro.c')
-rw-r--r-- | src/test/test-macro.c | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/src/test/test-macro.c b/src/test/test-macro.c index 960ff4bc09..ba319953cd 100644 --- a/src/test/test-macro.c +++ b/src/test/test-macro.c @@ -433,4 +433,27 @@ TEST(DECIMAL_STR_MAX) { assert_se(DECIMAL_STR_MAX(uint64_t) == DECIMAL_STR_WIDTH(u64_longest)+1); } +TEST(PTR_SUB1) { + static const uint64_t x[4] = { 2, 3, 4, 5 }; + const uint64_t *p; + + p = x + ELEMENTSOF(x)-1; + assert_se(*p == 5); + + p = PTR_SUB1(p, x); + assert_se(*p == 4); + + p = PTR_SUB1(p, x); + assert_se(*p == 3); + + p = PTR_SUB1(p, x); + assert_se(*p == 2); + + p = PTR_SUB1(p, x); + assert_se(!p); + + p = PTR_SUB1(p, x); + assert_se(!p); +} + DEFINE_TEST_MAIN(LOG_INFO); |