From 6951801bbc8f51be5db0b9262ac5e5349bf7d9d5 Mon Sep 17 00:00:00 2001 From: Loic Coenen Date: Sun, 3 May 2026 19:27:00 +0000 Subject: [PATCH] fix: replace real-time unsafe malloc with stack buffers in process_callback Co-authored-by: aider (deepseek/deepseek-coder) --- engine.c | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/engine.c b/engine.c index 0eac350..aa0af00 100644 --- a/engine.c +++ b/engine.c @@ -1,5 +1,7 @@ #include "engine.h" #include "carla.h" + +#define MAX_NFRAMES 8192 #include #include #include @@ -125,13 +127,11 @@ static int process_callback(jack_nframes_t nframes, void *arg) { for (int ch = 0; ch < MAX_CHANNELS; ch++) { memset(audio_out[ch], 0, sizeof(jack_default_audio_sample_t) * nframes); - // Create temporary buffer for rack processing - float *rack_in = malloc(nframes * sizeof(float)); - float *rack_out = malloc(nframes * sizeof(float)); - - if (!rack_in || !rack_out) { - free(rack_in); - free(rack_out); + // Use stack-allocated buffers (max MAX_NFRAMES samples) + float rack_in[MAX_NFRAMES]; + float rack_out[MAX_NFRAMES]; + if (nframes > MAX_NFRAMES) { + // Should never happen with JACK, but guard continue; } @@ -164,8 +164,6 @@ static int process_callback(jack_nframes_t nframes, void *arg) { // Copy to output memcpy(audio_out[ch], rack_out, nframes * sizeof(jack_default_audio_sample_t)); - free(rack_in); - free(rack_out); } return 0;