Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(void)fwrite triggers -Wunused-result again #2185

Closed
BillyDonahue opened this issue Mar 18, 2021 · 3 comments
Closed

(void)fwrite triggers -Wunused-result again #2185

BillyDonahue opened this issue Mar 18, 2021 · 3 comments

Comments

@BillyDonahue
Copy link
Contributor

BillyDonahue commented Mar 18, 2021

Regression of #1098 introduced here 211d312

https://github.com/fmtlib/fmt/blob/master/include/fmt/format-inl.h#L158

Unfortunately the (void) cast doesn't actually convince GCC that we've paid attention to the std::fwrite result.

Maybe report_error can return a bool indicating whether the error was reported successfullly, calculated from the fwrite result. We don't have to actually look at that bool, but GCC may shut up if we look like we care.

It's all kind of silly. The std::fputc on the next line has no such annotation but it's just as capable of failing.

 [2021/03/17 13:37:16.683] /opt/mongodbtoolchain/v3/bin/g++ -o build/cached/third_party/fmt/dist/src/format.o -c -std=c++17 -Wall  -Werror  src/third_party/fmt/dist/src/format.cc

 [2021/03/17 13:37:18.244] In file included from src/third_party/fmt/dist/src/format.cc:8:
 [2021/03/17 13:37:18.244] src/third_party/fmt/dist/include/fmt/format-inl.h: In function 'void fmt::v7::detail::report_error(fmt::v7::detail::format_func, int, fmt::v7::string_view)':
 [2021/03/17 13:37:18.244] src/third_party/fmt/dist/include/fmt/format-inl.h:158:20: error: ignoring return value of 'size_t fwrite(const void*, size_t, size_t, FILE*)', declared with attribute warn_unused_result [-Werror=unused-result]
 [2021/03/17 13:37:18.244]    (void)std::fwrite(full_message.data(), full_message.size(), 1, stderr);
 [2021/03/17 13:37:18.244]          ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@vitaut
Copy link
Contributor

vitaut commented Mar 18, 2021

What gcc version are you using? I wasn't able to repro this on gcc 9.1: https://godbolt.org/z/5EPcbY.

BillyDonahue added a commit to mongodb-forks/fmt that referenced this issue Mar 20, 2021
@BillyDonahue
Copy link
Contributor Author

BillyDonahue commented Mar 20, 2021

It's GCC 8.3, but I think the more important thing is the system's glibc version, as the __wur marking was removed from fwrite etc in glibc 2.16 (Bugzilla#11959). This is a Red Hat Enterprise Linux 6.2 machine, still using glibc 2.12. Godbolt can't select glibc versions, unfortunately.

$ rpm -q glibc
glibc-2.12-1.212.el6_10.3.x86_64

$ cat /etc/redhat-release
Red Hat Enterprise Linux Server release 6.2 (Santiago)

$ uname -a
Linux ip-... 2.6.32-220.el6.x86_64 #1 SMP Wed Nov 9 08:03:13 EST 2011 x86_64 x86_64 x86_64 GNU/Linux

$ /opt/mongodbtoolchain/v3/bin/g++ --version
g++ (GCC) 8.3.0
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

$ cat fwrite_test.cpp
#include <cstdio>
int main() {
    const char msg[] = "Hello, world!\n";
    (void)fwrite(msg, 1, sizeof(msg)-1, stdout);
    return 0;
}

$ /opt/mongodbtoolchain/v3/bin/g++ -O2 -D_FORTIFY_SOURCE=1 fwrite_test.cpp -o fwrite_test
fwrite_test.cpp: In function 'int main()':
fwrite_test.cpp:6:17: warning: ignoring return value of 'size_t fwrite(const void*, size_t, size_t, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
     (void)fwrite(msg, 1, sizeof(msg)-1, stdout);
           ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

least-annoying fix for this that I've seen is to use an if condition to uselessly examine the result.
I've verified that it works.

if (fwrite(msg, 1, sizeof(msg)-1, stdout)){}

@vitaut
Copy link
Contributor

vitaut commented Mar 31, 2021

Fixed in 308510e.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants