From d52d30cf3c10dcb222216207fb5cd5e33a335852 Mon Sep 17 00:00:00 2001 From: Cuda-Chen Date: Fri, 2 Jan 2026 13:24:03 +0800 Subject: [PATCH] virtio-snd: Fix dynamic memory allocation for PCM I/O frames Fix the PCM I/O transfer mechanism when allocating memory during frame enqueuing and freeing it upon dequeuing. --- virtio-snd.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/virtio-snd.c b/virtio-snd.c index 2eaf9ac3..f35d7f9d 100644 --- a/virtio-snd.c +++ b/virtio-snd.c @@ -799,8 +799,11 @@ static void __virtio_snd_frame_dequeue(void *out, written_bytes += len; node->pos += len; - if (node->pos >= node->len) + if (node->pos >= node->len) { list_del(&node->q); + free(node->addr); + free(node); + } } props->lock.buf_ev_notify--; @@ -929,15 +932,20 @@ static void __virtio_snd_frame_enqueue(void *payload, * [2] * https://github.com/rust-vmm/vhost-device/blob/eb2e2227e41d48a52e4e6346189b772c5363879d/staging/vhost-device-sound/src/device.rs#L554 */ - /* FIXME: locate the root case of repeating artifact even we - * keep the pointer of the payload. - */ vsnd_buf_queue_node_t *node = malloc(sizeof(*node)); - node->addr = payload; + if (!node) + goto tx_frame_enqueue_final; + node->addr = malloc(sizeof(*node->addr) * n); + if (!node->addr) { + free(node); + goto tx_frame_enqueue_final; + } + memcpy(node->addr, payload, n); node->len = n; node->pos = 0; list_push(&node->q, &props->buf_queue_head); +tx_frame_enqueue_final: pthread_mutex_unlock(&props->lock.lock); }