From c4037d4aa3f1c3a7ed03f00de3e0420346d64f52 Mon Sep 17 00:00:00 2001
From: Sean Bright <sean.bright@gmail.com>
Date: Wed, 8 Sep 2021 15:29:56 -0400
Subject: [PATCH] test_abstract_jb.c: Fix put and put_out_of_order memory
 leaks.

We can't rely on RAII_VAR(...) to properly clean up data that is
allocated within a loop.

ASTERISK-27176 #close

Change-Id: Ib575616101230c4f603519114ec62ebf3936882c
---
 tests/test_abstract_jb.c | 37 ++++++++++++++++++++++++++++---------
 1 file changed, 28 insertions(+), 9 deletions(-)

diff --git a/tests/test_abstract_jb.c b/tests/test_abstract_jb.c
index 53614bca13..76eb4bffec 100644
--- a/tests/test_abstract_jb.c
+++ b/tests/test_abstract_jb.c
@@ -293,8 +293,8 @@ static struct ast_jb default_jb = {
 	RAII_VAR(struct ast_jb *, jb, &default_jb, dispose_jitterbuffer); \
 	const struct ast_jb_impl *impl; \
 	struct ast_jb_conf conf; \
-	RAII_VAR(struct ast_frame *, expected_frame, NULL, ast_frame_dtor); \
-	RAII_VAR(struct ast_frame *, actual_frame, NULL, ast_frame_dtor); \
+	struct ast_frame *expected_frame = NULL; \
+	struct ast_frame *actual_frame = NULL; \
 	int res; \
 	long next; \
 	int i; \
@@ -318,7 +318,15 @@ static struct ast_jb default_jb = {
 	jb->impl = impl; \
 \
 	expected_frame = create_test_frame(1000, 0); \
-	jb->impl->put_first(jb->jbobj, expected_frame, 1100); \
+	res = jb->impl->put_first(jb->jbobj, \
+		expected_frame, \
+		1100); \
+	if (res != AST_JB_IMPL_OK) { \
+		ast_test_status_update(test, "Error: On first frame, got %d back from put_first (expected %d)\n", \
+			res, AST_JB_IMPL_OK); \
+		ast_frame_dtor(expected_frame); \
+		return AST_TEST_FAIL; \
+	} \
 	for (i = 1; i < 10; i++) { \
 		expected_frame = create_test_frame(1000 + i * DEFAULT_FRAME_MS, 0); \
 		res = jb->impl->put(jb->jbobj, \
@@ -327,6 +335,7 @@ static struct ast_jb default_jb = {
 		if (res != AST_JB_IMPL_OK) { \
 			ast_test_status_update(test, "Error: On frame %d, got %d back from put (expected %d)\n", \
 				i, res, AST_JB_IMPL_OK); \
+			ast_frame_dtor(expected_frame); \
 			return AST_TEST_FAIL; \
 		} \
 	} \
@@ -338,11 +347,12 @@ static struct ast_jb default_jb = {
 		if (res != AST_JB_IMPL_OK) { \
 			ast_test_status_update(test, "Error: failed to retrieve frame %i at time %ld\n", \
 				i, next); \
+			ast_frame_dtor(expected_frame); \
 			return AST_TEST_FAIL; \
 		} \
 		VERIFY_FRAME(actual_frame, expected_frame); \
-		ast_frfree(expected_frame); \
-		expected_frame = NULL; \
+		ast_frame_dtor(expected_frame); \
+		ast_frame_dtor(actual_frame); \
 	} \
 	return AST_TEST_PASS; \
 }
@@ -427,8 +437,8 @@ static struct ast_jb default_jb = {
 	RAII_VAR(struct ast_jb *, jb, &default_jb, dispose_jitterbuffer); \
 	const struct ast_jb_impl *impl; \
 	struct ast_jb_conf conf; \
-	RAII_VAR(struct ast_frame *, actual_frame, NULL, ast_frame_dtor); \
-	RAII_VAR(struct ast_frame *, expected_frame, NULL, ast_frame_dtor); \
+	struct ast_frame *expected_frame = NULL; \
+	struct ast_frame *actual_frame = NULL; \
 	int res; \
 	long next; \
 	int i; \
@@ -454,7 +464,13 @@ static struct ast_jb default_jb = {
 	jb->impl = impl; \
 \
 	expected_frame = create_test_frame(1000, 0); \
-	jb->impl->put_first(jb->jbobj, expected_frame, 1100); \
+	res = jb->impl->put_first(jb->jbobj, expected_frame, 1100); \
+	if (res != AST_JB_IMPL_OK) { \
+		ast_test_status_update(test, "Error: On first frame, got %d back from put_first (expected %d)\n", \
+			res, AST_JB_IMPL_OK); \
+		ast_frame_dtor(expected_frame); \
+		return AST_TEST_FAIL; \
+	} \
 	for (i = 1; i <= 10; i++) { \
 		if (i % 3 == 1 && i != 10) { \
 			expected_frame = create_test_frame(1000 + ((i + 1) * DEFAULT_FRAME_MS), 0); \
@@ -469,6 +485,7 @@ static struct ast_jb default_jb = {
 		if (res != AST_JB_IMPL_OK) { \
 			ast_test_status_update(test, "Error: On frame %d, got %d back from put (expected %d)\n", \
 				i, res, AST_JB_IMPL_OK); \
+			ast_frame_dtor(expected_frame); \
 			return AST_TEST_FAIL; \
 		} \
 	} \
@@ -480,10 +497,12 @@ static struct ast_jb default_jb = {
 		if (res != AST_JB_IMPL_OK) { \
 			ast_test_status_update(test, "Error: failed to retrieve frame at %ld\n", \
 				next); \
+			ast_frame_dtor(expected_frame); \
 			return AST_TEST_FAIL; \
 		} \
 		VERIFY_FRAME(actual_frame, expected_frame); \
-		ast_frfree(expected_frame); \
+		ast_frame_dtor(expected_frame); \
+		ast_frame_dtor(actual_frame); \
 		expected_frame = NULL; \
 	} \
 \
-- 
GitLab