From b8432a21a5d4868ca0c85158f8caec7e4f2e6e11 Mon Sep 17 00:00:00 2001 From: darkstar Date: Wed, 21 Feb 2018 23:33:16 +0100 Subject: [PATCH] SOUND: Use alloca() for stack allocations Using an array with a variable size is a gcc extension and is not portable. The _alloca() method works but should be reworked at some point to make use of a static buffer --- src/sound/snd_adlibgold.c | 10 ++++++++++ src/sound/snd_dbopl.cc | 26 ++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/src/sound/snd_adlibgold.c b/src/sound/snd_adlibgold.c index 3ae661e..a21aa7a 100644 --- a/src/sound/snd_adlibgold.c +++ b/src/sound/snd_adlibgold.c @@ -683,7 +683,17 @@ void adgold_timer_poll(void *p) static void adgold_get_buffer(int32_t *buffer, int len, void *p) { adgold_t *adgold = (adgold_t *)p; +#ifdef _MSC_VER + /* TODO: Fix this to use a static buffer */ + if (len > 512 * 1024) + { + pclog("adgold_get_buffer: possible stack overflow detected. Buffer size was %d bytes", 2 * len); + return; + } + int16_t *adgold_buffer = (int16_t *)_alloca(len * 2); +#else int16_t adgold_buffer[len*2]; +#endif int c; diff --git a/src/sound/snd_dbopl.cc b/src/sound/snd_dbopl.cc index 00af31d..2e6baa2 100644 --- a/src/sound/snd_dbopl.cc +++ b/src/sound/snd_dbopl.cc @@ -40,6 +40,12 @@ #include "dbopl.h" #include "nukedopl.h" #include "snd_dbopl.h" +#ifdef _MSC_VER +/* for _alloca() and printing of the related error message with pclog() */ +#include +#include +#include "../emu.h" +#endif int opl3_type = 0; @@ -184,7 +190,17 @@ uint8_t opl_read(int nr, uint16_t addr) void opl2_update(int nr, int16_t *buffer, int samples) { int c; +#ifdef _MSC_VER + /* TODO: Fix this to use a static buffer */ + if (samples > 512*1024) + { + pclog("opl2_update: possible stack overflow detected. sample count was %d", samples); + return; + } + Bit32s *buffer_32 = (Bit32s *)_alloca(samples); +#else Bit32s buffer_32[samples]; +#endif opl[nr].chip.GenerateBlock2(samples, buffer_32); @@ -195,7 +211,17 @@ void opl2_update(int nr, int16_t *buffer, int samples) void opl3_update(int nr, int16_t *buffer, int samples) { int c; +#ifdef _MSC_VER + /* TODO: Fix this to use a static buffer */ + if (samples > 512 * 1024) + { + pclog("opl2_update: possible stack overflow detected. sample count was %d", samples); + return; + } + Bit32s *buffer_32 = (Bit32s *)_alloca(samples); +#else Bit32s buffer_32[samples*2]; +#endif if (opl3_type) {