Added size restrictions to fonts and textures to avoid crashes caused by invalid theme configuration.

This commit is contained in:
Leon Styhre 2022-04-10 11:53:44 +02:00
parent cb8596ab8a
commit 91460495be
3 changed files with 25 additions and 6 deletions

View file

@ -124,11 +124,10 @@ void ImageComponent::resize()
}
}
// Make sure sub-pixel values are not rounded to zero.
if (mSize.x < 1.0f)
mSize.x = 1.0f;
if (mSize.y < 1.0f)
mSize.y = 1.0f;
// Make sure sub-pixel values are not rounded to zero and that the size is not unreasonably
// large (which may be caused by a mistake in the theme configuration).
mSize.x = glm::clamp(mSize.x, 1.0f, mRenderer->getScreenWidth() * 2.0f);
mSize.y = glm::clamp(mSize.y, 1.0f, mRenderer->getScreenHeight() * 2.0f);
mTexture->rasterizeAt(mSize.x, mSize.y);

View file

@ -111,6 +111,11 @@ void RatingComponent::setColorShift(unsigned int color)
void RatingComponent::onSizeChanged()
{
// Make sure the size is not unreasonably large (which may be caused by a mistake in
// the theme configuration).
mSize.x = glm::clamp(mSize.x, 0.0f, mRenderer->getScreenWidth() / 2.0f);
mSize.y = glm::clamp(mSize.y, 0.0f, mRenderer->getScreenHeight() / 2.0f);
if (mSize.y == 0.0f)
mSize.y = mSize.x / NUM_RATING_STARS;
else if (mSize.x == 0.0f)

View file

@ -160,6 +160,16 @@ Font::FontTexture::FontTexture(const int mSize)
// a texture buffer large enough to hold the fonts. This logic is obviously a hack though
// and needs to be properly reviewed and improved.
textureSize = glm::ivec2 {mSize * (20 + extraTextureSize), mSize * (8 + extraTextureSize / 2)};
// Make sure the size is not unreasonably large (which may be caused by a mistake in the
// theme configuration).
if (textureSize.x > static_cast<int>(Renderer::getScreenWidth()) * 10)
textureSize.x =
glm::clamp(textureSize.x, 0, static_cast<int>(Renderer::getScreenWidth()) * 10);
if (textureSize.y > static_cast<int>(Renderer::getScreenHeight()) * 10)
textureSize.y =
glm::clamp(textureSize.y, 0, static_cast<int>(Renderer::getScreenHeight()) * 10);
writePos = glm::ivec2 {0, 0};
rowHeight = 0;
}
@ -755,8 +765,13 @@ std::shared_ptr<Font> Font::getFromTheme(const ThemeData::ThemeElement* elem,
std::string path {orig ? orig->mPath : getDefaultPath()};
float sh {static_cast<float>(Renderer::getScreenHeight())};
// Make sure the size is not unreasonably large (which may be caused by a mistake in the
// theme configuration).
if (properties & FONT_SIZE && elem->has("fontSize"))
size = static_cast<int>(sh * elem->get<float>("fontSize"));
size = glm::clamp(static_cast<int>(sh * elem->get<float>("fontSize")), 0,
static_cast<int>(Renderer::getInstance()->getScreenHeight()));
if (properties & FONT_PATH && elem->has("fontPath"))
path = elem->get<std::string>("fontPath");