CComponentsForm: avoid segfault, if id is out of range

This commit is contained in:
2014-08-26 18:49:08 +02:00
committed by [CST] Focus
parent 67a42c3e96
commit 0ab881b338
2 changed files with 24 additions and 10 deletions

View File

@@ -445,28 +445,42 @@ u_int8_t CComponentsForm::getPageCount()
} }
void CComponentsForm::setSelectedItem(size_t item_id) void CComponentsForm::setSelectedItem(int item_id)
{ {
size_t btn_count = v_cc_items.size(); size_t count = v_cc_items.size();
int id = item_id;
if (item_id > (btn_count-1) || (btn_count == 0)){ if (id > (count-1) || id < 0 || (count == 0)){
dprintf(DEBUG_NORMAL, "[CComponentsForm] [%s - %d] invalid parameter item_id = %u, available items = %u, allowed values are: 0...%u! \n", __func__, dprintf(DEBUG_NORMAL, "[CComponentsForm] [%s - %d] invalid parameter item_id = %u, available items = %u, allowed values are: 0...%u! \n", __func__,
__LINE__, __LINE__,
item_id, item_id,
btn_count, count,
btn_count==0 ? 0:btn_count-1); count==0 ? 0:count-1);
return; //exit if no item is available
if (count == 0)
return;
//jump to last item
if (id < 0)
id = count-1;
//jump to 1st item, if id is out of range, avoids also possible segfault
if (id > (count-1))
id = 0;
} }
for (size_t i= 0; i< btn_count; i++) for (size_t i= 0; i< count; i++)
v_cc_items[i]->setSelected(i == item_id); v_cc_items[i]->setSelected(i == (size_t)id);
OnSelect(); OnSelect();
} }
void CComponentsForm::setSelectedItem(CComponentsItem* cc_item) void CComponentsForm::setSelectedItem(CComponentsItem* cc_item)
{ {
size_t id = getCCItemId(cc_item); int id = getCCItemId(cc_item);
if (id == -1){
dprintf(DEBUG_NORMAL, "[CComponentsForm] [%s - %d] invalid item parameter, no object available\n", __func__,__LINE__);
return;
}
setSelectedItem(id); setSelectedItem(id);
} }

View File

@@ -120,7 +120,7 @@ class CComponentsForm : public CComponentsItem
///returns id of select item, return value as int, returns -1: if is nothing selected ///returns id of select item, return value as int, returns -1: if is nothing selected
virtual int getSelectedItem(); virtual int getSelectedItem();
///select a definied item, parameter1 as size_t ///select a definied item, parameter1 as size_t
virtual void setSelectedItem(size_t item_id); virtual void setSelectedItem(int item_id);
///select a definied item, parameter1 as CComponentsItem* ///select a definied item, parameter1 as CComponentsItem*
virtual void setSelectedItem(CComponentsItem* cc_item); virtual void setSelectedItem(CComponentsItem* cc_item);
}; };