Skip to content

src: detect and warn against (void)! constructs in code - #22023

Closed
bagder wants to merge 5 commits into
masterfrom
bagder/void-excl
Closed

bagder wants to merge 5 commits into
masterfrom
bagder/void-excl

Conversation

@bagder

@bagder bagder commented Jun 15, 2026

Copy link
Copy Markdown
Member

No description provided.

@bagder

bagder commented Jun 15, 2026

Copy link
Copy Markdown
Member Author

I think this is a pretty strong argument against -Werror=unused-result. It is rather crazy that (void) is not enough to silence such warnings, but (void)! does. I still believe the latter looks so weird that we should not use it.

@bagder
bagder marked this pull request as ready for review June 15, 2026 12:26
@bagder bagder closed this in 9f25dce Jun 15, 2026
bagder added a commit that referenced this pull request Jun 15, 2026
The reason to use them seems to be that just (void) before a function
call is not enough to silence compiler warnings when return codes are
ignored and -Werror=unused-result is used.

While (void)! apparently works to silence those warnings, it is just too
weird and surprising to readers to use.

It is rather a reason to reconsider the usefulness of the warning.

Closes #22023
@bagder
bagder deleted the bagder/void-excl branch June 15, 2026 12:45
Comment thread tests/server/util.c
if(!serverlogfile) {
static const char msg[] = "exit_signal_handler: serverlogfile not set\n";
(void)!write(STDERR_FILENO, msg, sizeof(msg) - 1);
rc = write(STDERR_FILENO, msg, sizeof(msg) - 1);

@vszakats vszakats Jun 15, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wish I linked the compiler warning that triggered this ugly workaround
in subcommit a420aad.
Also revealing which compiler was this.

I'll fire up a test to see now.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vszakats

Copy link
Copy Markdown
Member

This needs a combination of this GCC warning option (that's enabled
implicitly by some others), and write() being declared with the attribute
warn_unused_result. In CI, only glibc seems to be setting it. Peeking
into the headers, it only happens with FORTIFY_LEVEL > 0. curl doesn't
set this explicitly. And/or maybe related to -O2/-O3/-g/valgrind? I'm not sure.
(It can likely be silenced by -D__wur before including system headers.)

Due to the many conditions it only affects these 5 CI jobs:

dist / AM in-tree & maketgz (pull_request)
Linux / AM awslc (pull_request)
Linux / AM curl_global_init_mem debug valgrind (pull_request)
Linux / CM openssl -O3 libssh valgrind 1 (pull_request)
Linux / CM openssl -O3 libssh valgrind 2 (pull_request)

Seems limited, and the trigger code in the signal handler is kind of special.

vszakats added a commit to vszakats/curl that referenced this pull request Jun 15, 2026
vszakats added a commit that referenced this pull request Jun 15, 2026
In some configurations the `write()` functions gets the
`warn_unused_result` attribute, that makes casting to `(void)`
ineffective to silence this warning. Seen with glibc, in 5 CI jobs.
The warning option appeared in GCC 4.5 and comes enabled by default.

```
tests/server/util.c:329:5: error: ignoring return value of ‘write’ declared with attribute ‘warn_unused_result’ [-Werror=unused-result]
  329 |     write(STDERR_FILENO, msg, sizeof(msg) - 1);
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
```
Ref: https://github.com/curl/curl/actions/runs/27548333990/job/81427544632

Refs:
#22023 (comment)
https://gcc.gnu.org/onlinedocs/gcc-16.1.0/gcc/Warning-Options.html#index-Wunused-result
https://gcc.gnu.org/onlinedocs/gcc-16.1.0/gcc/Common-Attributes.html#index-warn_005funused_005fresult
https://stackoverflow.com/questions/40576003/ignoring-warning-wunused-result

Follow-up to c8d8f08 #22023
Follow-up to e95f509 #16852

Closes #22034
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

2 participants