pytorch / cpuinfo

CPU INFOrmation library (x86/x86-64/ARM/ARM64, Linux/Windows/Android/macOS/iOS)
BSD 2-Clause "Simplified" License
992 stars 311 forks source link

Fix warning in clog.c about ignoring return value of ‘write’ #77

Closed WilliamTambellini closed 1 year ago

WilliamTambellini commented 2 years ago

Dozens of warnings in clog.c:

[  2%] Linking CXX shared library libonnxruntime_providers_shared.so
/home/wtambellini/repos/onnxruntime/cmake/external/pytorch_cpuinfo/deps/clog/src/clog.c:112:4: warning: ignoring return value of ‘write’, declared with attribute warn_unused_result [-Wunused-result]
    write(STDERR_FILENO, out_buffer, prefix_chars + format_chars + CLOG_SUFFIX_LENGTH);
    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/wtambellini/repos/onnxruntime/cmake/external/pytorch_cpuinfo/deps/clog/src/clog.c: In function ‘clog_vlog_error’:
/home/wtambellini/repos/onnxruntime/cmake/external/pytorch_cpuinfo/deps/clog/src/clog.c:188:4: warning: ignoring return value of ‘write’, declared with attribute warn_unused_result [-Wunused-result]
    write(STDERR_FILENO, out_buffer, prefix_chars + format_chars + CLOG_SUFFIX_LENGTH);
    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/wtambellini/repos/onnxruntime/cmake/external/pytorch_cpuinfo/deps/clog/src/clog.c: In function ‘clog_vlog_warning’:
/home/wtambellini/repos/onnxruntime/cmake/external/pytorch_cpuinfo/deps/clog/src/clog.c:264:4: warning: ignoring return value of ‘write’, declared with attribute warn_unused_result [-Wunused-result]
    write(STDERR_FILENO, out_buffer, prefix_chars + format_chars + CLOG_SUFFIX_LENGTH);
    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/wtambellini/repos/onnxruntime/cmake/external/pytorch_cpuinfo/deps/clog/src/clog.c: In function ‘clog_vlog_info’:
/home/wtambellini/repos/onnxruntime/cmake/external/pytorch_cpuinfo/deps/clog/src/clog.c:340:4: warning: ignoring return value of ‘write’, declared with attribute warn_unused_result [-Wunused-result]
    write(STDOUT_FILENO, out_buffer, prefix_chars + format_chars + CLOG_SUFFIX_LENGTH);
    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
....

line 112 https://github.com/pytorch/cpuinfo/blob/728f3e909fa5c57e0123c4658e234f6b1941385d/deps/clog/src/clog.c#L112

write() indeed returns a size_t : https://linux.die.net/man/2/write Would you mind if I add a check ? eg:

ssize_t rv =  write(STDOUT_FILENO, out_buffer, prefix_chars + format_chars + CLOG_SUFFIX_LENGTH);
asert(rv != -1);
Maratyszcza commented 2 years ago

It would be good to suppress the warning, but assert is not the right way to do it. assert may crash the whole program because of failed logging write.

WilliamTambellini commented 2 years ago

Hi @Maratyszcza
Sure, what about logging (warning ?error ?) if the rv == -1 ?

Maratyszcza commented 2 years ago

rv == -1 indicates that we've had an error during logging. I don't expect anything good attempting to log the error about the logging error. The best option IMO is to ignore the error, and silence the compiler warning about it.

WilliamTambellini commented 2 years ago

Well, totally ignoring write() errors would make debugging even harder but nevermind. So would you accept a PR if I add target_compile_options(... "-Wno-unused-result") to the main CMakeList.txt, only for gcc builds ?

Maratyszcza commented 1 year ago

Fixed in #129