Skip to content

Commit 5e0cb49

Browse files
committed
Eliminate VLA for stack safety and portability
Variable-length arrays (VLAs) were banned from the Linux kernel starting with 4.20 due to stack overflow risks and unpredictable stack usage. This replaces the VLA in picosynth_process() with a fixed-size scratch array bounded by PICOSYNTH_MAX_NODES (default 32). Stack usage is now predictable: 128 bytes per voice processing call.
1 parent b3b0dfd commit 5e0cb49

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

include/picosynth.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,19 @@
4444
#error "PICOSYNTH_BLOCK_SIZE must be <= 255 (uint8_t block_counter)"
4545
#endif
4646

47+
/* Maximum nodes per voice. Fixed-size scratch array avoids VLAs
48+
* (banned from Linux kernel due to stack overflow risk).
49+
* Default 32 is sufficient for complex patches; increase if needed.
50+
* picosynth_create() returns NULL if nodes exceeds this limit.
51+
*/
52+
#ifndef PICOSYNTH_MAX_NODES
53+
#define PICOSYNTH_MAX_NODES 32
54+
#endif
55+
56+
#if PICOSYNTH_MAX_NODES > 255
57+
#error "PICOSYNTH_MAX_NODES must be <= 255 (uint8_t n_nodes)"
58+
#endif
59+
4760
/**
4861
* Q15 fixed-point: signed 16-bit, 15 fractional bits.
4962
* Range: [-1.0, +1.0) as [-32768, +32767].

src/picosynth.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,9 @@ static void voice_update_usage_mask(picosynth_voice_t *v)
153153

154154
picosynth_t *picosynth_create(uint8_t voices, uint8_t nodes)
155155
{
156+
if (nodes > PICOSYNTH_MAX_NODES)
157+
return NULL;
158+
156159
picosynth_t *s = calloc(1, sizeof(picosynth_t));
157160
if (!s)
158161
return NULL;
@@ -475,7 +478,7 @@ q15_t picosynth_process(picosynth_t *s)
475478

476479
picosynth_voice_t *v = &s->voices[vi];
477480
picosynth_node_t *nodes = v->nodes;
478-
int32_t tmp[v->n_nodes];
481+
int32_t tmp[PICOSYNTH_MAX_NODES];
479482

480483
/* Two-pass processing per voice:
481484
* 1. Compute outputs from current state of all nodes.

0 commit comments

Comments
 (0)