AGS: Engine: replaced uses of strncpy with snprintf for safety

From upstream 02feb81b34f041dccd8a4bb33ff060cc532d979b
This commit is contained in:
Walter Agazzi 2024-01-08 17:17:53 +01:00
parent 4eaac90e4e
commit cce6aff754
8 changed files with 8 additions and 16 deletions

View File

@ -595,8 +595,7 @@ const char *Game_GetName() {
}
void Game_SetName(const char *newName) {
strncpy(_GP(play).game_name, newName, 99);
_GP(play).game_name[99] = 0;
snprintf(_GP(play).game_name, MAX_GAME_STATE_NAME_LENGTH, "%s", newName);
sys_window_set_title(_GP(play).game_name);
GUI::MarkSpecialLabelsForUpdate(kLabelMacro_Gamename);
}

View File

@ -58,10 +58,7 @@ void SetInvItemName(int invi, const char *newName) {
if ((invi < 1) || (invi > _GP(game).numinvitems))
quit("!SetInvName: invalid inventory item specified");
// set the new name, making sure it doesn't overflow the buffer
strncpy(_GP(game).invinfo[invi].name, newName, 25);
_GP(game).invinfo[invi].name[24] = 0;
snprintf(_GP(game).invinfo[invi].name, MAX_INVENTORY_NAME_LENGTH, "%s", newName);
// might need to redraw the GUI if it has the inv item name on it
GUI::MarkSpecialLabelsForUpdate(kLabelMacro_Overhotspot);
}

View File

@ -190,8 +190,7 @@ int ListBox_GetItemAtLocation(GUIListBox *listbox, int x, int y) {
char *ListBox_GetItemText(GUIListBox *listbox, int index, char *buffer) {
if ((index < 0) || (index >= listbox->ItemCount))
quit("!ListBoxGetItemText: invalid item specified");
strncpy(buffer, listbox->Items[index].GetCStr(), 198);
buffer[199] = 0;
snprintf(buffer, MAX_MAXSTRLEN, "%s", listbox->Items[index].GetCStr());
return buffer;
}

View File

@ -33,8 +33,7 @@ namespace AGS3 {
using namespace Shared;
MyLabel::MyLabel(int xx, int yy, int wii, const char *tee) {
strncpy(text, tee, 150);
text[149] = 0;
snprintf(text, sizeof(text), "%s", tee);
x = xx;
y = yy;
wid = wii;

View File

@ -40,8 +40,7 @@ MyPushButton::MyPushButton(int xx, int yy, int wi, int hi, const char *tex) {
wid = wi;
hit = hi + 1; //hit=hi;
state = 0;
strncpy(text, tex, 50);
text[49] = 0;
snprintf(text, sizeof(text), "%s", tex);
}
void MyPushButton::draw(Bitmap *ds) {

View File

@ -779,7 +779,7 @@ void engine_init_game_settings() {
void engine_setup_scsystem_auxiliary() {
// ScriptSystem::aci_version is only 10 chars long
Common::strlcpy(_GP(scsystem).aci_version, _G(EngineVersion).LongString.GetCStr(), 10);
snprintf(_GP(scsystem).aci_version, sizeof(_GP(scsystem).aci_version), "%s", _G(EngineVersion).LongString.GetCStr());
if (_GP(usetup).override_script_os >= 0) {
_GP(scsystem).os = _GP(usetup).override_script_os;
} else {

View File

@ -213,8 +213,7 @@ int main_process_cmdline(ConfigTree &cfg, int argc, const char *argv[]) {
if (argc < ee + 2)
break;
_GP(play).takeover_data = atoi(argv[ee + 1]);
strncpy(_GP(play).takeover_from, argv[ee + 2], 49);
_GP(play).takeover_from[49] = 0;
snprintf(_GP(play).takeover_from, sizeof(_GP(play).takeover_from), "%s", argv[ee + 2]);
ee += 2;
} else if (ags_stricmp(arg, "--clear-cache-on-room-change") == 0) {
cfg["misc"]["clear_cache_on_room_change"] = "1";

View File

@ -334,7 +334,7 @@ static int PrepareTextScript(ccInstance *sci, const char **tsname) {
if (_G(num_scripts) >= MAX_SCRIPT_AT_ONCE)
quit("too many nested text script instances created");
// in case script_run_another is the function name, take a backup
strncpy(scfunctionname, tsname[0], MAX_FUNCTION_NAME_LEN);
snprintf(scfunctionname, sizeof(scfunctionname), "%s", tsname[0]);
tsname[0] = &scfunctionname[0];
update_script_mouse_coords();
_G(inside_script)++;