bug-gnulib@gnu.org mirror (unofficial)
 help / color / mirror / Atom feed
From: Bruno Haible <bruno@clisp.org>
To: bug-gnulib@gnu.org
Subject: limits-h: Fix BOOL_MAX value
Date: Mon, 12 Feb 2024 14:09:25 +0100	[thread overview]
Message-ID: <5575962.44csPzL39Z@nimes> (raw)

With clang 17 and CC="clang -std=gnu2x", I see this compilation error:

../../gltests/test-limits-h.c:118:16: error: static assertion failed due to requirement '1 == (((1U << (8 - 1)) - 1) * 2) + 1'
  118 | static_assert (BOOL_MAX == (((1U << (BOOL_WIDTH - 1)) - 1) * 2) + 1);
      |                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/limits.h:186:20: note: expanded from macro 'BOOL_MAX'
  186 | #  define BOOL_MAX 1
      |                    ^
1 error generated.
make[3]: *** [Makefile:3853: test-limits-h.o] Error 1

The cause are the values of BOOL_WIDTH and BOOL_MAX. This program
================================================================================
#include <limits.h>
#include <stdint.h>
#include <stdio.h>
int main () { printf ("BOOL_WIDTH = %d, BOOL_MAX = %d\n", BOOL_WIDTH, BOOL_MAX); }
================================================================================
prints
  BOOL_WIDTH = 1, BOOL_MAX = 1
with CC="gcc -std=gnu2x", but
  BOOL_WIDTH = 8, BOOL_MAX = 1
with CC="clang -std=gnu2x" (for all clang version 14 ... 17).

Apparently I had misinterpreted the meaning of BOOL_WIDTH. (Cf.
ISO C 23 § E.(3)).

This patch fixes the compilation error.


2024-02-12  Bruno Haible  <bruno@clisp.org>

	limits-h: Fix BOOL_MAX value.
	* lib/limits.in.h (BOOL_MAX): Define to 1, not to 2^BOOL_WIDTH-1.
	* tests/test-limits-h.c (main): Change expected value of BOOL_MAX.

diff --git a/lib/limits.in.h b/lib/limits.in.h
index 236fc58e52..c65eb4c1cf 100644
--- a/lib/limits.in.h
+++ b/lib/limits.in.h
@@ -130,7 +130,7 @@
 #  define BOOL_WIDTH 1
 #  define BOOL_MAX 1
 # elif ! defined BOOL_MAX
-#  define BOOL_MAX ((((1U << (BOOL_WIDTH - 1)) - 1) << 1) + 1)
+#  define BOOL_MAX 1
 # endif
 #endif
 
diff --git a/tests/test-limits-h.c b/tests/test-limits-h.c
index 817e239c6e..6f574c88a0 100644
--- a/tests/test-limits-h.c
+++ b/tests/test-limits-h.c
@@ -115,7 +115,7 @@ verify_width (ULLONG_WIDTH, 0, ULLONG_MAX);
 /* Macros specified by C23.  */
 
 int bool_attrs[] = { BOOL_MAX, BOOL_WIDTH };
-static_assert (BOOL_MAX == (((1U << (BOOL_WIDTH - 1)) - 1) * 2) + 1);
+static_assert (BOOL_MAX == 1);
 
 static_assert (0 < MB_LEN_MAX);
 





             reply	other threads:[~2024-02-12 13:09 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-12 13:09 Bruno Haible [this message]
2024-02-12 14:48 ` limits-h: Fix BOOL_MAX value Collin Funk
2024-02-12 16:25   ` Bruno Haible

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://lists.gnu.org/mailman/listinfo/bug-gnulib

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5575962.44csPzL39Z@nimes \
    --to=bruno@clisp.org \
    --cc=bug-gnulib@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).