Convert int_array to use size_t instead of int as the length

This extends this to allow longer lists with LP32 data model to avoid
limit of 16-bit int.

Signed-off-by: Jouni Malinen <j@w1.fi>
This commit is contained in:
Jouni Malinen 2020-03-22 18:49:04 +02:00
parent 749add5c64
commit 9f9a148af6
3 changed files with 15 additions and 15 deletions

View file

@ -879,9 +879,10 @@ char * freq_range_list_str(const struct wpa_freq_range_list *list)
} }
int int_array_len(const int *a) size_t int_array_len(const int *a)
{ {
int i; size_t i;
for (i = 0; a && a[i]; i++) for (i = 0; a && a[i]; i++)
; ;
return i; return i;
@ -890,24 +891,23 @@ int int_array_len(const int *a)
void int_array_concat(int **res, const int *a) void int_array_concat(int **res, const int *a)
{ {
int reslen, alen, i, new_len; size_t reslen, alen, i, max_size;
int *n; int *n;
reslen = int_array_len(*res); reslen = int_array_len(*res);
alen = int_array_len(a); alen = int_array_len(a);
new_len = reslen + alen + 1; max_size = (size_t) -1;
if (reslen < 0 || alen < 0 || new_len < 0) { if (alen >= max_size - reslen) {
/* This should not really happen, but if it did, something /* This should not really happen, but if it did, something
* overflowed. Do not try to merge the arrays; instead, make * would overflow. Do not try to merge the arrays; instead, make
* this behave like memory allocation failure to avoid messing * this behave like memory allocation failure to avoid messing
* up memory. */ * up memory. */
os_free(*res); os_free(*res);
*res = NULL; *res = NULL;
return; return;
} }
n = os_realloc_array(*res, new_len, sizeof(int)); n = os_realloc_array(*res, reslen + alen + 1, sizeof(int));
if (n == NULL) { if (n == NULL) {
if (new_len)
os_free(*res); os_free(*res);
*res = NULL; *res = NULL;
return; return;
@ -933,8 +933,7 @@ static int freq_cmp(const void *a, const void *b)
void int_array_sort_unique(int *a) void int_array_sort_unique(int *a)
{ {
int alen; size_t alen, i, j;
int i, j;
if (a == NULL) if (a == NULL)
return; return;
@ -959,7 +958,7 @@ void int_array_sort_unique(int *a)
void int_array_add_unique(int **res, int a) void int_array_add_unique(int **res, int a)
{ {
int reslen; size_t reslen, max_size;
int *n; int *n;
for (reslen = 0; *res && (*res)[reslen]; reslen++) { for (reslen = 0; *res && (*res)[reslen]; reslen++) {
@ -967,7 +966,8 @@ void int_array_add_unique(int **res, int a)
return; /* already in the list */ return; /* already in the list */
} }
if (reslen > INT_MAX - 2) { max_size = (size_t) -1;
if (reslen > max_size - 2) {
/* This should not really happen in practice, but if it did, /* This should not really happen in practice, but if it did,
* something would overflow. Do not try to add the new value; * something would overflow. Do not try to add the new value;
* instead, make this behave like memory allocation failure to * instead, make this behave like memory allocation failure to

View file

@ -547,7 +547,7 @@ int freq_range_list_includes(const struct wpa_freq_range_list *list,
unsigned int freq); unsigned int freq);
char * freq_range_list_str(const struct wpa_freq_range_list *list); char * freq_range_list_str(const struct wpa_freq_range_list *list);
int int_array_len(const int *a); size_t int_array_len(const int *a);
void int_array_concat(int **res, const int *a); void int_array_concat(int **res, const int *a);
void int_array_sort_unique(int *a); void int_array_sort_unique(int *a);
void int_array_add_unique(int **res, int a); void int_array_add_unique(int **res, int a);

View file

@ -226,7 +226,7 @@ static int int_array_tests(void)
int test3[] = { 1, 1, 1, -1, 2, 3, 4, 1, 2, 0 }; int test3[] = { 1, 1, 1, -1, 2, 3, 4, 1, 2, 0 };
int test3_res[] = { -1, 1, 2, 3, 4, 0 }; int test3_res[] = { -1, 1, 2, 3, 4, 0 };
int errors = 0; int errors = 0;
int len; size_t len;
wpa_printf(MSG_INFO, "int_array tests"); wpa_printf(MSG_INFO, "int_array tests");