Skip to content

Commit

Permalink
Fix crash when trying to access a property of a Style which is nullptr (
Browse files Browse the repository at this point in the history
fix aseprite#4015)

Before this fix, an incomplete custom theme or an outdated official
theme could cause a crash during Aseprite startup.

This fix does not alert the artist the problem of the theme.
Simply avoid the crash.
  • Loading branch information
Gasparoken committed Feb 23, 2024
1 parent 0134c74 commit fa1ad32
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/app/ui/skin/skin_theme.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Aseprite
// Copyright (C) 2020-2023 Igara Studio S.A.
// Copyright (C) 2020-2024 Igara Studio S.A.
// Copyright (C) 2001-2017 David Capello
//
// This program is distributed under the terms of
Expand Down Expand Up @@ -108,7 +108,7 @@ namespace app {
if (it != m_styles.end())
return it->second;
else
return nullptr;
return getDefaultStyle();
}

SkinPartPtr getPartById(const std::string& id) const {
Expand Down
5 changes: 4 additions & 1 deletion src/ui/grid.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Aseprite UI Library
// Copyright (C) 2018-2023 Igara Studio S.A.
// Copyright (C) 2018-2024 Igara Studio S.A.
// Copyright (C) 2001-2017 David Capello
//
// This file is released under the terms of the MIT license.
Expand Down Expand Up @@ -125,6 +125,9 @@ Grid::Info Grid::getChildInfo(Widget* child)

void Grid::setStyle(Style* style)
{
ASSERT(style);
if (!style)
style = Theme::getDefaultStyle();
Widget::setStyle(style);
setGap(style->gap());
}
Expand Down
5 changes: 4 additions & 1 deletion src/ui/theme.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Aseprite UI Library
// Copyright (C) 2019-2023 Igara Studio S.A.
// Copyright (C) 2019-2024 Igara Studio S.A.
// Copyright (C) 2001-2018 David Capello
//
// This file is released under the terms of the MIT license.
Expand Down Expand Up @@ -142,6 +142,9 @@ Theme::~Theme()
set_theme(nullptr, guiscale());
}

// static
std::unique_ptr<ui::Style> Theme::m_defaultStyle;

void Theme::regenerateTheme()
{
set_mouse_cursor(kNoCursor);
Expand Down
11 changes: 10 additions & 1 deletion src/ui/theme.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Aseprite UI Library
// Copyright (C) 2020-2022 Igara Studio S.A.
// Copyright (C) 2020-2024 Igara Studio S.A.
// Copyright (C) 2001-2017 David Capello
//
// This file is released under the terms of the MIT license.
Expand Down Expand Up @@ -141,6 +141,14 @@ namespace ui {
static void drawTextBox(Graphics* g, const Widget* textbox,
int* w, int* h, gfx::Color bg, gfx::Color fg);

static ui::Style* getDefaultStyle() {
if (!m_defaultStyle.get()) {
m_defaultStyle.reset();
m_defaultStyle = std::make_unique<ui::Style>(nullptr);
}
return m_defaultStyle.get();
};

protected:
virtual void onRegenerateTheme() = 0;

Expand All @@ -165,6 +173,7 @@ namespace ui {
gfx::Size& sizeHint,
gfx::Border& borderHint,
gfx::Rect& textHint, int& textAlign);
static std::unique_ptr<ui::Style> m_defaultStyle;
};

} // namespace ui
Expand Down
6 changes: 4 additions & 2 deletions src/ui/widget.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Aseprite UI Library
// Copyright (C) 2018-2023 Igara Studio S.A.
// Copyright (C) 2018-2024 Igara Studio S.A.
// Copyright (C) 2001-2018 David Capello
//
// This file is released under the terms of the MIT license.
Expand Down Expand Up @@ -199,7 +199,9 @@ void Widget::setTheme(Theme* theme)
void Widget::setStyle(Style* style)
{
assert_ui_thread();

ASSERT(style);
if (!style)
style = Theme::getDefaultStyle();
m_style = style;
m_border = m_theme->calcBorder(this, style);
m_bgColor = m_theme->calcBgColor(this, style);
Expand Down

0 comments on commit fa1ad32

Please sign in to comment.