Skip to content

Commit bcc3c7b

Browse files
authored
Merge pull request #119 from Cuda-Chen/fix-tx-mem-manage
virtio-snd: Fix dynamic memory allocation for PCM I/O frames
2 parents 6dba748 + d52d30c commit bcc3c7b

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

virtio-snd.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -799,8 +799,11 @@ static void __virtio_snd_frame_dequeue(void *out,
799799

800800
written_bytes += len;
801801
node->pos += len;
802-
if (node->pos >= node->len)
802+
if (node->pos >= node->len) {
803803
list_del(&node->q);
804+
free(node->addr);
805+
free(node);
806+
}
804807
}
805808

806809
props->lock.buf_ev_notify--;
@@ -929,15 +932,20 @@ static void __virtio_snd_frame_enqueue(void *payload,
929932
* [2]
930933
* https://github.com/rust-vmm/vhost-device/blob/eb2e2227e41d48a52e4e6346189b772c5363879d/staging/vhost-device-sound/src/device.rs#L554
931934
*/
932-
/* FIXME: locate the root case of repeating artifact even we
933-
* keep the pointer of the payload.
934-
*/
935935
vsnd_buf_queue_node_t *node = malloc(sizeof(*node));
936-
node->addr = payload;
936+
if (!node)
937+
goto tx_frame_enqueue_final;
938+
node->addr = malloc(sizeof(*node->addr) * n);
939+
if (!node->addr) {
940+
free(node);
941+
goto tx_frame_enqueue_final;
942+
}
943+
memcpy(node->addr, payload, n);
937944
node->len = n;
938945
node->pos = 0;
939946
list_push(&node->q, &props->buf_queue_head);
940947

948+
tx_frame_enqueue_final:
941949
pthread_mutex_unlock(&props->lock.lock);
942950
}
943951

0 commit comments

Comments
 (0)