1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-03-25 21:38:59 +02:00

Fortify CLabel interface to prevent unchecked access

This commit is contained in:
Ivan Savenko 2022-12-19 22:04:50 +02:00
parent 87ba221415
commit d29c9d6445
10 changed files with 33 additions and 21 deletions

View File

@ -66,10 +66,10 @@ void CSavingScreen::changeSelection(std::shared_ptr<CMapInfo> to)
void CSavingScreen::saveGame()
{
if(!(tabSel && tabSel->inputName && tabSel->inputName->text.size()))
if(!(tabSel && tabSel->inputName && tabSel->inputName->getText().size()))
return;
std::string path = "Saves/" + tabSel->inputName->text;
std::string path = "Saves/" + tabSel->inputName->getText();
auto overWrite = [this, path]() -> void
{
@ -82,7 +82,7 @@ void CSavingScreen::saveGame()
if(CResourceHandler::get("local")->existsResource(ResourceID(path, EResType::CLIENT_SAVEGAME)))
{
std::string hlp = CGI->generaltexth->allTexts[493]; //%s exists. Overwrite?
boost::algorithm::replace_first(hlp, "%s", tabSel->inputName->text);
boost::algorithm::replace_first(hlp, "%s", tabSel->inputName->getText());
LOCPLINT->showYesNoDialog(hlp, overWrite, nullptr);
}
else

View File

@ -321,9 +321,9 @@ CChatBox::CChatBox(const Rect & rect)
void CChatBox::keyPressed(const SDL_KeyboardEvent & key)
{
if(key.keysym.sym == SDLK_RETURN && key.state == SDL_PRESSED && inputBox->text.size())
if(key.keysym.sym == SDLK_RETURN && key.state == SDL_PRESSED && inputBox->getText().size())
{
CSH->sendMessage(inputBox->text);
CSH->sendMessage(inputBox->getText());
inputBox->setText("");
}
else
@ -333,7 +333,7 @@ void CChatBox::keyPressed(const SDL_KeyboardEvent & key)
void CChatBox::addNewMessage(const std::string & text)
{
CCS->soundh->playSound("CHAT");
chatHistory->setText(chatHistory->label->text + text + "\n");
chatHistory->setText(chatHistory->label->getText() + text + "\n");
if(chatHistory->slider)
chatHistory->slider->moveToMax();
}

View File

@ -438,7 +438,7 @@ void CMultiPlayers::onChange(std::string newText)
size_t namesCount = 0;
for(auto & elem : inputNames)
if(!elem->text.empty())
if(!elem->getText().empty())
namesCount++;
}
@ -447,8 +447,8 @@ void CMultiPlayers::enterSelectionScreen()
std::vector<std::string> names;
for(auto name : inputNames)
{
if(name->text.length())
names.push_back(name->text);
if(name->getText().length())
names.push_back(name->getText());
}
Settings name = settings.write["general"]["playerName"];
@ -494,7 +494,7 @@ void CSimpleJoinScreen::connectToServer()
buttonOk->block(true);
CSDL_Ext::stopTextInput();
boost::thread(&CSimpleJoinScreen::connectThread, this, inputAddress->text, boost::lexical_cast<ui16>(inputPort->text));
boost::thread(&CSimpleJoinScreen::connectThread, this, inputAddress->getText(), boost::lexical_cast<ui16>(inputPort->getText()));
}
void CSimpleJoinScreen::leaveScreen()
@ -512,7 +512,7 @@ void CSimpleJoinScreen::leaveScreen()
void CSimpleJoinScreen::onChange(const std::string & newText)
{
buttonOk->block(inputAddress->text.empty() || inputPort->text.empty());
buttonOk->block(inputAddress->getText().empty() || inputPort->getText().empty());
}
void CSimpleJoinScreen::connectThread(const std::string addr, const ui16 port)

View File

@ -1126,6 +1126,9 @@ void CInGameConsole::textEdited(const SDL_TextEditingEvent & event)
void CInGameConsole::startEnteringText()
{
if (captureAllKeys)
return;
assert(GH.statusbar);
captureAllKeys = true;
enteredText = "_";

View File

@ -41,11 +41,11 @@ protected:
virtual std::string visibleText();
std::shared_ptr<CPicture> background;
public:
std::string text;
bool autoRedraw; //whether control will redraw itself on setTxt
public:
std::string getText();
virtual void setAutoRedraw(bool option);
virtual void setText(const std::string & Txt);
@ -124,6 +124,14 @@ class CGStatusBar : public CLabel, public std::enable_shared_from_this<CGStatusB
CGStatusBar(std::shared_ptr<CPicture> background_, EFonts Font = FONT_SMALL, ETextAlignment Align = ETextAlignment::CENTER, const SDL_Color & Color = Colors::WHITE);
CGStatusBar(int x, int y, std::string name, int maxw = -1);
//make CLabel API private
using CLabel::getText;
using CLabel::setAutoRedraw;
using CLabel::setText;
using CLabel::setColor;
using CLabel::getWidth;
protected:
Point getBorderSize() override;

View File

@ -978,6 +978,7 @@ void CAdvMapInt::deactivate()
}
minimap.deactivate();
terrain.deactivate();
statusbar->deactivate();
}
}
@ -1667,13 +1668,13 @@ void CAdvMapInt::tileHovered(const int3 &mapPos)
objRelations = LOCPLINT->cb->getPlayerRelations(LOCPLINT->playerID, objAtTile->tempOwner);
std::string text = curHero() ? objAtTile->getHoverText(curHero()) : objAtTile->getHoverText(LOCPLINT->playerID);
boost::replace_all(text,"\n"," ");
statusbar->setText(text);
statusbar->write(text);
}
else
{
std::string hlp;
CGI->mh->getTerrainDescr(mapPos, hlp, false);
statusbar->setText(hlp);
statusbar->write(hlp);
}
if(spellBeingCasted)

