From 6e63354cd1c33a78765e85763a6dfcedeacc4ee6 Mon Sep 17 00:00:00 2001 From: Anders Jenbo Date: Mon, 22 Nov 2021 03:29:25 +0100 Subject: [PATCH] Remove safe guards around strings own the input for --- Source/DiabloUI/selconn.cpp | 12 ++++------ Source/DiabloUI/selgame.cpp | 46 ++++++++++++++++-------------------- Source/DiabloUI/selhero.cpp | 17 +++++-------- Source/DiabloUI/selok.cpp | 4 +--- Source/DiabloUI/selyesno.cpp | 4 +--- Source/appfat.cpp | 5 ++-- Source/pack.cpp | 3 +-- 7 files changed, 37 insertions(+), 54 deletions(-) diff --git a/Source/DiabloUI/selconn.cpp b/Source/DiabloUI/selconn.cpp index c780055db..c7b55e812 100644 --- a/Source/DiabloUI/selconn.cpp +++ b/Source/DiabloUI/selconn.cpp @@ -4,7 +4,6 @@ #include "stores.h" #include "storm/storm_net.hpp" #include "utils/language.h" -#include "utils/utf8.hpp" namespace devilution { @@ -105,22 +104,21 @@ void SelconnFocus(int value) int players = MAX_PLRS; switch (vecConnItems[value]->m_value) { case SELCONN_TCP: - CopyUtf8(selconn_Description, _("All computers must be connected to a TCP-compatible network."), sizeof(selconn_Description)); + strcpy(selconn_Description, _("All computers must be connected to a TCP-compatible network.")); players = MAX_PLRS; break; case SELCONN_ZT: - CopyUtf8(selconn_Description, _("All computers must be connected to the internet."), sizeof(selconn_Description)); + strcpy(selconn_Description, _("All computers must be connected to the internet.")); players = MAX_PLRS; break; case SELCONN_LOOPBACK: - CopyUtf8(selconn_Description, _("Play by yourself with no network exposure."), sizeof(selconn_Description)); + strcpy(selconn_Description, _("Play by yourself with no network exposure.")); players = 1; break; } - CopyUtf8(selconn_MaxPlayers, fmt::format(_("Players Supported: {:d}"), players), sizeof(selconn_MaxPlayers)); - const std::string wrapped = WordWrapString(selconn_Description, DESCRIPTION_WIDTH); - CopyUtf8(selconn_Description, wrapped, sizeof(selconn_Description)); + strcpy(selconn_MaxPlayers, fmt::format(_("Players Supported: {:d}"), players).c_str()); + strcpy(selconn_Description, WordWrapString(selconn_Description, DESCRIPTION_WIDTH).c_str()); } void SelconnSelect(int value) diff --git a/Source/DiabloUI/selgame.cpp b/Source/DiabloUI/selgame.cpp index a22eadf8d..148ff79ac 100644 --- a/Source/DiabloUI/selgame.cpp +++ b/Source/DiabloUI/selgame.cpp @@ -12,7 +12,6 @@ #include "options.h" #include "storm/storm_net.hpp" #include "utils/language.h" -#include "utils/utf8.hpp" namespace devilution { @@ -120,20 +119,19 @@ void selgame_GameSelection_Focus(int value) HighlightedItem = value; switch (vecSelGameDlgItems[value]->m_value) { case 0: - CopyUtf8(selgame_Description, _("Create a new game with a difficulty setting of your choice."), sizeof(selgame_Description)); + strcpy(selgame_Description, _("Create a new game with a difficulty setting of your choice.")); break; case 1: - CopyUtf8(selgame_Description, _("Create a new public game that anyone can join with a difficulty setting of your choice."), sizeof(selgame_Description)); + strcpy(selgame_Description, _("Create a new public game that anyone can join with a difficulty setting of your choice.")); break; case 2: - CopyUtf8(selgame_Description, _("Enter an IP or a hostname and join a game already in progress at that address."), sizeof(selgame_Description)); + strcpy(selgame_Description, _("Enter an IP or a hostname and join a game already in progress at that address.")); break; default: - CopyUtf8(selgame_Description, _("Join the public game already in progress at this address."), sizeof(selgame_Description)); + strcpy(selgame_Description, _("Join the public game already in progress at this address.")); break; } - const std::string wrapped = WordWrapString(selgame_Description, DESCRIPTION_WIDTH); - CopyUtf8(selgame_Description, wrapped.data(), sizeof(selgame_Description)); + strcpy(selgame_Description, WordWrapString(selgame_Description, DESCRIPTION_WIDTH).c_str()); } /** @@ -237,20 +235,19 @@ void selgame_Diff_Focus(int value) { switch (vecSelGameDlgItems[value]->m_value) { case DIFF_NORMAL: - CopyUtf8(selgame_Label, _("Normal"), sizeof(selgame_Label)); - CopyUtf8(selgame_Description, _("Normal Difficulty\nThis is where a starting character should begin the quest to defeat Diablo."), sizeof(selgame_Description)); + strcpy(selgame_Label, _("Normal")); + strcpy(selgame_Description, _("Normal Difficulty\nThis is where a starting character should begin the quest to defeat Diablo.")); break; case DIFF_NIGHTMARE: - CopyUtf8(selgame_Label, _("Nightmare"), sizeof(selgame_Label)); - CopyUtf8(selgame_Description, _("Nightmare Difficulty\nThe denizens of the Labyrinth have been bolstered and will prove to be a greater challenge. This is recommended for experienced characters only."), sizeof(selgame_Description)); + strcpy(selgame_Label, _("Nightmare")); + strcpy(selgame_Description, _("Nightmare Difficulty\nThe denizens of the Labyrinth have been bolstered and will prove to be a greater challenge. This is recommended for experienced characters only.")); break; case DIFF_HELL: - CopyUtf8(selgame_Label, _("Hell"), sizeof(selgame_Label)); - CopyUtf8(selgame_Description, _("Hell Difficulty\nThe most powerful of the underworld's creatures lurk at the gateway into Hell. Only the most experienced characters should venture in this realm."), sizeof(selgame_Description)); + strcpy(selgame_Label, _("Hell")); + strcpy(selgame_Description, _("Hell Difficulty\nThe most powerful of the underworld's creatures lurk at the gateway into Hell. Only the most experienced characters should venture in this realm.")); break; } - const std::string wrapped = WordWrapString(selgame_Description, DESCRIPTION_WIDTH); - CopyUtf8(selgame_Description, wrapped, sizeof(selgame_Description)); + strcpy(selgame_Description, WordWrapString(selgame_Description, DESCRIPTION_WIDTH).c_str()); } bool IsDifficultyAllowed(int value) @@ -362,24 +359,23 @@ void selgame_Speed_Focus(int value) { switch (vecSelGameDlgItems[value]->m_value) { case 20: - CopyUtf8(selgame_Label, _("Normal"), sizeof(selgame_Label)); - CopyUtf8(selgame_Description, _("Normal Speed\nThis is where a starting character should begin the quest to defeat Diablo."), sizeof(selgame_Description)); + strcpy(selgame_Label, _("Normal")); + strcpy(selgame_Description, _("Normal Speed\nThis is where a starting character should begin the quest to defeat Diablo.")); break; case 30: - CopyUtf8(selgame_Label, _("Fast"), sizeof(selgame_Label)); - CopyUtf8(selgame_Description, _("Fast Speed\nThe denizens of the Labyrinth have been hastened and will prove to be a greater challenge. This is recommended for experienced characters only."), sizeof(selgame_Description)); + strcpy(selgame_Label, _("Fast")); + strcpy(selgame_Description, _("Fast Speed\nThe denizens of the Labyrinth have been hastened and will prove to be a greater challenge. This is recommended for experienced characters only.")); break; case 40: - CopyUtf8(selgame_Label, _("Faster"), sizeof(selgame_Label)); - CopyUtf8(selgame_Description, _("Faster Speed\nMost monsters of the dungeon will seek you out quicker than ever before. Only an experienced champion should try their luck at this speed."), sizeof(selgame_Description)); + strcpy(selgame_Label, _("Faster")); + strcpy(selgame_Description, _("Faster Speed\nMost monsters of the dungeon will seek you out quicker than ever before. Only an experienced champion should try their luck at this speed.")); break; case 50: - CopyUtf8(selgame_Label, _("Fastest"), sizeof(selgame_Label)); - CopyUtf8(selgame_Description, _("Fastest Speed\nThe minions of the underworld will rush to attack without hesitation. Only a true speed demon should enter at this pace."), sizeof(selgame_Description)); + strcpy(selgame_Label, _("Fastest")); + strcpy(selgame_Description, _("Fastest Speed\nThe minions of the underworld will rush to attack without hesitation. Only a true speed demon should enter at this pace.")); break; } - const std::string wrapped = WordWrapString(selgame_Description, DESCRIPTION_WIDTH); - CopyUtf8(selgame_Description, wrapped, sizeof(selgame_Description)); + strcpy(selgame_Description, WordWrapString(selgame_Description, DESCRIPTION_WIDTH).c_str()); } void selgame_Speed_Esc() diff --git a/Source/DiabloUI/selhero.cpp b/Source/DiabloUI/selhero.cpp index dd07a0990..1532cb08a 100644 --- a/Source/DiabloUI/selhero.cpp +++ b/Source/DiabloUI/selhero.cpp @@ -16,7 +16,6 @@ #include "options.h" #include "pfile.h" #include "utils/language.h" -#include "utils/utf8.hpp" #include namespace devilution { @@ -139,12 +138,8 @@ void SelheroListFocus(int value) } SELHERO_DIALOG_HERO_IMG->m_frame = static_cast(enum_size::value); - CopyUtf8(textStats[0], "--", sizeof(textStats[0])); - CopyUtf8(textStats[1], "--", sizeof(textStats[1])); - CopyUtf8(textStats[2], "--", sizeof(textStats[2])); - CopyUtf8(textStats[3], "--", sizeof(textStats[3])); - CopyUtf8(textStats[4], "--", sizeof(textStats[4])); - CopyUtf8(textStats[5], "--", sizeof(textStats[5])); + for (char *textStat : textStats) + strcpy(textStat, "--"); SELLIST_DIALOG_DELETE_BUTTON->m_iFlags = baseFlags | UiFlags::ColorUiSilver | UiFlags::ElementDisabled; selhero_deleteEnabled = false; } @@ -270,7 +265,7 @@ void SelheroClassSelectorSelect(int value) title = selhero_isMultiPlayer ? _("New Multi Player Hero") : _("New Single Player Hero"); memset(selhero_heroInfo.name, '\0', sizeof(selhero_heroInfo.name)); if (ShouldPrefillHeroName()) - CopyUtf8(selhero_heroInfo.name, SelheroGenerateName(selhero_heroInfo.heroclass), sizeof(selhero_heroInfo.name)); + strcpy(selhero_heroInfo.name, SelheroGenerateName(selhero_heroInfo.heroclass)); vecSelDlgItems.clear(); SDL_Rect rect1 = { (Sint16)(PANEL_LEFT + 264), (Sint16)(UI_OFFSET_Y + 211), 320, 33 }; vecSelDlgItems.push_back(std::make_unique(_("Enter Name"), rect1, UiFlags::AlignCenter | UiFlags::FontSize30 | UiFlags::ColorUiSilver, 3)); @@ -597,11 +592,11 @@ static void UiSelHeroDialog( char dialogTitle[128]; char dialogText[256]; if (selhero_isMultiPlayer) { - CopyUtf8(dialogTitle, _("Delete Multi Player Hero"), sizeof(dialogTitle)); + strcpy(dialogTitle, _("Delete Multi Player Hero")); } else { - CopyUtf8(dialogTitle, _("Delete Single Player Hero"), sizeof(dialogTitle)); + strcpy(dialogTitle, _("Delete Single Player Hero")); } - CopyUtf8(dialogText, fmt::format(_("Are you sure you want to delete the character \"{:s}\"?"), selhero_heroInfo.name), sizeof(dialogText)); + strcpy(dialogText, fmt::format(_("Are you sure you want to delete the character \"{:s}\"?"), selhero_heroInfo.name).c_str()); if (UiSelHeroYesNoDialog(dialogTitle, dialogText)) fnremove(&selhero_heroInfo); diff --git a/Source/DiabloUI/selok.cpp b/Source/DiabloUI/selok.cpp index 8d244c269..e5444ecfe 100644 --- a/Source/DiabloUI/selok.cpp +++ b/Source/DiabloUI/selok.cpp @@ -3,7 +3,6 @@ #include "DiabloUI/diabloui.h" #include "control.h" #include "utils/language.h" -#include "utils/utf8.hpp" #include "engine/render/text_render.hpp" namespace devilution { @@ -69,8 +68,7 @@ void UiSelOkDialog(const char *title, const char *body, bool background) vecSelOkDialogItems.push_back(std::make_unique(_("OK"), 0)); vecSelOkDialog.push_back(std::make_unique(vecSelOkDialogItems, PANEL_LEFT + 230, (UI_OFFSET_Y + 390), 180, 35, UiFlags::AlignCenter | UiFlags::FontSize30 | UiFlags::ColorUiGold)); - const std::string wrapped = WordWrapString(body, MESSAGE_WIDTH, GameFont24); - CopyUtf8(dialogText, wrapped, sizeof(dialogText)); + strcpy(dialogText, WordWrapString(body, MESSAGE_WIDTH, GameFont24).c_str()); UiInitList(0, nullptr, selok_Select, selok_Esc, vecSelOkDialog, false, nullptr); diff --git a/Source/DiabloUI/selyesno.cpp b/Source/DiabloUI/selyesno.cpp index 7734046f2..11c5006eb 100644 --- a/Source/DiabloUI/selyesno.cpp +++ b/Source/DiabloUI/selyesno.cpp @@ -3,7 +3,6 @@ #include "DiabloUI/diabloui.h" #include "control.h" #include "utils/language.h" -#include "utils/utf8.hpp" namespace devilution { namespace { @@ -56,8 +55,7 @@ bool UiSelHeroYesNoDialog(const char *title, const char *body) vecSelYesNoDialogItems.push_back(std::make_unique(_("No"), 1)); vecSelYesNoDialog.push_back(std::make_unique(vecSelYesNoDialogItems, PANEL_LEFT + 230, (UI_OFFSET_Y + 390), 180, 35, UiFlags::AlignCenter | UiFlags::FontSize30 | UiFlags::ColorUiGold)); - const std::string wrapped = WordWrapString(body, MESSAGE_WIDTH, GameFont24); - CopyUtf8(selyesno_confirmationMessage, wrapped, sizeof(selyesno_confirmationMessage)); + strcpy(selyesno_confirmationMessage, WordWrapString(body, MESSAGE_WIDTH, GameFont24).c_str()); UiInitList(vecSelYesNoDialogItems.size(), nullptr, SelyesnoSelect, SelyesnoEsc, vecSelYesNoDialog, true, nullptr); diff --git a/Source/appfat.cpp b/Source/appfat.cpp index 25630487d..736bf04a3 100644 --- a/Source/appfat.cpp +++ b/Source/appfat.cpp @@ -14,7 +14,6 @@ #include "utils/language.h" #include "utils/sdl_thread.h" #include "utils/ui_fwd.h" -#include "utils/utf8.hpp" namespace devilution { @@ -100,7 +99,7 @@ void ErrDlg(const char *title, const char *error, const char *logFilePath, int l FreeDlg(); - CopyUtf8(text, fmt::format(_(/* TRANSLATORS: Error message that displays relevant information for bug report */ "{:s}\n\nThe error occurred at: {:s} line {:d}"), error, logFilePath, logLineNr), sizeof(text)); + strcpy(text, fmt::format(_(/* TRANSLATORS: Error message that displays relevant information for bug report */ "{:s}\n\nThe error occurred at: {:s} line {:d}"), error, logFilePath, logLineNr).c_str()); UiErrorOkDialog(title, text); app_fatal(nullptr); @@ -129,7 +128,7 @@ void DirErrorDlg(const char *error) { char text[1024]; - CopyUtf8(text, fmt::format(_(/* TRANSLATORS: Error when Program is not allowed to write data */ "Unable to write to location:\n{:s}"), error), sizeof(text)); + strcpy(text, fmt::format(_(/* TRANSLATORS: Error when Program is not allowed to write data */ "Unable to write to location:\n{:s}"), error).c_str()); UiErrorOkDialog(_("Read-Only Directory Error"), text); app_fatal(nullptr); diff --git a/Source/pack.cpp b/Source/pack.cpp index 7899a0cc9..00857298d 100644 --- a/Source/pack.cpp +++ b/Source/pack.cpp @@ -10,7 +10,6 @@ #include "loadsave.h" #include "stores.h" #include "utils/endian.hpp" -#include "utils/utf8.hpp" namespace devilution { @@ -223,7 +222,7 @@ bool UnPackPlayer(const PlayerPack *pPack, Player &player, bool netSync) ClrPlrPath(player); player.destAction = ACTION_NONE; - CopyUtf8(player._pName, pPack->pName, sizeof(player._pName)); + strcpy(player._pName, pPack->pName); InitPlayer(player, true);