diff options
author | Lennart Poettering <lennart@poettering.net> | 2023-02-22 23:10:25 +0100 |
---|---|---|
committer | Yu Watanabe <watanabe.yu+github@gmail.com> | 2023-02-23 11:43:43 +0900 |
commit | ff3f1464ec2dd40c9d8eb92e1474cb4d1c8c676b (patch) | |
tree | a0d437665b0e0b35eef3f137b6b9ceb7d5441e13 /src/test | |
parent | c7d941c52771e09f14cdfff969e7e9a00e2c8a2f (diff) | |
download | systemd-ff3f1464ec2dd40c9d8eb92e1474cb4d1c8c676b.tar.gz |
memory-util: add a concept for gcc cleanup attribute based array destruction
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/test-memory-util.c | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/src/test/test-memory-util.c b/src/test/test-memory-util.c index 241f46c0d0..2f8384ac09 100644 --- a/src/test/test-memory-util.c +++ b/src/test/test-memory-util.c @@ -15,4 +15,41 @@ TEST(eqzero) { assert_se(!eqzero(longer)); } +static void my_destructor(struct iovec *iov, size_t n) { + /* not really a destructor, just something we can use to check if the destruction worked */ + memset(iov, 'y', sizeof(struct iovec) * n); +} + +TEST(cleanup_array) { + struct iovec *iov, *saved_iov; + size_t n, saved_n; + + n = 7; + iov = new(struct iovec, n); + assert_se(iov); + + memset(iov, 'x', sizeof(struct iovec) * n); + + saved_iov = iov; + saved_n = n; + + { + assert_se(memeqbyte('x', saved_iov, sizeof(struct iovec) * saved_n)); + assert_se(iov); + assert_se(n > 0); + + CLEANUP_ARRAY(iov, n, my_destructor); + + assert_se(memeqbyte('x', saved_iov, sizeof(struct iovec) * saved_n)); + assert_se(iov); + assert_se(n > 0); + } + + assert_se(memeqbyte('y', saved_iov, sizeof(struct iovec) * saved_n)); + assert_se(!iov); + assert_se(n == 0); + + free(saved_iov); +} + DEFINE_TEST_MAIN(LOG_INFO); |