CGE2: Don't use unsafe strcat and strcpy

This commit is contained in:
Le Philousophe 2022-09-24 12:30:37 +02:00 committed by Eugene Sandulenko
parent d698fc6bbe
commit ceca603464
6 changed files with 23 additions and 16 deletions

View File

@ -169,6 +169,7 @@ Sprite *CGE2Engine::loadSprite(const char *fname, int ref, int scene, V3D &pos)
ID id;
char tmpStr[kLineMax + 1];
STATIC_ASSERT(sizeof(tmpStr) >= kPathMax, mergeExt_expects_kPathMax_buffer);
mergeExt(tmpStr, fname, kSprExt);
if (_resman->exist(tmpStr)) { // sprite description file exist
@ -758,10 +759,10 @@ void CGE2Engine::cge2_main() {
}
char *CGE2Engine::mergeExt(char *buf, const char *name, const char *ext) {
strcpy(buf, name);
Common::strcpy_s(buf, kPathMax, name);
char *dot = strrchr(buf, '.');
if (!dot)
strcat(buf, ext);
Common::strcat_s(buf, kPathMax, ext);
return buf;
}
@ -777,8 +778,9 @@ void CGE2Engine::setEye(const V2D& e2, int z) {
}
void CGE2Engine::setEye(const char *s) {
char *tempStr = new char[strlen(s) + 1];
strcpy(tempStr, s);
size_t ln = strlen(s) + 1;
char *tempStr = new char[ln];
Common::strcpy_s(tempStr, ln, s);
_eye->_x = atoi(token(tempStr));
_eye->_y = atoi(token(nullptr));
_eye->_z = atoi(token(nullptr));

View File

@ -194,8 +194,9 @@ Sprite *Hero::expand() {
}
char *tempStr = _vm->_text->getText(_ref + 100);
char *text = new char[strlen(tempStr) + 1];
strcpy(text, tempStr);
size_t ln = strlen(tempStr) + 1;
char *text = new char[ln];
Common::strcpy_s(text, ln, tempStr);
_reachStart = atoi(_vm->token(text));
_reachCycle = atoi(_vm->token(nullptr));
_sayStart = atoi(_vm->token(nullptr));

View File

@ -61,7 +61,7 @@ Font::~Font() {
void Font::load() {
char path[10];
strcpy(path, "CGE.CFT");
Common::strcpy_s(path, "CGE.CFT");
if (!_vm->_resman->exist(path))
error("Missing Font file! %s", path);
@ -78,7 +78,7 @@ void Font::load() {
}
fontFile.read(_map, p);
strcpy(path, "CGE.TXC");
Common::strcpy_s(path, "CGE.TXC");
if (!_vm->_resman->exist(path))
error("Missing Color file! %s", path);

View File

@ -47,7 +47,7 @@ Text::Text(CGE2Engine *vm, const char *fname) : _vm(vm) {
_cache[_txtCount - 1]._ref = -1;
_cache[_txtCount - 1]._text = new char[3];
strcpy(_cache[_txtCount - 1]._text, "");
_cache[_txtCount - 1]._text[0] = '\0';
}
Text::~Text() {
@ -113,8 +113,9 @@ void Text::load() {
++s;
_cache[idx]._ref = r;
_cache[idx]._text = new char[strlen(s) + 1];
strcpy(_cache[idx]._text, s);
size_t ln = strlen(s) + 1;
_cache[idx]._text = new char[ln];
Common::strcpy_s(_cache[idx]._text, ln, s);
idx++;
}
}

View File

@ -222,8 +222,9 @@ void Sprite::setName(char *newName) {
_ext->_name = nullptr;
}
if (newName) {
_ext->_name = new char[strlen(newName) + 1];
strcpy(_ext->_name, newName);
size_t ln = strlen(newName) + 1;
_ext->_name = new char[ln];
Common::strcpy_s(_ext->_name, ln, newName);
}
}
@ -243,6 +244,7 @@ int Sprite::labVal(Action snq, int lab) {
return i;
} else {
char tmpStr[kLineMax + 1];
STATIC_ASSERT(sizeof(tmpStr) >= kPathMax, mergeExt_expects_kPathMax_buffer);
_vm->mergeExt(tmpStr, _file, kSprExt);
if (_vm->_resman->exist(tmpStr)) { // sprite description file exist

View File

@ -110,12 +110,13 @@ char *VMenu::vmGather(Common::Array<Choice *> list) {
len += strlen(list[i]->_text);
++h;
}
_vmgt = new char[len + h];
len += h;
_vmgt = new char[len];
*_vmgt = '\0';
for (uint i = 0; i < list.size(); i++) {
if (*_vmgt)
strcat(_vmgt, "|");
strcat(_vmgt, list[i]->_text);
Common::strcat_s(_vmgt, len, "|");
Common::strcat_s(_vmgt, len, list[i]->_text);
++h;
}