From 526d3b8a53f19874544a8cc9695ac88ddbde3493 Mon Sep 17 00:00:00 2001 From: Viktor Szakats Date: Mon, 15 Jun 2026 21:27:51 +0200 Subject: [PATCH] servers: silence `-Wunused-result` with pragma MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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: https://github.com/curl/curl/pull/22023#issuecomment-4708455631 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 c8d8f081fd62c68c588f8963a8e441c730a23b5b #22023 Follow-up to e95f509c66abdd88ae02e3243cdc217f19c4a330 #16852 Closes #22034 --- tests/server/util.c | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/tests/server/util.c b/tests/server/util.c index 77aec5aaaf..a6511598dc 100644 --- a/tests/server/util.c +++ b/tests/server/util.c @@ -313,22 +313,25 @@ static HWND hidden_main_window = NULL; * should finish its execution in a controlled manner as soon as possible. * The first time this is called it sets got_exit_signal to 1 and * stores in exit_signal the signal that triggered its execution. - */ -/* + * * Only call signal-safe functions from the signal handler, as required by * the POSIX specification: * https://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html * Hence, do not call 'logmsg()', and instead use 'open/write/close' to * log errors. */ -static size_t useless; /* to silence variable 'rc' set but not used */ +/* suppress warnings seen in configurations where 'write()' has the attribute + 'warn_unused_result', which is not silenced by casting to '(void)'. */ +#if defined(CURL_HAVE_DIAG) && !defined(__clang__) +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wunused-result" /* GCC 4.5+ */ +#endif static void exit_signal_handler(int signum) { int old_errno = errno; - size_t rc = 0; if(!serverlogfile) { static const char msg[] = "exit_signal_handler: serverlogfile not set\n"; - rc = write(STDERR_FILENO, msg, sizeof(msg) - 1); + (void)write(STDERR_FILENO, msg, sizeof(msg) - 1); } else { int fd = -1; @@ -342,14 +345,14 @@ static void exit_signal_handler(int signum) if(fd != -1) { #endif static const char msg[] = "exit_signal_handler: called\n"; - rc = write(fd, msg, sizeof(msg) - 1); + (void)write(fd, msg, sizeof(msg) - 1); curlx_close(fd); } else { static const char msg[] = "exit_signal_handler: failed opening "; - rc = write(STDERR_FILENO, msg, sizeof(msg) - 1); - rc += write(STDERR_FILENO, serverlogfile, strlen(serverlogfile)); - rc += write(STDERR_FILENO, "\n", 1); + (void)write(STDERR_FILENO, msg, sizeof(msg) - 1); + (void)write(STDERR_FILENO, serverlogfile, strlen(serverlogfile)); + (void)write(STDERR_FILENO, "\n", 1); } } if(got_exit_signal == 0) { @@ -360,10 +363,12 @@ static void exit_signal_handler(int signum) (void)SetEvent(exit_event); #endif } - useless = rc; (void)signal(signum, exit_signal_handler); errno = old_errno; } +#if defined(CURL_HAVE_DIAG) && !defined(__clang__) +#pragma GCC diagnostic pop +#endif #ifdef _WIN32 /* CTRL event handler for Windows Console applications to simulate