dovecot-2.2: lib: test-array - remove possibility to optimise ou...

dovecot at dovecot.org dovecot at dovecot.org
Fri Nov 28 08:58:05 UTC 2014


details:   http://hg.dovecot.org/dovecot-2.2/rev/8a4b536705ad
changeset: 18117:8a4b536705ad
user:      Phil Carmody <phil at dovecot.fi>
date:      Fri Nov 28 10:57:43 2014 +0200
description:
lib: test-array - remove possibility to optimise out tests
(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 at dovecot.fi>

diffstat:

 src/lib/array.h      |   1 +
 src/lib/test-array.c |  18 +++++++++++++++++-
 2 files changed, 18 insertions(+), 1 deletions(-)

diffs (52 lines):

diff -r 247f3742a3c2 -r 8a4b536705ad src/lib/array.h
--- a/src/lib/array.h	Fri Nov 28 10:57:43 2014 +0200
+++ b/src/lib/array.h	Fri Nov 28 10:57:43 2014 +0200
@@ -197,6 +197,7 @@
 #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)
 {
diff -r 247f3742a3c2 -r 8a4b536705ad src/lib/test-array.c
--- a/src/lib/test-array.c	Fri Nov 28 10:57:43 2014 +0200
+++ b/src/lib/test-array.c	Fri Nov 28 10:57:43 2014 +0200
@@ -179,20 +179,36 @@
 
 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;


More information about the dovecot-cvs mailing list