From 913ed609a3c3c73f39db8a015fa42514a133f5a0 Mon Sep 17 00:00:00 2001 From: Mark Jocas Date: Mon, 1 Jun 2026 17:05:33 +0200 Subject: [PATCH] Makes buzzer melody buffer static Ensures the RTTTL player has a persistent buffer for melody data. The player stores a pointer to the melody, which requires the buffer to outlive the function call. A static buffer guarantees this, preventing issues from accessing deallocated memory. This is safe as concurrent playback is not supported. --- src/helpers/ui/buzzer.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/helpers/ui/buzzer.cpp b/src/helpers/ui/buzzer.cpp index 4ac7e74c..ee5fc4ce 100644 --- a/src/helpers/ui/buzzer.cpp +++ b/src/helpers/ui/buzzer.cpp @@ -49,8 +49,10 @@ void genericBuzzer::playToggle(int count, bool enabled) { const int max_notes = (int)(sizeof(notes) / sizeof(notes[0])); if (count < 1) count = 1; if (count > max_notes) count = max_notes; - char melody[64]; - // d=8 (eighths) at b=180 -> ~166 ms per note, audible on small piezos. + // NonBlockingRtttl stores only a pointer to the melody, so the backing + // buffer must outlive the call. A static buffer is fine here because the + // library can't play two melodies at once anyway. + static char melody[64]; int n = snprintf(melody, sizeof(melody), "Tg:d=8,o=6,b=180:"); for (int i = 0; i < count && n < (int)sizeof(melody); i++) { int idx = enabled ? i : (count - 1 - i);