From 104f8a10cfe1a407fc8d4c9c1b6db20de93a35f4 Mon Sep 17 00:00:00 2001 From: David Capello Date: Thu, 17 Feb 2022 10:36:18 -0300 Subject: [PATCH] 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. --- src/app/ui/file_list.cpp | 25 ++++++++++++++++++++++--- src/app/ui/file_list.h | 4 ++-- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/app/ui/file_list.cpp b/src/app/ui/file_list.cpp index 9f0b562ed..d7a7602a4 100644 --- a/src/app/ui/file_list.cpp +++ b/src/app/ui/file_list.cpp @@ -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(); diff --git a/src/app/ui/file_list.h b/src/app/ui/file_list.h index 5a7288c46..f72053500 100644 --- a/src/app/ui/file_list.h +++ b/src/app/ui/file_list.h @@ -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;