ソースを参照

Fortify CLabel interface to prevent unchecked access

Ivan Savenko 2 年 前
コミット
d29c9d6445

+ 3 - 3
client/lobby/CSavingScreen.cpp

@@ -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

+ 3 - 3
client/lobby/CSelectionBase.cpp

@@ -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();
 }

+ 5 - 5
client/mainmenu/CMainMenu.cpp

@@ -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)

+ 3 - 0
client/widgets/AdventureMapClasses.cpp

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

+ 10 - 2
client/widgets/TextControls.h

@@ -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;
 

+ 3 - 2
client/windows/CAdvmapInterface.cpp

@@ -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)

+ 1 - 1
client/windows/CCastleInterface.cpp

@@ -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);
 	}
 }

+ 1 - 1
client/windows/CKingdomInterface.cpp

@@ -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();

+ 3 - 3
client/windows/CSpellWindow.cpp

@@ -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();
 	}

+ 1 - 1
client/windows/GUIClasses.cpp

@@ -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;