From 1e972a8a37c153ddc15e604592f84f939eb3c2ad Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Sat, 6 Jun 2020 10:14:24 +0200 Subject: [PATCH] calloc-gnu tests: Avoid a test failure with clang. * tests/test-calloc-gnu.c (main): Mark the pointer variable as 'volatile', to defeat compiler optimizations. --- ChangeLog | 6 ++++++ tests/test-calloc-gnu.c | 28 +++++++++++++++++----------- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/ChangeLog b/ChangeLog index 7e3e708521..747d7e5364 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2020-06-06 Bruno Haible + + calloc-gnu tests: Avoid a test failure with clang. + * tests/test-calloc-gnu.c (main): Mark the pointer variable as + 'volatile', to defeat compiler optimizations. + 2020-06-01 Paul Eggert getloadavg: fix double-increment bug diff --git a/tests/test-calloc-gnu.c b/tests/test-calloc-gnu.c index c13ba475fa..2ae392565c 100644 --- a/tests/test-calloc-gnu.c +++ b/tests/test-calloc-gnu.c @@ -36,20 +36,26 @@ int main () { /* Check that calloc (0, 0) is not a NULL pointer. */ - void *p = calloc (0, 0); - if (p == NULL) - return 1; - free (p); + { + void * volatile p = calloc (0, 0); + if (p == NULL) + return 1; + free (p); + } /* Check that calloc fails when requested to allocate a block of memory larger than SIZE_MAX bytes. - We use eight (), not 8, to avoid a compiler warning from GCC 7. */ - p = calloc ((size_t) -1 / 8 + 1, eight ()); - if (p != NULL) - { - free (p); - return 1; - } + We use eight (), not 8, to avoid a compiler warning from GCC 7. + 'volatile' is needed to defeat an incorrect optimization by clang 10, + see . */ + { + void * volatile p = calloc ((size_t) -1 / 8 + 1, eight ()); + if (p != NULL) + { + free (p); + return 2; + } + } return 0; } -- 2.39.5