[MERGED] osmo-iuh[master]: tests: sanitize: fix mem leaks, clean after tests

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

Neels Hofmeyr gerrit-no-reply at lists.osmocom.org
Wed Nov 22 16:09:36 UTC 2017


Neels Hofmeyr has submitted this change and it was merged.

Change subject: tests: sanitize: fix mem leaks, clean after tests
......................................................................


tests: sanitize: fix mem leaks, clean after tests

Fix various mem leaks in the testing code.

Add test_common_cleanup() in test_common.c, to free talloc contexts; call in
test-{helpers,hnbap,ranap}.c. Upon talloc ctx cleanup, ensure that they are
actually empty, in order to catch newly introduced mem leaks.

If non-empty, print talloc context reports.

Change-Id: Ic66c005f2a264774e18bb54e58b87bef5944511c
---
M src/tests/test-helpers.c
M src/tests/test-hnbap.c
M src/tests/test-ranap.c
M src/tests/test_common.c
M src/tests/test_common.h
5 files changed, 33 insertions(+), 1 deletion(-)

Approvals:
  Jenkins Builder: Verified
  Holger Freyther: Looks good to me, approved



diff --git a/src/tests/test-helpers.c b/src/tests/test-helpers.c
index 44fd735..f218a79 100644
--- a/src/tests/test-helpers.c
+++ b/src/tests/test-helpers.c
@@ -105,6 +105,8 @@
 	ASSERT(enc.size == 24/8);
 	ASSERT(enc.bits_unused == 0);
 
+	talloc_free(buffer);
+
 	rc = aper_encode_to_new_buffer(&asn_DEF_BIT_STRING, 0, &enc, (void **) &buffer);
 	printf("Encoded: %s\n", osmo_hexdump_nospc(buffer, rc));
 
@@ -118,6 +120,7 @@
 	printf("Decoding large string from asn1: %s\n", text);
 	ASSERT(rc == 31);
 
+	talloc_free(buffer);
 }
 
 void test_ranap_common(void)
@@ -211,5 +214,6 @@
 	test_asn1_helpers();
 	test_ranap_common();
 
+	test_common_cleanup();
 	return 0;
 }
diff --git a/src/tests/test-hnbap.c b/src/tests/test-hnbap.c
index ef46070..dfd5ae9 100644
--- a/src/tests/test-hnbap.c
+++ b/src/tests/test-hnbap.c
@@ -142,6 +142,8 @@
 	printf("HNBAP UE Register request from IMSI %s\n", imsi);
 	hnbap_free_ueregisterrequesties(&ue_req_ies);
 
+	ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_HNBAP_PDU, pdu);
+
 	memset(pdu, 0, sizeof(*pdu));
 	dec_ret = aper_decode(NULL, &asn_DEF_HNBAP_PDU, (void **) &pdu,
 			hnbap_ue_reg_acc, sizeof(hnbap_ue_reg_acc), 0, 0);
@@ -163,6 +165,7 @@
 	printf("HNBAP UE Register accept to IMSI %s\n", imsi);
 	hnbap_free_ueregisteraccepties(&ue_acc_ies);
 
+	ASN_STRUCT_FREE_CONTENTS_ONLY(asn_DEF_HNBAP_PDU, pdu);
 }
 
 int main(int argc, char **argv)
@@ -175,6 +178,7 @@
 
 	test_asn1_decoding();
 
+	test_common_cleanup();
 	return 0;
 }
 
diff --git a/src/tests/test-ranap.c b/src/tests/test-ranap.c
index c1c7003..05be874 100644
--- a/src/tests/test-ranap.c
+++ b/src/tests/test-ranap.c
@@ -197,6 +197,8 @@
 	talloc_report(talloc_asn1_ctx, stdout);
 	talloc_report(tall_msgb_ctx, stdout);
 	//talloc_report(NULL, stdout);
+
+	test_common_cleanup();
 	printf("exit\n");
 	exit(0);
 }
diff --git a/src/tests/test_common.c b/src/tests/test_common.c
index c8aafdd..0af8ceb 100644
--- a/src/tests/test_common.c
+++ b/src/tests/test_common.c
@@ -69,11 +69,13 @@
 	.num_cat = ARRAY_SIZE(log_cat),
 };
 
+static void *msgb_ctx;
+
 int test_common_init(void)
 {
 	int rc;
 
-	msgb_talloc_ctx_init(NULL, 0);
+	msgb_ctx = msgb_talloc_ctx_init(NULL, 0);
 	talloc_asn1_ctx = talloc_named_const(NULL, 0, "asn1_context");
 
 	rc = osmo_init_logging(&test_log_info);
@@ -85,3 +87,22 @@
 	log_set_print_filename(osmo_stderr_target, 0);
 	log_set_use_color(osmo_stderr_target, 0);
 }
+
+void test_common_cleanup(void)
+{
+	if (talloc_total_blocks(msgb_ctx) != 1
+	    || talloc_total_size(msgb_ctx) != 0)
+		talloc_report_full(msgb_ctx, stderr);
+
+	OSMO_ASSERT(talloc_total_blocks(msgb_ctx) == 1);
+	OSMO_ASSERT(talloc_total_size(msgb_ctx) == 0);
+	talloc_free(msgb_ctx);
+
+	if (talloc_total_blocks(talloc_asn1_ctx) != 1
+	    || talloc_total_size(talloc_asn1_ctx) != 0)
+		talloc_report_full(talloc_asn1_ctx, stderr);
+
+	OSMO_ASSERT(talloc_total_blocks(talloc_asn1_ctx) == 1);
+	OSMO_ASSERT(talloc_total_size(talloc_asn1_ctx) == 0);
+	talloc_free(talloc_asn1_ctx);
+}
diff --git a/src/tests/test_common.h b/src/tests/test_common.h
index 1af1abd..836d999 100644
--- a/src/tests/test_common.h
+++ b/src/tests/test_common.h
@@ -1,3 +1,4 @@
 #pragma once
 
 int test_common_init(void);
+void test_common_cleanup(void);

-- 
To view, visit https://gerrit.osmocom.org/4916
To unsubscribe, visit https://gerrit.osmocom.org/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Ic66c005f2a264774e18bb54e58b87bef5944511c
Gerrit-PatchSet: 4
Gerrit-Project: osmo-iuh
Gerrit-Branch: master
Gerrit-Owner: Neels Hofmeyr <nhofmeyr at sysmocom.de>
Gerrit-Reviewer: Harald Welte <laforge at gnumonks.org>
Gerrit-Reviewer: Holger Freyther <holger at freyther.de>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: Neels Hofmeyr <nhofmeyr at sysmocom.de>



More information about the gerrit-log mailing list