bug-gnulib@gnu.org mirror (unofficial)
 help / color / mirror / Atom feed
From: Bruno Haible <bruno@clisp.org>
To: bug-gnulib@gnu.org, Collin Funk <collin.funk1@gmail.com>
Cc: Paul Eggert <eggert@cs.ucla.edu>
Subject: Re: byteswap.h behavior
Date: Sun, 12 May 2024 22:02:04 +0200	[thread overview]
Message-ID: <4989361.0VBMTVartN@nimes> (raw)
In-Reply-To: <bf40370e-abe5-4eb3-a6e6-dbfeaca7ad34@gmail.com>

Collin Funk wrote:
> I worry though that in some cases programs will be accustomed to
> glibc's behavior. Since all of the functions are just macros to static
> inline functions in <bits/byteswap.h> arguments will only be evaluated
> once. It seems like it could be the cause of some unexpected bugs...

The simple fix to your worry is: Let the user use '-Wall' routinely.
gcc has a warning option '-Wsequence-point', included in '-Wall'.
clang has a warning option '-Wunsequenced', included in '-Wall'.
The do the job, and I have not seen them produce false warnings ever.

$ gcc -Wall foo.c
foo.c: In function ‘main’:
foo.c:15:59: warning: operation on ‘value_macro’ may be undefined [-Wsequence-point]
   15 |   printf ("2. %#" PRIX32 "\n", bswap_32_macro (value_macro++));
      |                                                           ^
foo.c:7:31: note: in definition of macro ‘bswap_32_macro’
    7 |                            (((x) & 0x00FF0000) >> 8) |  \
      |                               ^
foo.c:15:59: warning: operation on ‘value_macro’ may be undefined [-Wsequence-point]
   15 |   printf ("2. %#" PRIX32 "\n", bswap_32_macro (value_macro++));
      |                                                           ^
foo.c:7:31: note: in definition of macro ‘bswap_32_macro’
    7 |                            (((x) & 0x00FF0000) >> 8) |  \
      |                               ^
foo.c:15:59: warning: operation on ‘value_macro’ may be undefined [-Wsequence-point]
   15 |   printf ("2. %#" PRIX32 "\n", bswap_32_macro (value_macro++));
      |                                                           ^
foo.c:7:31: note: in definition of macro ‘bswap_32_macro’
    7 |                            (((x) & 0x00FF0000) >> 8) |  \
      |                               ^

$ clang -Wall foo.c
foo.c:15:59: warning: multiple unsequenced modifications to 'value_macro' [-Wunsequenced]
   15 |   printf ("2. %#" PRIX32 "\n", bswap_32_macro (value_macro++));
      |                                                           ^~
foo.c:5:31: note: expanded from macro 'bswap_32_macro'
    5 | #define bswap_32_macro(x) ((((x) & 0x000000FF) << 24) | \
      |                               ^
    6 |                            (((x) & 0x0000FF00) << 8) |  \
      |                               ~
1 warning generated.

Assume this warning option is used. Then you can leave byteswap.h as it is.

Bruno





  reply	other threads:[~2024-05-12 20:02 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-12 19:23 byteswap.h behavior Collin Funk
2024-05-12 19:38 ` Paul Eggert
2024-05-12 19:47   ` Collin Funk
2024-05-12 20:02     ` Bruno Haible [this message]
2024-05-12 20:11       ` Collin Funk
2024-05-12 20:30     ` Paul Eggert
2024-05-12 22:43       ` Collin Funk
2024-05-12 23:10         ` endian.h Bruno Haible
2024-05-12 19:50 ` byteswap.h behavior Jeffrey Walton

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=4989361.0VBMTVartN@nimes \
    --to=bruno@clisp.org \
    --cc=bug-gnulib@gnu.org \
    --cc=collin.funk1@gmail.com \
    --cc=eggert@cs.ucla.edu \
    /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).