From 51ff4ec08bd54344139627d20f299f51dc48a619 Mon Sep 17 00:00:00 2001 From: Artur Wieczorek Date: Sun, 4 Apr 2021 21:01:43 +0200 Subject: [PATCH] Dont allow to remove from wxImageList an images with index out of range Since ImageList_Remove() accepts -1 index as a magic number to remove all images, we need to explicitly prevent using indices < 0 in the call. Because wxImageList::RemoveAll() uses the trick with -1 index to call wxImageList::Remove(-1) we also need to refactor this mehod and call ImageList_Remove() API directly instead. --- src/msw/imaglist.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/msw/imaglist.cpp b/src/msw/imaglist.cpp index 9ee5283905..65997291fc 100644 --- a/src/msw/imaglist.cpp +++ b/src/msw/imaglist.cpp @@ -323,7 +323,7 @@ bool wxImageList::Replace(int i, const wxIcon& icon) // Removes the image at the given index. bool wxImageList::Remove(int index) { - bool ok = ImageList_Remove(GetHImageList(), index) != 0; + bool ok = index >= 0 && ImageList_Remove(GetHImageList(), index) != FALSE; if ( !ok ) { wxLogLastError(wxT("ImageList_Remove()")); @@ -336,7 +336,13 @@ bool wxImageList::Remove(int index) bool wxImageList::RemoveAll() { // don't use ImageList_RemoveAll() because mingw32 headers don't have it - return Remove(-1); + bool ok = ImageList_Remove(GetHImageList(), -1) != FALSE; + if ( !ok ) + { + wxLogLastError(wxT("ImageList_Remove()")); + } + + return ok; } // Draws the given image on a dc at the specified position.