]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib: array - Make sure it assert-crashes if trying to add more than UINT_MAX elements
authorTimo Sirainen <timo.sirainen@open-xchange.com>
Wed, 20 May 2020 08:18:48 +0000 (11:18 +0300)
committeraki.tuomi <aki.tuomi@open-xchange.com>
Fri, 15 Jan 2021 17:26:51 +0000 (17:26 +0000)
This is required, because array_count() returns unsigned int.

src/lib/array.h
src/lib/test-array.c

index eef813f322efc2851ff0c9213d36d992f1a7b6ff..85eca3a1a81f01caafa607ad6269beb5cb996382 100644 (file)
@@ -126,7 +126,9 @@ array_create_i(struct array *array, pool_t pool,
 {
        buffer_t *buffer;
 
-        buffer = buffer_create_dynamic(pool, init_count * element_size);
+       buffer = buffer_create_dynamic_max(pool, init_count * element_size,
+               SIZE_MAX / element_size < UINT_MAX ? SIZE_MAX :
+               UINT_MAX * element_size);
        array_create_from_buffer_i(array, buffer, element_size);
 }
 #define array_create(array, pool, element_size, init_count) \
index 1799487ddda4e81393509512ed9f1f5022eda2c0..99085304b705ee80ba038aafcde38afdc58494d7 100644 (file)
@@ -361,6 +361,30 @@ enum fatal_test_state fatal_array(unsigned int stage)
                array_copy(&ad.arr, 1, &as.arr, 0, 4);
                return FATAL_TEST_FAILURE;
        }
+       case 3: {
+               ARRAY(uint8_t) arr;
+               uint8_t value = 0;
+
+               t_array_init(&arr, 2);
+               array_push_back(&arr, &value);
+               test_expect_fatal_string("Buffer write out of range");
+               /* this is supposed to assert-crash before it even attempts to
+                  access value */
+               array_append(&arr, &value, UINT_MAX);
+               return FATAL_TEST_FAILURE;
+       }
+       case 4: {
+               ARRAY(uint32_t) arr;
+               uint32_t value = 0;
+
+               t_array_init(&arr, 2);
+               array_push_back(&arr, &value);
+               test_expect_fatal_string("Buffer write out of range");
+               /* this is supposed to assert-crash before it even attempts to
+                  access value */
+               array_append(&arr, &value, UINT_MAX);
+               return FATAL_TEST_FAILURE;
+       }
        }
        test_end();
        /* Forces the compiler to check the value of useless_ptr, so that it