Fix crash when using a wxRadioBox without items in wxOSX

wxRadioBox::m_radioButtonCycle is never null -- except when it is, when
there are no items at all in the radio box. Handle this case without
crashing too by checking for the pointer being valid before using it.

Also add a unit test checking that this works correctly.

See #22751.
This commit is contained in:
Vadim Zeitlin 2022-08-29 18:39:09 +02:00
parent 5a6f2796d5
commit b0c417db36
2 changed files with 25 additions and 4 deletions

View file

@ -59,18 +59,21 @@ wxRadioBox::~wxRadioBox()
wxRadioButton *next, *current;
current = m_radioButtonCycle->NextInCycle();
current = m_radioButtonCycle;
if (current != NULL)
{
while (current != m_radioButtonCycle)
// We need to start deleting the buttons from the second one because
// deleting the first one would change the pointers stored in them.
for (current = current->NextInCycle();;)
{
next = current->NextInCycle();
delete current;
if (next == m_radioButtonCycle)
break;
current = next;
}
delete current;
}
}
@ -339,6 +342,9 @@ void wxRadioBox::Command( wxCommandEvent& event )
//
void wxRadioBox::SetFocus()
{
if (!m_radioButtonCycle)
return;
wxRadioButton *current;
current = m_radioButtonCycle;

View file

@ -16,6 +16,7 @@
#include "wx/radiobox.h"
#endif // WX_PRECOMP
#include "wx/scopedptr.h"
#include "wx/tooltip.h"
class RadioBoxTestCase
@ -192,4 +193,18 @@ TEST_CASE_METHOD(RadioBoxTestCase, "RadioBox::SetString", "[radiobox]")
CHECK( m_radio->GetString(2) == "" );
}
TEST_CASE("RadioBox::NoItems", "[radiobox]")
{
wxScopedPtr<wxRadioBox>
radio(new wxRadioBox(wxTheApp->GetTopWindow(), wxID_ANY, "Empty",
wxDefaultPosition, wxDefaultSize,
0, NULL,
1, wxRA_SPECIFY_COLS));
CHECK( radio->GetCount() == 0 );
CHECK( radio->IsEmpty() );
CHECK_NOTHROW( radio->SetFocus() );
}
#endif // wxUSE_RADIOBOX