View File

@ -1019,7 +1019,7 @@ void CCreaInfo::update()
else
value = boost::lexical_cast<std::string>(town->creatureGrowth(level));
if(value != label->text)
if(value != label->getText())
label->setText(value);
}
}

View File

@ -799,7 +799,7 @@ void CTownItem::updateGarrisons()
void CTownItem::update()
{
std::string incomeVal = boost::lexical_cast<std::string>(town->dailyIncome()[Res::GOLD]);
if (incomeVal != income->text)
if (incomeVal != income->getText())
income->setText(incomeVal);
heroes->update();

View File

@ -66,7 +66,7 @@ void CSpellWindow::InteractiveArea::clickRight(tribool down, bool previousState)
void CSpellWindow::InteractiveArea::hover(bool on)
{
if(on)
owner->statusBar->setText(hoverText);
owner->statusBar->write(hoverText);
else
owner->statusBar->clear();
}
@ -513,7 +513,7 @@ CSpellWindow::SpellArea::SpellArea(SDL_Rect pos, CSpellWindow * owner)
cost = std::make_shared<CLabel>(39, 94, FONT_TINY, ETextAlignment::CENTER);
for(auto l : {name, level, cost})
l->autoRedraw = false;
l->setAutoRedraw(false);
}
CSpellWindow::SpellArea::~SpellArea() = default;
@ -609,7 +609,7 @@ void CSpellWindow::SpellArea::hover(bool on)
if(mySpell)
{
if(on)
owner->statusBar->setText(boost::to_string(boost::format("%s (%s)") % mySpell->name % CGI->generaltexth->allTexts[171+mySpell->level]));
owner->statusBar->write(boost::to_string(boost::format("%s (%s)") % mySpell->name % CGI->generaltexth->allTexts[171+mySpell->level]));
else
owner->statusBar->clear();
}

View File

@ -579,7 +579,7 @@ void CSystemOptionsWindow::selectGameRes()
#endif
auto resolutionStr = resolutionToString(resolution.first, resolution.second);
if(gameResLabel->text == resolutionStr)
if(gameResLabel->getText() == resolutionStr)
currentResolutionIndex = i;
items.push_back(std::move(resolutionStr));
++i;