Add range check to Fl_Group::child(int)

Returns NULL if n is out of range to prevent accessing undefined
memory.
This commit is contained in:
Albrecht Schlosser 2024-10-25 02:39:47 +02:00
parent f9f89be7d7
commit c0e07d3452

View File

@ -29,7 +29,7 @@ class Fl_Rect;
/** /**
The Fl_Group class is the FLTK container widget. It maintains The Fl_Group class is the main FLTK container widget. It maintains
an array of child widgets. These children can themselves be any widget an array of child widgets. These children can themselves be any widget
including Fl_Group. The most important subclass of Fl_Group including Fl_Group. The most important subclass of Fl_Group
is Fl_Window, however groups can also be used to control radio buttons is Fl_Window, however groups can also be used to control radio buttons
@ -96,10 +96,22 @@ public:
Returns how many child widgets the group has. Returns how many child widgets the group has.
*/ */
int children() const { return children_; } int children() const { return children_; }
/** /**
Returns array()[n]. <i>No range checking is done!</i> Returns the n'th child.
Returns \c NULL if \c n is out of range (since FLTK 1.4.0).
<i>No range checking was done in FLTK 1.3 and older versions!</i>
\param[in] n index of child (0 .. children() - 1)
\return pointer to the n'th child or NULL if out of range
*/ */
Fl_Widget* child(int n) const {return array()[n];} Fl_Widget *child(int n) const {
if (n < 0 || n > children() - 1) return NULL;
return array()[n];
}
int find(const Fl_Widget*) const; int find(const Fl_Widget*) const;
/** /**
See int Fl_Group::find(const Fl_Widget *w) const See int Fl_Group::find(const Fl_Widget *w) const