[PATCH v1 2/2] tests/chamelium/audio: Fix resource leaks on assertion failure

Mohammed Bilal
2026-03-04T10:08:46Z

Fatal assertions can bypass cleanup paths, causing resources to remain
unreleased when failures occur during audio tests.

Replace fatal assertions with non-fatal checks and route failures
through a common cleanup path to ensure proper cleanup.

Signed-off-by: Mohammed Bilal <mohammed.bilal@intel.com>
---
tests/chamelium/kms_chamelium_audio.c | 43 ++++++++++++++++++++-------
1 file changed, 33 insertions(+), 10 deletions(-)

diff --git a/tests/chamelium/kms_chamelium_audio.c b/tests/chamelium/kms_chamelium_audio.c
index 2967e3c50..41cbe92d9 100644
--- a/tests/chamelium/kms_chamelium_audio.c
+++ b/tests/chamelium/kms_chamelium_audio.c
@@ -426,9 +426,21 @@ static bool test_audio_frequencies(struct audio_state *state)

audio_state_start(state, "frequencies");

- igt_assert_f(state->capture.rate == state->playback.rate,
- "Capture rate (%dHz) doesn't match playback rate (%dHz)\n",
- state->capture.rate, state->playback.rate);
+ /* Initialize heap pointers to NULL so the cleanup path can safely
+ * free them regardless of where an error occurs.
+ */
+ channel = NULL;
+ buf = NULL;
+ recv = NULL;
+ recv_len = 0;
+ success = false;
+
+ if (state->capture.rate != state->playback.rate) {
+ igt_critical("Capture rate (%dHz) doesn't match playback "
+ "rate (%dHz)\n",
+ state->capture.rate, state->playback.rate);
+ goto out;
+ }

/* Needs to be a multiple of 128, because that's the number of samples
* we get per channel each time we receive an audio page from the
@@ -447,10 +459,6 @@ static bool test_audio_frequencies(struct audio_state *state)
buf = malloc(sizeof(int32_t) * buf_cap);
buf_len = 0;

- recv = NULL;
- recv_len = 0;
-
- success = false;
streak = 0;
while (!success && state->msec < AUDIO_TIMEOUT) {
audio_state_receive(state, &recv, &recv_len);
@@ -460,13 +468,21 @@ static bool test_audio_frequencies(struct audio_state *state)

if (buf_len < buf_cap)
continue;
- igt_assert(buf_len == buf_cap);
+ if (buf_len != buf_cap) {
+ igt_critical("Unexpected buffer length: %zu != %zu\n",
+ buf_len, buf_cap);
+ goto out;
+ }

igt_debug("Detecting audio signal, t=%d msec\n", state->msec);

for (j = 0; j < state->playback.channels; j++) {
capture_chan = state->channel_mapping[j];
- igt_assert(capture_chan >= 0);
+ if (capture_chan < 0) {
+ igt_critical("Invalid channel mapping for "
+ "channel %zu\n", j);
+ goto out;
+ }
igt_debug("Processing channel %zu (captured as "
"channel %d)\n",
j, capture_chan);
@@ -489,6 +505,7 @@ static bool test_audio_frequencies(struct audio_state *state)
success = streak == MIN_STREAK * state->playback.channels;
}

+out:
audio_state_stop(state, success);

free(recv);
@@ -587,6 +604,7 @@ static bool test_audio_flatline(struct audio_state *state)
recv = NULL;
recv_len = 0;
amp_success = false;
+ align_success = false;
streak = 0;
while (!amp_success && state->msec < AUDIO_TIMEOUT) {
audio_state_receive(state, &recv, &recv_len);
@@ -595,7 +613,11 @@ static bool test_audio_flatline(struct audio_state *state)

for (i = 0; i < state->playback.channels; i++) {
capture_chan = state->channel_mapping[i];
- igt_assert(capture_chan >= 0);
+ if (capture_chan < 0) {
+ igt_critical("Invalid channel mapping for "
+ "channel %zu\n", i);
+ goto out;
+ }
igt_debug("Processing channel %zu (captured as "
"channel %d)\n",
i, capture_chan);
@@ -661,6 +683,7 @@ static bool test_audio_flatline(struct audio_state *state)
}
}

+out:
success = amp_success && align_success;
audio_state_stop(state, success);

--
2.48.1