From f26ce1e4ffba89d6e23e3957d0dc4aa8907bfa73 Mon Sep 17 00:00:00 2001 From: Le Philousophe Date: Sun, 25 Sep 2022 10:53:28 +0200 Subject: [PATCH] GOB: Don't use unsafe strcat and strcpy --- engines/gob/draw.cpp | 2 +- engines/gob/hotspots.cpp | 2 +- engines/gob/inter.cpp | 2 +- engines/gob/inter_v2.cpp | 8 ++++---- engines/gob/map_v1.cpp | 8 ++++---- engines/gob/util.cpp | 8 +++++--- engines/gob/util.h | 2 +- engines/gob/variables.cpp | 2 +- 8 files changed, 18 insertions(+), 16 deletions(-) diff --git a/engines/gob/draw.cpp b/engines/gob/draw.cpp index 360edbe13b1..2ae69dc6ac9 100644 --- a/engines/gob/draw.cpp +++ b/engines/gob/draw.cpp @@ -514,7 +514,7 @@ void Draw::oPlaytoons_sub_F_1B(uint16 id, int16 left, int16 top, int16 right, in _vm->_game->_script->pop(); } - strcpy(paramStr, tmpStr); + Common::strcpy_s(paramStr, 200, tmpStr); if (fontIndex >= kFontCount) { warning("Draw::oPlaytoons_sub_F_1B(): Font %d > Count %d", fontIndex, kFontCount); diff --git a/engines/gob/hotspots.cpp b/engines/gob/hotspots.cpp index 03a27247d81..a960112264c 100644 --- a/engines/gob/hotspots.cpp +++ b/engines/gob/hotspots.cpp @@ -917,7 +917,7 @@ uint16 Hotspots::updateInput(uint16 xPos, uint16 yPos, uint16 width, uint16 heig while (1) { // If we the edit field has enough space, add a space for the new character Common::strlcpy(tempStr, str, 255); - strcat(tempStr, " "); + Common::strcat_s(tempStr, " "); if ((editSize != 0) && strlen(tempStr) > editSize) Common::strlcpy(tempStr, str, 256); diff --git a/engines/gob/inter.cpp b/engines/gob/inter.cpp index f81fb13e07f..c0145500a7a 100644 --- a/engines/gob/inter.cpp +++ b/engines/gob/inter.cpp @@ -404,7 +404,7 @@ void Inter::storeString(uint16 index, uint16 type, const char *value) { case TYPE_IMM_INT8: case TYPE_VAR_INT8: - strcpy(str, value); + Common::strcpy_s(str, maxLength, value); break; case TYPE_ARRAY_INT8: diff --git a/engines/gob/inter_v2.cpp b/engines/gob/inter_v2.cpp index caa0e6f6f63..c9c109540d6 100644 --- a/engines/gob/inter_v2.cpp +++ b/engines/gob/inter_v2.cpp @@ -1565,7 +1565,7 @@ void Inter_v2::o2_loadInfogramesIns(OpGobParams ¶ms) { varName = _vm->_game->_script->readInt16(); Common::strlcpy(fileName, GET_VAR_STR(varName), 16); - strcat(fileName, ".INS"); + Common::strcat_s(fileName, ".INS"); _vm->_sound->infogramesLoadInstruments(fileName); } @@ -1577,7 +1577,7 @@ void Inter_v2::o2_playInfogrames(OpGobParams ¶ms) { varName = _vm->_game->_script->readInt16(); Common::strlcpy(fileName, GET_VAR_STR(varName), 16); - strcat(fileName, ".DUM"); + Common::strcat_s(fileName, ".DUM"); _vm->_sound->infogramesLoadSong(fileName); _vm->_sound->infogramesPlay(); @@ -1662,9 +1662,9 @@ int16 Inter_v2::loadSound(int16 search) { Common::strlcpy(sndfile, _vm->_game->_script->readString(9), 10); if (type == SOUND_ADL) - strcat(sndfile, ".ADL"); + Common::strcat_s(sndfile, ".ADL"); else - strcat(sndfile, ".SND"); + Common::strcat_s(sndfile, ".SND"); int32 dataSize; byte *dataPtr = _vm->_dataIO->getFile(sndfile, dataSize); diff --git a/engines/gob/map_v1.cpp b/engines/gob/map_v1.cpp index 76276470d2f..dc0e93866c1 100644 --- a/engines/gob/map_v1.cpp +++ b/engines/gob/map_v1.cpp @@ -65,8 +65,8 @@ void Map_v1::loadMapObjects(const char *avjFile) { uint32 gobsPos; uint32 objsPos; - strcpy(avoName, _sourceFile); - strcat(avoName, ".avo"); + Common::strcpy_s(avoName, sizeof(avoName) - 4, _sourceFile); + Common::strcat_s(avoName, ".avo"); int32 size; dataBuf = _vm->_dataIO->getFile(avoName, size); @@ -151,8 +151,8 @@ void Map_v1::loadSounds(Common::SeekableReadStream &data) { for (int i = 0; i < count; i++) { data.read(buf, 14); buf[14] = 0; - strcat(buf, ".SND"); - strcpy(sndNames[i], buf); + Common::strcat_s(buf, ".SND"); + Common::strcpy_s(sndNames[i], buf); } _vm->_sound->sampleLoad(&_vm->_goblin->_soundData[14], SOUND_SND, "diamant1.snd"); diff --git a/engines/gob/util.cpp b/engines/gob/util.cpp index c9f315c30c2..65e305affb6 100644 --- a/engines/gob/util.cpp +++ b/engines/gob/util.cpp @@ -534,9 +534,9 @@ void Util::cleanupStr(char *str) { char *start, *end; char buf[300]; - strcpy(buf, trStr1); - strcat(buf, trStr2); - strcat(buf, trStr3); + Common::strcpy_s(buf, trStr1); + Common::strcat_s(buf, trStr2); + Common::strcat_s(buf, trStr3); // Translating "wrong" characters for (size_t i = 0; i < strlen(str); i++) @@ -620,6 +620,7 @@ void Util::deleteList(List *list) { delete list; } +#if 0 char *Util::setExtension(char *str, const char *ext) { assert(str && ext); @@ -633,6 +634,7 @@ char *Util::setExtension(char *str, const char *ext) { strcat(str, ext); return str; } +#endif Common::String Util::setExtension(const Common::String &str, const Common::String &ext) { if (str.empty()) diff --git a/engines/gob/util.h b/engines/gob/util.h index def95f612dc..c5938d93430 100644 --- a/engines/gob/util.h +++ b/engines/gob/util.h @@ -136,7 +136,7 @@ public: static void listDropFront(List *list); static void deleteList(List *list); - static char *setExtension(char *str, const char *ext); + //static char *setExtension(char *str, const char *ext); static Common::String setExtension(const Common::String &str, const Common::String &ext); /** Read a constant-length string out of a stream. */ diff --git a/engines/gob/variables.cpp b/engines/gob/variables.cpp index 4d7dba4d427..725d5371093 100644 --- a/engines/gob/variables.cpp +++ b/engines/gob/variables.cpp @@ -85,7 +85,7 @@ void Variables::writeOffString(uint32 offset, const char *value) { uint32 length = strlen(value); assert((offset + length + 1) < _size); - strcpy((char *)(_vars + offset), value); + Common::strcpy_s((char *)(_vars + offset), _size - offset, value); } uint8 Variables::readVar8(uint32 var) const {