Live data from Hacker News

GCC proves an uninitialized variable must be 0 and doesn't warn about it

lkml.org

11–20 of 67 posts

Re: GCC proves an uninitialized variable must be 0 and doesn't warn about it

#11
post #8

To summarize various replies in the thread: This is a variation of an unfixed 2004 GCC bug. Clang detects the issue when the right warnings are enabled. Those warnings are disabled currently. A developer is auditing and fixing all such instances found by Clang so that they can reenable those warnings.

What are the right warnings?

I think -Wsometimes-uninitialized ?

https://github.com/ClangBuiltLinux/linux/issues/381

Re: GCC proves an uninitialized variable must be 0 and doesn't warn about it

#12

It look like a bug to me, since inode->i_nlink may be zero, and then ret is uninitialized (optimizing the final "return ret" to "return 0" should be safe (but is not necessarily worth it, depending on the target instruction set and ABI), although if warnings are enabled, it seem like the warning should still be displayed).

The compiler is allowed to do that. Since it would be undefined behavior to read the undefined variable, the compiler gets to pick whatever value it wants for it.

So after the if, the compiler sees that ret is either 0, or an undefined value.

It picks 0 as the undefined value since that's a good optimization, so ret is now always 0, and (in this case) there is no warning.

Re: GCC proves an uninitialized variable must be 0 and doesn't warn about it

#17

Random question - what are the _underscores_ that show up frequently in Linus's responses?

It's for emphasis. Some people use asterisks. He uses underscores.

Note he uses both, even in the same paragraph - asterisks typically mean "bold," whereas underscores typically mean "underlined."

Re: GCC proves an uninitialized variable must be 0 and doesn't warn about it

#18
post #12

It look like a bug to me, since inode->i_nlink may be zero, and then ret is uninitialized (optimizing the final "return ret" to "return 0" should be safe (but is not necessarily worth it, depending on the target instruction set and ABI), although if warnings are enabled, it seem like the warning should still be displayed).

The compiler is allowed to do that. Since it would be undefined behavior to read the undefined variable, the compiler gets to pick whatever value it wants for it. So after the if, the compiler sees that ret is either 0, or an undefined value. It picks 0 as the undefined value since that's a good optimization, so ret is now always 0, and (in this case) there is no warning.

Right, the bug in the kernel code snippet is like GP describes, but the gcc bug isn't that the compiled code is incorrect, it's that people expect an "uninitialized variable" warning and the compiler doesn't produce one.

Re: GCC proves an uninitialized variable must be 0 and doesn't warn about it

#20
What I don't understand is why it doesn't optimize away the check on (inode->i_nlink), since it is assuming the code in the dependent block always runs.

It has to be assuming that, because it is the only path that doesn't lead to undefined behavior.

Post reply on HN