From 91460495be6a111c64d891249e8360abc61f9ba7 Mon Sep 17 00:00:00 2001 From: Leon Styhre Date: Sun, 10 Apr 2022 11:53:44 +0200 Subject: [PATCH] Added size restrictions to fonts and textures to avoid crashes caused by invalid theme configuration. --- es-core/src/components/ImageComponent.cpp | 9 ++++----- es-core/src/components/RatingComponent.cpp | 5 +++++ es-core/src/resources/Font.cpp | 17 ++++++++++++++++- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/es-core/src/components/ImageComponent.cpp b/es-core/src/components/ImageComponent.cpp index 82a115e0a..875ecedf7 100644 --- a/es-core/src/components/ImageComponent.cpp +++ b/es-core/src/components/ImageComponent.cpp @@ -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); diff --git a/es-core/src/components/RatingComponent.cpp b/es-core/src/components/RatingComponent.cpp index a7e001a08..ccb47a56a 100644 --- a/es-core/src/components/RatingComponent.cpp +++ b/es-core/src/components/RatingComponent.cpp @@ -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) diff --git a/es-core/src/resources/Font.cpp b/es-core/src/resources/Font.cpp index 6c4339691..4b9bcca81 100644 --- a/es-core/src/resources/Font.cpp +++ b/es-core/src/resources/Font.cpp @@ -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(Renderer::getScreenWidth()) * 10) + textureSize.x = + glm::clamp(textureSize.x, 0, static_cast(Renderer::getScreenWidth()) * 10); + if (textureSize.y > static_cast(Renderer::getScreenHeight()) * 10) + textureSize.y = + glm::clamp(textureSize.y, 0, static_cast(Renderer::getScreenHeight()) * 10); + writePos = glm::ivec2 {0, 0}; rowHeight = 0; } @@ -755,8 +765,13 @@ std::shared_ptr Font::getFromTheme(const ThemeData::ThemeElement* elem, std::string path {orig ? orig->mPath : getDefaultPath()}; float sh {static_cast(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(sh * elem->get("fontSize")); + size = glm::clamp(static_cast(sh * elem->get("fontSize")), 0, + static_cast(Renderer::getInstance()->getScreenHeight())); + if (properties & FONT_PATH && elem->has("fontPath")) path = elem->get("fontPath");