From 2c5ba944582a8101cd3fd7445814a2e50b4701bd Mon Sep 17 00:00:00 2001 From: Albrecht Schlosser Date: Fri, 25 Jan 2019 17:39:20 +0100 Subject: Fix Fl_Tree::insert() with pos out ouf range (#18) The given position to insert a new item was not checked against the valid range so the program could crash if a position less than zero or greater than children() was given. The position is now clamped to the valid range, i.e. the item is either prepended or appended. Fixes issue #18. --- src/Fl_Tree_Item_Array.cxx | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'src/Fl_Tree_Item_Array.cxx') diff --git a/src/Fl_Tree_Item_Array.cxx b/src/Fl_Tree_Item_Array.cxx index 1da368c9a..0503a8000 100644 --- a/src/Fl_Tree_Item_Array.cxx +++ b/src/Fl_Tree_Item_Array.cxx @@ -109,9 +109,14 @@ void Fl_Tree_Item_Array::enlarge(int count) { /// Insert an item at index position \p pos. /// /// Handles enlarging array if needed, total increased by 1. -/// If \p pos == total(), an empty item is appended to the array. +/// If \p pos \>= total(), the item is appended to the array. +/// If \p pos \< 0, the item is prepended (works like pos == 0). /// void Fl_Tree_Item_Array::insert(int pos, Fl_Tree_Item *new_item) { + if (pos < 0) + pos = 0; + else if (pos > _total) + pos = _total; enlarge(1); // printf("*** POS=%d TOTAL-1=%d NITEMS=%d\n", pos, _total-1, (_total-pos)); if ( pos <= (_total - 1) ) { // need to move memory around? -- cgit v1.2.3