Change bounds to a const variable

Add one more bounds check
This commit is contained in:
Dwedit 2018-05-28 12:14:04 -05:00
parent e8b381680a
commit adbc71ce46
1 changed files with 6 additions and 3 deletions

View File

@ -50,6 +50,7 @@ static void input_state_set_last(unsigned port, unsigned device,
{ {
unsigned i; unsigned i;
InputListElement *element = NULL; InputListElement *element = NULL;
const int MAX_ID = sizeof(element->state) / sizeof(int16_t);
if (!input_state_list) if (!input_state_list)
mylist_create(&input_state_list, 16, mylist_create(&input_state_list, 16,
@ -62,7 +63,7 @@ static void input_state_set_last(unsigned port, unsigned device,
if ( (element->port == port) && if ( (element->port == port) &&
(element->device == device) && (element->device == device) &&
(element->index == index) && (element->index == index) &&
(id >= 0 && id < (sizeof(element->state) / sizeof(int16_t))) (id >= 0 && id < MAX_ID)
) )
{ {
element->state[id] = value; element->state[id] = value;
@ -75,7 +76,8 @@ static void input_state_set_last(unsigned port, unsigned device,
element->port = port; element->port = port;
element->device = device; element->device = device;
element->index = index; element->index = index;
element->state[id] = value; if (id >= 0 && id < MAX_ID)
element->state[id] = value;
} }
static int16_t input_state_get_last(unsigned port, static int16_t input_state_get_last(unsigned port,
@ -91,11 +93,12 @@ static int16_t input_state_get_last(unsigned port,
{ {
InputListElement *element = InputListElement *element =
(InputListElement*)input_state_list->data[i]; (InputListElement*)input_state_list->data[i];
const int MAX_ID = sizeof(element->state) / sizeof(int16_t);
if ( (element->port == port) && if ( (element->port == port) &&
(element->device == device) && (element->device == device) &&
(element->index == index) && (element->index == index) &&
(id >= 0 && id < (sizeof(element->state) / sizeof(int16_t))) (id >= 0 && id < MAX_ID)
) )
return element->state[id]; return element->state[id];
} }