From 8b492f67ab10417d061556f97b7298ff37055393 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Sat, 7 Mar 2009 23:37:51 +0000 Subject: [PATCH] detect adding too many items to a grid sizer sooner and don't crash if this happens git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@59422 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/sizer.h | 2 ++ src/common/sizer.cpp | 35 +++++++++++++++++++++++++++++++---- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/include/wx/sizer.h b/include/wx/sizer.h index 26a32ba3ea..9a686c4fee 100644 --- a/include/wx/sizer.h +++ b/include/wx/sizer.h @@ -727,6 +727,8 @@ public: wxGridSizer( int rows, int cols, int vgap, int hgap ); wxGridSizer( int cols, int vgap = 0, int hgap = 0 ); + virtual wxSizerItem *Insert(size_t index, wxSizerItem *item); + virtual void RecalcSizes(); virtual wxSize CalcMin(); diff --git a/src/common/sizer.cpp b/src/common/sizer.cpp index c4502c852e..f8d4d2ddf1 100644 --- a/src/common/sizer.cpp +++ b/src/common/sizer.cpp @@ -1333,18 +1333,45 @@ wxGridSizer::wxGridSizer( int cols, int vgap, int hgap ) { } +wxSizerItem *wxGridSizer::Insert(size_t index, wxSizerItem *item) +{ + // if only the number of columns or the number of rows is specified for a + // sizer, arbitrarily many items can be added to it but if both of them are + // fixed, then the sizer can't have more than that many items -- check for + // this here to ensure that we detect errors as soon as possible + if ( m_cols && m_rows ) + { + if ( m_children.GetCount() == m_cols*m_rows ) + { + wxFAIL_MSG( "too many items in grid sizer (maybe you should omit " + "the number of either rows or columns?)" ); + + // additionally, continuing to use the specified number of columns + // and rows is not a good idea as callers of CalcRowsCols() expect + // that all sizer items can fit into m_cols/m_rows-sized arrays + // which is not the case if there are too many items and results in + // crashes, so let it compute the number of rows automatically by + // forgetting the (wrong) number of rows specified (this also has a + // nice side effect of giving only one assert even if there are + // many more items than allowed in this sizer) + m_rows = 0; + } + } + + return wxSizer::Insert(index, item); +} + int wxGridSizer::CalcRowsCols(int& nrows, int& ncols) const { const int nitems = m_children.GetCount(); if ( m_cols && m_rows ) { - // if both rows and columns are specified by user, use the provided - // values even if we don't have enough items but check that we don't - // have too many of them as this is going to result in problems later ncols = m_cols; nrows = m_rows; - wxASSERT_MSG( ncols*nrows >= nitems, "too many items in grid sizer" ); + // this should be impossible because the too high number of items + // should have been detected by Insert() above + wxASSERT_MSG( nitems <= ncols*nrows, "logic error in wxGridSizer" ); } else if ( m_cols ) { -- 2.45.2