From 9916b263831f20e5841275051a8ed014de1f24eb Mon Sep 17 00:00:00 2001 From: Colin Snover Date: Wed, 29 Nov 2017 00:06:12 -0600 Subject: SCUMM: Replace UB-triggering serialization code with Common::Serializer Fixes Trac#10342. --- engines/scumm/imuse/instrument.cpp | 106 ++++++++++++++++--------------------- 1 file changed, 47 insertions(+), 59 deletions(-) (limited to 'engines/scumm/imuse/instrument.cpp') diff --git a/engines/scumm/imuse/instrument.cpp b/engines/scumm/imuse/instrument.cpp index 670b3647eb..708a98046b 100644 --- a/engines/scumm/imuse/instrument.cpp +++ b/engines/scumm/imuse/instrument.cpp @@ -20,7 +20,6 @@ * */ - #include "scumm/scumm.h" #include "scumm/saveload.h" #include "scumm/imuse/instrument.h" @@ -132,8 +131,8 @@ private: public: Instrument_Program(byte program, bool mt32); - Instrument_Program(Serializer *s); - void saveOrLoad(Serializer *s); + Instrument_Program(Common::Serializer &s); + void saveLoadWithSerializer(Common::Serializer &s); void send(MidiChannel *mc); void copy_to(Instrument *dest) { dest->program(_program, _mt32); } bool is_valid() { @@ -178,8 +177,8 @@ private: public: Instrument_AdLib(const byte *data); - Instrument_AdLib(Serializer *s); - void saveOrLoad(Serializer *s); + Instrument_AdLib(Common::Serializer &s); + void saveLoadWithSerializer(Common::Serializer &s); void send(MidiChannel *mc); void copy_to(Instrument *dest) { dest->adlib((byte *)&_instrument); } bool is_valid() { return true; } @@ -259,8 +258,8 @@ private: public: Instrument_Roland(const byte *data); - Instrument_Roland(Serializer *s); - void saveOrLoad(Serializer *s); + Instrument_Roland(Common::Serializer &s); + void saveLoadWithSerializer(Common::Serializer &s); void send(MidiChannel *mc); void copy_to(Instrument *dest) { dest->roland((byte *)&_instrument); } bool is_valid() { return (_native_mt32 ? true : (_instrument_name[0] != '\0')); } @@ -269,8 +268,8 @@ public: class Instrument_PcSpk : public InstrumentInternal { public: Instrument_PcSpk(const byte *data); - Instrument_PcSpk(Serializer *s); - void saveOrLoad(Serializer *s); + Instrument_PcSpk(Common::Serializer &s); + void saveLoadWithSerializer(Common::Serializer &s); void send(MidiChannel *mc); void copy_to(Instrument *dest) { dest->pcspk((byte *)&_instrument); } bool is_valid() { return true; } @@ -285,8 +284,8 @@ private: public: Instrument_MacSfx(byte program); - Instrument_MacSfx(Serializer *s); - void saveOrLoad(Serializer *s); + Instrument_MacSfx(Common::Serializer &s); + void saveLoadWithSerializer(Common::Serializer &s); void send(MidiChannel *mc); void copy_to(Instrument *dest) { dest->macSfx(_program); } bool is_valid() { @@ -350,14 +349,14 @@ void Instrument::macSfx(byte prog) { _instrument = new Instrument_MacSfx(prog); } -void Instrument::saveOrLoad(Serializer *s) { - if (s->isSaving()) { - s->saveByte(_type); +void Instrument::saveLoadWithSerializer(Common::Serializer &s) { + if (s.isSaving()) { + s.syncAsByte(_type); if (_instrument) - _instrument->saveOrLoad(s); + _instrument->saveLoadWithSerializer(s); } else { clear(); - _type = s->loadByte(); + s.syncAsByte(_type); switch (_type) { case itNone: break; @@ -396,20 +395,21 @@ Instrument_Program::Instrument_Program(byte program, bool mt32) : _program = 255; } -Instrument_Program::Instrument_Program(Serializer *s) { +Instrument_Program::Instrument_Program(Common::Serializer &s) { _program = 255; _mt32 = false; - if (!s->isSaving()) - saveOrLoad(s); + if (!s.isSaving()) + saveLoadWithSerializer(s); } -void Instrument_Program::saveOrLoad(Serializer *s) { - if (s->isSaving()) { - s->saveByte(_program); - s->saveByte(_mt32 ? 1 : 0); +void Instrument_Program::saveLoadWithSerializer(Common::Serializer &s) { + s.syncAsByte(_program); + if (s.isSaving()) { + s.syncAsByte(_mt32); } else { - _program = s->loadByte(); - _mt32 = (s->loadByte() > 0); + byte tmp; + s.syncAsByte(tmp); + _mt32 = (tmp > 0); } } @@ -434,18 +434,15 @@ Instrument_AdLib::Instrument_AdLib(const byte *data) { memcpy(&_instrument, data, sizeof(_instrument)); } -Instrument_AdLib::Instrument_AdLib(Serializer *s) { - if (!s->isSaving()) - saveOrLoad(s); +Instrument_AdLib::Instrument_AdLib(Common::Serializer &s) { + if (!s.isSaving()) + saveLoadWithSerializer(s); else memset(&_instrument, 0, sizeof(_instrument)); } -void Instrument_AdLib::saveOrLoad(Serializer *s) { - if (s->isSaving()) - s->saveBytes(&_instrument, sizeof(_instrument)); - else - s->loadBytes(&_instrument, sizeof(_instrument)); +void Instrument_AdLib::saveLoadWithSerializer(Common::Serializer &s) { + s.syncBytes((byte *)(&_instrument), sizeof(_instrument)); } void Instrument_AdLib::send(MidiChannel *mc) { @@ -468,26 +465,24 @@ Instrument_Roland::Instrument_Roland(const byte *data) { } } -Instrument_Roland::Instrument_Roland(Serializer *s) { +Instrument_Roland::Instrument_Roland(Common::Serializer &s) { _instrument_name[0] = '\0'; - if (!s->isSaving()) - saveOrLoad(s); + if (!s.isSaving()) + saveLoadWithSerializer(s); else memset(&_instrument, 0, sizeof(_instrument)); } -void Instrument_Roland::saveOrLoad(Serializer *s) { - if (s->isSaving()) { - s->saveBytes(&_instrument, sizeof(_instrument)); - } else { - s->loadBytes(&_instrument, sizeof(_instrument)); +void Instrument_Roland::saveLoadWithSerializer(Common::Serializer &s) { + s.syncBytes((byte *)(&_instrument), sizeof(_instrument)); + if (!s.isSaving()) { memcpy(&_instrument_name, &_instrument.common.name, sizeof(_instrument.common.name)); _instrument_name[10] = '\0'; if (!_native_mt32 && getEquivalentGM() >= 128) { debug(2, "MT-32 custom instrument \"%s\" not supported", _instrument_name); _instrument_name[0] = '\0'; } - } // end if + } } void Instrument_Roland::send(MidiChannel *mc) { @@ -538,18 +533,15 @@ Instrument_PcSpk::Instrument_PcSpk(const byte *data) { memcpy(_instrument, data, sizeof(_instrument)); } -Instrument_PcSpk::Instrument_PcSpk(Serializer *s) { - if (!s->isSaving()) - saveOrLoad(s); +Instrument_PcSpk::Instrument_PcSpk(Common::Serializer &s) { + if (!s.isSaving()) + saveLoadWithSerializer(s); else memset(_instrument, 0, sizeof(_instrument)); } -void Instrument_PcSpk::saveOrLoad(Serializer *s) { - if (s->isSaving()) - s->saveBytes(_instrument, sizeof(_instrument)); - else - s->loadBytes(_instrument, sizeof(_instrument)); +void Instrument_PcSpk::saveLoadWithSerializer(Common::Serializer &s) { + s.syncBytes(_instrument, sizeof(_instrument)); } void Instrument_PcSpk::send(MidiChannel *mc) { @@ -569,19 +561,15 @@ Instrument_MacSfx::Instrument_MacSfx(byte program) : } } -Instrument_MacSfx::Instrument_MacSfx(Serializer *s) { +Instrument_MacSfx::Instrument_MacSfx(Common::Serializer &s) { _program = 255; - if (!s->isSaving()) { - saveOrLoad(s); + if (!s.isSaving()) { + saveLoadWithSerializer(s); } } -void Instrument_MacSfx::saveOrLoad(Serializer *s) { - if (s->isSaving()) { - s->saveByte(_program); - } else { - _program = s->loadByte(); - } +void Instrument_MacSfx::saveLoadWithSerializer(Common::Serializer &s) { + s.syncAsByte(_program); } void Instrument_MacSfx::send(MidiChannel *mc) { -- cgit v1.2.3