]> git.ipfire.org Git - thirdparty/strongswan.git/commitdiff
unit-tests: Use allocated listener instead of stack object in exchange tests
authorThomas Egerer <thomas.egerer@secunet.com>
Fri, 2 Sep 2022 11:54:05 +0000 (11:54 +0000)
committerTobias Brunner <tobias@strongswan.org>
Thu, 15 Sep 2022 10:16:12 +0000 (12:16 +0200)
When using the statement expression and a stack object along with
clang-11 and libasan, we get quite a lot of errors about reading
invalid memory. This is due to clang making the actual listener_t local
to the block, such that the access outside of the macros using
_assert_payload is (correctly) considered an error.
By using a heap allocated object, we can destroy it once the listener
returns FALSE (cleaning up properly), and since bus_t does not touch the
listener after that, we don't get any errors from libasan.

Co-authored-by: Tobias Brunner <tobias@strongswan.org>
src/libcharon/tests/suites/test_ike_mid_sync.c
src/libcharon/tests/utils/exchange_test_asserts.c
src/libcharon/tests/utils/exchange_test_asserts.h

index fa7592a6ac54158ebae0c6b1104601eca3b56b92..efe58c23f70a507ecd9b1b9d33750eb7e66cde73 100644 (file)
@@ -22,7 +22,7 @@
 #include <bio/bio_reader.h>
 #include <bio/bio_writer.h>
 
-/**
+/*
  * FIXME: Since we don't have the server side yet, this is kind of a hack!!!
  */
 
@@ -37,15 +37,18 @@ static bool add_notify(listener_t *listener, ike_sa_t *ike_sa,
        {
                message->add_notify(message, FALSE, IKEV2_MESSAGE_ID_SYNC_SUPPORTED,
                                                        chunk_empty);
+               free(listener);
                return FALSE;
        }
        return TRUE;
 }
+
 #define add_notify_to_ike_auth() ({ \
-       listener_t _notify_listener = { \
+       listener_t *_notify_listener; \
+       INIT(_notify_listener, \
                .message = add_notify, \
-       }; \
-       exchange_test_helper->add_listener(exchange_test_helper, &_notify_listener); \
+       ); \
+       exchange_test_helper->add_listener(exchange_test_helper, _notify_listener); \
 })
 
 /**
index 8c39a664329fb1cf376c1d16cc1ebcb643b62c23..1a4fdda837f04ca4b73d2876c7c82f92ff025a6a 100644 (file)
@@ -178,6 +178,8 @@ bool exchange_test_asserts_message(listener_t *listener, ike_sa_t *ike_sa,
                                assert_message_rule(this, message, &this->rules[i]);
                        }
                }
+               free(this->rules);
+               free(this);
                return FALSE;
        }
        return TRUE;
index ac98dd452774643f574899acc832696fb36eed60..a58832d7333c883d4e6bf07e427dd7508130bf38 100644 (file)
@@ -350,16 +350,18 @@ bool exchange_test_asserts_message(listener_t *this, ike_sa_t *ike_sa,
 
 #define _assert_payload(dir, c, ...) ({ \
        listener_message_rule_t _rules[] = { __VA_ARGS__ }; \
-       listener_message_assert_t _listener = { \
+       listener_message_assert_t *_listener; \
+       INIT(_listener, \
                .listener = { .message = exchange_test_asserts_message, }, \
                .file = __FILE__, \
                .line = __LINE__, \
                .incoming = streq(dir, "IN") ? TRUE : FALSE, \
                .count = c, \
-               .rules = _rules, \
+               .rules = malloc(sizeof(_rules)), \
                .num_rules = countof(_rules), \
-       }; \
-       exchange_test_helper->add_listener(exchange_test_helper, &_listener.listener); \
+       ); \
+       memcpy(_listener->rules, _rules, sizeof(_rules)); \
+       exchange_test_helper->add_listener(exchange_test_helper, &_listener->listener); \
 })
 
 /**