protocolbuffers / protobuf

Protocol Buffers - Google's data interchange format
http://protobuf.dev
Other
65.76k stars 15.51k forks source link

Error: may be used uninitialized in this function when using `-Os` optimization #11148

Closed jtilahun closed 1 year ago

jtilahun commented 1 year ago

What version of protobuf and what language are you using? Version: v3.19.5 Language: C++

What operating system (Linux, Windows, ...) and version? Linux Ubuntu 20.04

What runtime / compiler are you using (e.g., python version or gcc version) powerpc-linux-gnuspe-gcc (Wind River VxWorks GCC 8.1.0.1 - 2019.03.13) 8.1.0

What did you do? I attempted to compile coded_stream.cc with the -Os optimization level flag to optimize for size, as well as the -Wall and -Werror flags to enable warnings and treat all warnings as errors, respectively.

What did you expect to see I expected to see the compilation to proceed successfully with no errors.

What did you see instead? I saw the compilation fail with the below errors:

external/com_google_protobuf/src/google/protobuf/io/coded_stream.cc: In member function 'int64_t google::protobuf::io::CodedInputStream::ReadVarint32Fallback(uint32_t)':
external/com_google_protobuf/src/google/protobuf/io/coded_stream.cc:469:12: error: 'temp' may be used uninitialized in this function [-Werror=maybe-uninitialized]
     return temp;
            ^~~~
external/com_google_protobuf/src/google/protobuf/io/coded_stream.cc: In member function 'uint32_t google::protobuf::io::CodedInputStream::ReadTagFallback(uint32_t)':
external/com_google_protobuf/src/google/protobuf/io/coded_stream.cc:549:12: error: 'tag' may be used uninitialized in this function [-Werror=maybe-uninitialized]
     return tag;
            ^~~

Note that the errors do not appear when the -O2 optimization level flag is used. The errors only appear when the -Os optimization level flag is used.

Anything else we should know about your project / environment Bazel v4.2.1 is used for the build system.

jorgbrown commented 1 year ago

maybe-uninitialized is not a perfect diagnostic, and you really shouldn't be using it in a warnings-are-errors situation. This comes up from to time, for example here -> https://stackoverflow.com/questions/51093415/why-does-this-code-trigger-gccs-maybe-uninitialized-warning

This particular compiler problem was fixed in 11.1,as you can see at https://godbolt.org/z/Pes7s47K3

So either update your compiler, or turn its broken warning off.