]> git.ipfire.org Git - thirdparty/hostap.git/commitdiff
Convert int_array to use size_t instead of int as the length
authorJouni Malinen <j@w1.fi>
Sun, 22 Mar 2020 16:49:04 +0000 (18:49 +0200)
committerJouni Malinen <j@w1.fi>
Sun, 22 Mar 2020 16:50:04 +0000 (18:50 +0200)
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>
src/utils/common.c
src/utils/common.h
src/utils/utils_module_tests.c

index 16aa7b6eed7321cd38530be0007c84d00b5440ed..2c12751938e531eb6f1f5fa26a1c765b5b8a74c0 100644 (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++)
                ;
        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
index 833469a52daabbeffe00b4564daef461e84dd9e0..8e5cfe16a855ea14d25c88c7eb8a2392f2963142 100644 (file)
@@ -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);
index f75d4065dd9ead245d0294a13da9c70e8b0ff83b..365f21fb11c63b35b0f483dfa4bfc87724196813 100644 (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_res[] = { -1, 1, 2, 3, 4, 0 };
        int errors = 0;
-       int len;
+       size_t len;
 
        wpa_printf(MSG_INFO, "int_array tests");