dovecot-2.2: lib: test-array - really really really stop gcc opt...

dovecot at dovecot.org dovecot at dovecot.org
Mon Dec 1 20:44:02 UTC 2014


details:   http://hg.dovecot.org/dovecot-2.2/rev/096d233acb7d
changeset: 18120:096d233acb7d
user:      Phil Carmody <phil at dovecot.fi>
date:      Mon Dec 01 22:43:33 2014 +0200
description:
lib: test-array - really really really stop gcc optimising away fatal tests
Gcc 4.6 also was determined to optimise away the calls that should crash.
We therefore have to do *something*, no matter how stupid, with the return
values. This should do it. (please, oh please, oh please, ...)

Signed-off-by: Phil Carmody <phil at dovecot.fi>

diffstat:

 src/lib/test-array.c |  14 ++++++++------
 1 files changed, 8 insertions(+), 6 deletions(-)

diffs (45 lines):

diff -r 831f7e40546d -r 096d233acb7d src/lib/test-array.c
--- a/src/lib/test-array.c	Mon Dec 01 12:11:54 2014 -0800
+++ b/src/lib/test-array.c	Mon Dec 01 22:43:33 2014 +0200
@@ -181,6 +181,7 @@
 {
 	double tmpd[2] = { 42., -42. };
 	short tmps[8] = {1,2,3,4,5,6,7,8};
+	static const void *useless_ptr; /* persuade gcc to not optimise the tests */
 
 	switch(stage) {
 	case 0: {
@@ -188,8 +189,7 @@
 		test_begin("fatal_array");
 		t_array_init(&ad, 3);
 		/* allocation big enough, but memory not initialised */
-		if (array_idx(&ad, 0) == NULL)
-			return FATAL_TEST_FAILURE;
+		useless_ptr = array_idx(&ad, 0);
 		return FATAL_TEST_FAILURE;
 	} break;
 
@@ -198,8 +198,7 @@
 		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;
+		useless_ptr = array_idx(&ad, 2);
 		return FATAL_TEST_FAILURE;
 	} break;
 
@@ -212,8 +211,11 @@
 		array_copy(&ad.arr, 1, &as.arr, 0, 4);
 		return FATAL_TEST_FAILURE;
 	} break;
-
 	}
 	test_end();
-	return FATAL_TEST_FINISHED;
+	/* Forces the compiler to check the value of useless_ptr, so that it
+	   must call array_idx (which is marked as pure, and gcc was desperate
+	   to optimise out. Of course, gcc is unaware stage is never -1.*/
+	return (useless_ptr != NULL && stage == -1)
+		? FATAL_TEST_FAILURE : FATAL_TEST_FINISHED;
 }


More information about the dovecot-cvs mailing list