Fix crash using invalid index in FileItemList (fix #3181)

There is a state where m_selected is pointing to an item that doesn't
exist in the current list of items. We didn't detect this on Debug
mode yet, but we've received one Sentry crash report about it.
This commit is contained in:
David Capello 2022-02-17 10:36:18 -03:00
parent 845ff17788
commit 104f8a10cf
2 changed files with 24 additions and 5 deletions

View File

@ -1,5 +1,5 @@
// Aseprite
// Copyright (C) 2019-2020 Igara Studio S.A.
// Copyright (C) 2019-2022 Igara Studio S.A.
// Copyright (C) 2001-2018 David Capello
//
// This program is distributed under the terms of
@ -442,8 +442,17 @@ void FileList::onPaint(ui::PaintEvent& ev)
// Paint main selected index (so if the filename label is bigger it
// will appear over other items).
if (m_selected)
paintItem(g, m_selected, selectedIndex);
if (m_selected) {
ASSERT(selectedIndex >= 0);
if (selectedIndex >= 0)
paintItem(g, m_selected, selectedIndex);
else {
// Strange run-time state where the "m_selected" is not in the
// list. The previous assert should fail on Debug so this is
// here only for Release mode.
return;
}
}
// Draw main thumbnail for the selected item when there are no
// thumbnails per item.
@ -778,6 +787,16 @@ FileList::ItemInfo FileList::calcFileItemInfo(int i) const
return info;
}
FileList::ItemInfo FileList::getFileItemInfo(int i) const
{
ASSERT(i >= 0 && i < int(m_info.size()));
if (i >= 0 && i < int(m_info.size()))
return m_info[i];
else
return ItemInfo();
}
void FileList::makeSelectedFileitemVisible()
{
int i = selectedIndex();

View File

@ -1,5 +1,5 @@
// Aseprite
// Copyright (C) 2019 Igara Studio S.A.
// Copyright (C) 2019-2022 Igara Studio S.A.
// Copyright (C) 2001-2018 David Capello
//
// This program is distributed under the terms of
@ -84,7 +84,7 @@ namespace app {
void onMonitoringTick();
void recalcAllFileItemInfo();
ItemInfo calcFileItemInfo(int i) const;
ItemInfo getFileItemInfo(int i) const { return m_info[i]; }
ItemInfo getFileItemInfo(int i) const;
void makeSelectedFileitemVisible();
void regenerateList();
int selectedIndex() const;