From c01328c2da67aa489d824282fea2e49be8393acd Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Wed, 31 Oct 2018 23:43:17 +0100 Subject: [PATCH] Don't crash due to accessing invalid bitmaps in wxAUI code Neither GetWidth() nor GetScaledWidth() (nor the corresponding height-related methods) can be called if the bitmap is invalid and the resulting assert led to a crash when it happened in wxAuiToolBarArt drawing code, as it was triggered on each redraw. Just use bitmap size of (0, 0) if we're not going to draw it anyhow. Closes #18263. --- src/aui/auibar.cpp | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/aui/auibar.cpp b/src/aui/auibar.cpp index e4027d7d5c..b2dc732a66 100644 --- a/src/aui/auibar.cpp +++ b/src/aui/auibar.cpp @@ -293,15 +293,21 @@ void wxAuiGenericToolBarArt::DrawButton( int bmpX = 0, bmpY = 0; int textX = 0, textY = 0; + const wxBitmap& bmp = item.GetState() & wxAUI_BUTTON_STATE_DISABLED + ? item.GetDisabledBitmap() + : item.GetBitmap(); + + const wxSize bmpSize = bmp.IsOk() ? bmp.GetScaledSize() : wxSize(0, 0); + if (m_textOrientation == wxAUI_TBTOOL_TEXT_BOTTOM) { bmpX = rect.x + (rect.width/2) - - (item.GetBitmap().GetScaledWidth()/2); + (bmpSize.x/2); bmpY = rect.y + ((rect.height-textHeight)/2) - - (item.GetBitmap().GetScaledHeight()/2); + (bmpSize.y/2); textX = rect.x + (rect.width/2) - (textWidth/2) + 1; textY = rect.y + rect.height - textHeight - 1; @@ -312,9 +318,9 @@ void wxAuiGenericToolBarArt::DrawButton( bmpY = rect.y + (rect.height/2) - - (item.GetBitmap().GetScaledHeight()/2); + (bmpSize.y/2); - textX = bmpX + wnd->FromDIP(3) + item.GetBitmap().GetScaledWidth(); + textX = bmpX + wnd->FromDIP(3) + bmpSize.x; textY = rect.y + (rect.height/2) - (textHeight/2); @@ -351,12 +357,6 @@ void wxAuiGenericToolBarArt::DrawButton( } } - wxBitmap bmp; - if (item.GetState() & wxAUI_BUTTON_STATE_DISABLED) - bmp = item.GetDisabledBitmap(); - else - bmp = item.GetBitmap(); - if ( bmp.IsOk() ) dc.DrawBitmap(bmp, bmpX, bmpY, true); @@ -596,8 +596,9 @@ wxSize wxAuiGenericToolBarArt::GetToolSize( if (!item.GetBitmap().IsOk() && !(m_flags & wxAUI_TB_TEXT)) return wnd->FromDIP(wxSize(16,16)); - int width = item.GetBitmap().GetScaledWidth(); - int height = item.GetBitmap().GetScaledHeight(); + const wxBitmap& bmp = item.GetBitmap(); + int width = bmp.IsOk() ? bmp.GetScaledWidth() : 0; + int height = bmp.IsOk() ? bmp.GetScaledHeight() : 0; if (m_flags & wxAUI_TB_TEXT) {