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>
master
Jouni Malinen 4 years ago
parent 749add5c64
commit 9f9a148af6

@ -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++)
;
return i;
@ -890,25 +891,24 @@ int int_array_len(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;
reslen = int_array_len(*res);
alen = int_array_len(a);
new_len = reslen + alen + 1;
if (reslen < 0 || alen < 0 || new_len < 0) {
max_size = (size_t) -1;
if (alen >= max_size - reslen) {
/* 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
* up memory. */
os_free(*res);
*res = NULL;
return;
}
n = os_realloc_array(*res, new_len, sizeof(int));
n = os_realloc_array(*res, reslen + alen + 1, sizeof(int));
if (n == NULL) {
if (new_len)
os_free(*res);
os_free(*res);
*res = NULL;
return;
}
@ -933,8 +933,7 @@ static int freq_cmp(const void *a, const void *b)
void int_array_sort_unique(int *a)
{
int alen;
int i, j;
size_t alen, i, j;
if (a == NULL)
return;
@ -959,7 +958,7 @@ void int_array_sort_unique(int *a)
void int_array_add_unique(int **res, int a)
{
int reslen;
size_t reslen, max_size;
int *n;
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 */
}
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,
* something would overflow. Do not try to add the new value;
* instead, make this behave like memory allocation failure to

@ -547,7 +547,7 @@ int freq_range_list_includes(const struct wpa_freq_range_list *list,
unsigned int freq);
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_sort_unique(int *a);
void int_array_add_unique(int **res, int a);

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

Loading…
Cancel
Save