From 6a1c90b6d82c2b12d50fd07c6dae8fd1714e6a21 Mon Sep 17 00:00:00 2001 From: krcroft Date: Thu, 16 Jan 2020 14:43:03 -0800 Subject: [PATCH] Fix NULL issues in OPL code The 'ptr' pointer in the 'ptr += sizeof (FM_OPL)' expression could be nullptr. In such case, resulting value will be senseless and it should not be used. --- src/hardware/mame/fmopl.cpp | 14 +++++++------- src/hardware/mame/ymf262.cpp | 8 ++++++++ 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/hardware/mame/fmopl.cpp b/src/hardware/mame/fmopl.cpp index 90b88345..6f5b3dc5 100644 --- a/src/hardware/mame/fmopl.cpp +++ b/src/hardware/mame/fmopl.cpp @@ -1958,23 +1958,23 @@ static void OPL_clock_changed(FM_OPL *OPL, uint32_t clock, uint32_t rate) /* 'rate' is sampling rate */ static FM_OPL *OPLCreate(device_t *device, uint32_t clock, uint32_t rate, int type) { - char *ptr; - FM_OPL *OPL; - int state_size; - if (FM_OPL::LockTable(device) == -1) return 0; /* calculate OPL state size */ - state_size = sizeof(FM_OPL); + int state_size = sizeof(FM_OPL); #if BUILD_Y8950 if (type&OPL_TYPE_ADPCM) state_size+= sizeof(YM_DELTAT); #endif /* allocate memory block */ - ptr = (char *)auto_alloc_array_clear(device->machine(), uint8_t, state_size); + char *ptr = (char *)auto_alloc_array_clear(device->machine(), uint8_t, state_size); + if (!ptr) { + device->logerror("Could not allocate memory during device creation"); + return 0; + } - OPL = (FM_OPL *)ptr; + FM_OPL *OPL = (FM_OPL *)ptr; ptr += sizeof(FM_OPL); diff --git a/src/hardware/mame/ymf262.cpp b/src/hardware/mame/ymf262.cpp index 98056c5d..fd2e940a 100644 --- a/src/hardware/mame/ymf262.cpp +++ b/src/hardware/mame/ymf262.cpp @@ -2353,12 +2353,20 @@ static void OPL3ResetChip(OPL3 *chip) /* 'rate' is sampling rate */ static OPL3 *OPL3Create(device_t *device, int clock, int rate, int type) { + // Guard + if (device == nullptr) { + return 0; + } OPL3 *chip; if (OPL3_LockTable(device) == -1) return 0; /* allocate memory block */ chip = auto_alloc_clear(device->machine(), OPL3 ); + if (chip == nullptr) { + device->logerror("Could not allocate memory for OPL3 chip"); + return 0; + } chip->device = device; chip->type = type;