]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib: test-array - remove possibility to optimise out tests
authorPhil Carmody <phil@dovecot.fi>
Fri, 28 Nov 2014 08:57:43 +0000 (10:57 +0200)
committerPhil Carmody <phil@dovecot.fi>
Fri, 28 Nov 2014 08:57:43 +0000 (10:57 +0200)
(void) on a call to a "pure" function was causing some compilers (gcc 4.8)
to not reach the assert within the pure function. Technically it's not a
pure function, as (in the assert fail case) it has effects other than its
return value. However, simply removing the void cast (and then actually
using the return value) also prevents this optimisation.

An additional fatal test was added - there's a difference between an access
outside the allocated range and outside the initialised range.

Signed-off-by: Phil Carmody <phil@dovecot.fi>
src/lib/array.h
src/lib/test-array.c

index 166cf5876d14387bc225f8077f7ebe4bf06b961d..5d463dda32d8a5c2e324478cd8fac20836c139b6 100644 (file)
@@ -197,6 +197,7 @@ array_get_i(const struct array *array, unsigned int *count_r)
 #define array_get(array, count) \
        ARRAY_TYPE_CAST_CONST(array)array_get_i(&(array)->arr, count)
 
+/* Re: i_assert() vs. pure: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=51971#c1 */
 static inline const void * ATTR_PURE
 array_idx_i(const struct array *array, unsigned int idx)
 {
index fb052795d88382f81f55adc7aafe951ed25f2eca..38caff0bda0b7e4d5223ff21044cc7ddf5f2f75e 100644 (file)
@@ -179,20 +179,36 @@ void test_array(void)
 
 enum fatal_test_state fatal_array(int stage)
 {
+       double tmpd[2] = { 42., -42. };
+       short tmps[8] = {1,2,3,4,5,6,7,8};
+
        switch(stage) {
        case 0: {
                ARRAY(double) ad;
                test_begin("fatal_array");
                t_array_init(&ad, 3);
-               (void)array_idx(&ad, 3);
+               /* allocation big enough, but memory not initialised */
+               if (array_idx(&ad, 0) == NULL)
+                       return FATAL_TEST_FAILURE;
                return FATAL_TEST_FAILURE;
        } break;
 
        case 1: {
+               ARRAY(double) ad;
+               t_array_init(&ad, 2);
+               array_append(&ad, tmpd, 2);
+               /* actual out of range address requested */
+               if (array_idx(&ad, 2) == NULL)
+                       return FATAL_TEST_FAILURE;
+               return FATAL_TEST_FAILURE;
+       } break;
+
+       case 2: {
                ARRAY(double) ad;
                ARRAY(short) as;
                t_array_init(&ad, 2);
                t_array_init(&as, 8);
+               array_append(&as, tmps, 2);
                array_copy(&ad.arr, 1, &as.arr, 0, 4);
                return FATAL_TEST_FAILURE;
        } break;