From d4b472d2e5f88ff32bbcb516260d395963e93f8f Mon Sep 17 00:00:00 2001 From: zuozhiwei Date: Fri, 17 Apr 2026 10:04:35 +0800 Subject: [PATCH] tools: fix realloc failure handling in midifile ensure_buffer On realloc failure, the old mf->buffer pointer should be preserverd to avoid memory leaks. --- src/tools/midifile.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/tools/midifile.c b/src/tools/midifile.c index 974cec6e5..6bb70f1c0 100644 --- a/src/tools/midifile.c +++ b/src/tools/midifile.c @@ -152,7 +152,12 @@ static uint8_t *ensure_buffer(struct midi_file *mf, struct midi_track *tr, size_ return tr->event; if (size > mf->buffer_size) { - mf->buffer = realloc(mf->buffer, size); + uint8_t *newbuf = realloc(mf->buffer, size); + + if (newbuf == NULL) + return NULL; + + mf->buffer = newbuf; mf->buffer_size = size; } return mf->buffer;