Commit 2607476d authored by Chen Bill's avatar Chen Bill Committed by GitHub

fix buffer overflow in SetStaticText (#2925)

parent d552e276
...@@ -1091,41 +1091,48 @@ void Game::InitStaticText(irr::gui::IGUIStaticText* pControl, irr::u32 cWidth, i ...@@ -1091,41 +1091,48 @@ void Game::InitStaticText(irr::gui::IGUIStaticText* pControl, irr::u32 cWidth, i
scrCardText->setPos(0); scrCardText->setPos(0);
} }
std::wstring Game::SetStaticText(irr::gui::IGUIStaticText* pControl, irr::u32 cWidth, irr::gui::CGUITTFont* font, const wchar_t* text, irr::u32 pos) { std::wstring Game::SetStaticText(irr::gui::IGUIStaticText* pControl, irr::u32 cWidth, irr::gui::CGUITTFont* font, const wchar_t* text, irr::u32 pos) {
int pbuffer = 0; size_t pbuffer = 0;
irr::u32 _width = 0, _height = 0; irr::u32 _width = 0, _height = 0;
wchar_t prev = 0; wchar_t prev = 0;
wchar_t strBuffer[4096]; wchar_t strBuffer[4096]{};
std::wstring ret; constexpr size_t buffer_len = sizeof strBuffer / sizeof strBuffer[0] - 1;
const size_t text_len = std::wcslen(text);
for(size_t i = 0; text[i] != 0 && i < std::wcslen(text); ++i) { for(size_t i = 0; i < text_len ; ++i) {
if (pbuffer >= buffer_len)
break;
wchar_t c = text[i]; wchar_t c = text[i];
irr::u32 w = font->getCharDimension(c).Width + font->getKerningWidth(c, prev); irr::u32 w = font->getCharDimension(c).Width + font->getKerningWidth(c, prev);
prev = c; prev = c;
if(text[i] == L'\r') { if (c == L'\r') {
continue; continue;
} else if(text[i] == L'\n') { }
if (c == L'\n') {
strBuffer[pbuffer++] = L'\n'; strBuffer[pbuffer++] = L'\n';
_width = 0; _width = 0;
_height++; _height++;
prev = 0; prev = 0;
if(_height == pos) if (_height == pos)
pbuffer = 0; pbuffer = 0;
continue; continue;
} else if(_width > 0 && _width + w > cWidth) { }
if (_width > 0 && _width + w > cWidth) {
strBuffer[pbuffer++] = L'\n'; strBuffer[pbuffer++] = L'\n';
_width = 0; _width = 0;
_height++; _height++;
prev = 0; prev = 0;
if(_height == pos) if (_height == pos)
pbuffer = 0; pbuffer = 0;
} }
if (pbuffer >= buffer_len)
break;
_width += w; _width += w;
strBuffer[pbuffer++] = c; strBuffer[pbuffer++] = c;
} }
strBuffer[pbuffer] = 0; strBuffer[pbuffer] = 0;
if(pControl) pControl->setText(strBuffer); if (pControl)
ret.assign(strBuffer); pControl->setText(strBuffer);
return ret; return std::wstring(strBuffer);
} }
void Game::LoadExpansions() { void Game::LoadExpansions() {
FileSystem::TraversalDir(L"./expansions", [](const wchar_t* name, bool isdir) { FileSystem::TraversalDir(L"./expansions", [](const wchar_t* name, bool isdir) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment