LWN: Comments on "Removing uninitialized_var()" https://lwn.net/Articles/529954/ This is a special feed containing comments posted to the individual LWN article titled "Removing uninitialized_var()". en-us Mon, 13 Oct 2025 17:46:00 +0000 Mon, 13 Oct 2025 17:46:00 +0000 https://www.rssboard.org/rss-specification lwn@lwn.net Removing uninitialized_var() https://lwn.net/Articles/532460/ https://lwn.net/Articles/532460/ oak <div class="FormattedComment"> GCC not detecting all the cases where variable might be uninitialized (and it requiring optimizations enabled to find what it can find) is one thing, but it's not what annoys people. Wrong warnings are what annoy them.<br> <p> As noted, GCC does these checks using information gathered with the optimization passes. A bug in GCC 4.5 and earlier was that it did the warning before all optimization passes had been done and therefore could give warnings for code paths that weren't relevant (= dead, would never happen). This was (at least mostly) fixed in GCC 4.6 and there is/are bugs about it in the GCC bugzilla.<br> <p> Even if kernel people wouldn't fix GCC bugs instead of kludging kernel code, at least they could file bugs against GCC, or if there's already a bug on the issue, add a pointer to it and comment in code about which GCC version fixes that bug.<br> <p> (On quick glance of these kind of bugs in GCC bug tracker, most of them seem to be pretty old and might be already fixed with GCC 4.6, I think they need a bit of a re-testing &amp; cleanup.)<br> <p> </div> Sun, 13 Jan 2013 00:39:54 +0000 Removing uninitialized_var() https://lwn.net/Articles/531581/ https://lwn.net/Articles/531581/ nix <div class="FormattedComment"> It's impossible to get it right *in the general case*. You can get it right in an arbitrarily large percentage of special cases (and indeed, GCC often does, when it says 'is used uninitialized' rather than 'may be used uninitialized').<br> </div> Fri, 04 Jan 2013 23:24:55 +0000 Removing uninitialized_var() https://lwn.net/Articles/531466/ https://lwn.net/Articles/531466/ heijo <div class="FormattedComment"> It's impossible (<a rel="nofollow" href="http://en.wikipedia.org/wiki/Rice_theorem">http://en.wikipedia.org/wiki/Rice_theorem</a>).<br> <p> </div> Fri, 04 Jan 2013 12:05:31 +0000 Removing uninitialized_var() https://lwn.net/Articles/531451/ https://lwn.net/Articles/531451/ mlopezibanez <div class="FormattedComment"> There is indeed a limit to what GCC can guess correctly, but there are also quite a number of bugs and deficiencies in the warning machinery. See <a rel="nofollow" href="http://gcc.gnu.org/wiki/Better_Uninitialized_Warnings">http://gcc.gnu.org/wiki/Better_Uninitialized_Warnings</a> for a list of problems (the page may be slightly outdated by now) and <a rel="nofollow" href="http://gcc.gnu.org/PR24639">http://gcc.gnu.org/PR24639</a> Solving these problems would require substantial work. Unfortunately, there is no enough people working on GCC to even start such work in the near future.<br> <p> I wonder if kernel devs would be less frustrated with gcc if they tried to fix GCC bugs rather than work-around them. Well, perhaps they will become even more frustrated. ;-)<br> </div> Fri, 04 Jan 2013 10:16:54 +0000 Removing uninitialized_var() https://lwn.net/Articles/530913/ https://lwn.net/Articles/530913/ stevenb <div class="FormattedComment"> That case is handled just fine, at least AFAICT:<br> <p> $ cat t.c<br> extern int foo_p (int *, int);<br> extern int foo (int, int);<br> extern void bar (void);<br> <p> int foo_p (int *x, int y)<br> {<br> int a;<br> if (*x) a = y;<br> bar ();<br> if (*x) return a;<br> bar ();<br> return -1;<br> }<br> <p> int foo (int x, int y)<br> {<br> int a;<br> if (x) a = y;<br> bar ();<br> if (x) return a;<br> bar ();<br> return -1;<br> }<br> $ <br> $ gcc-4.4 -S -O2 -W -Wall -Wextra t.c<br> t.c: In function ‘foo_p’:<br> t.c:7: warning: ‘a’ may be used uninitialized in this function<br> $ <br> $ gcc-4.6 -S -O2 -W -Wall -Wextra t.c<br> t.c: In function ‘foo_p’:<br> t.c:7:7: warning: ‘a’ may be used uninitialized in this function [-Wuninitialized]<br> $ <br> $ ./xgcc -B. -S -O2 -W -Wall -Wextra t.c --version<br> xgcc (GCC) 4.8.0 20121226 (experimental) [trunk revision 194725]<br> Copyright (C) 2012 Free Software Foundation, Inc.<br> This is free software; see the source for copying conditions. There is NO<br> warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.<br> <p> $ ./xgcc -B. -S -O2 -W -Wall -Wextra t.c <br> t.c: In function 'foo_p':<br> t.c:7:7: warning: 'a' may be used uninitialized in this function [-Wmaybe-uninitialized]<br> int a;<br> ^<br> $ <br> <p> <p> In foo_p, the compiler cannot know whether bar will change the value at *x and the warning is valid. In foo, which is your example, there is no warning. In general, there will be no warning if the compiler can prove that the code in your "/* later */" doesn't change the result of the tested conditional. This is implemented in tree-ssa-uninit.c.<br> <p> Still, it's true that there will be false positives, or missed warnings. Proving whether a variable is used uninitialized is not an easy problem.<br> <p> </div> Fri, 28 Dec 2012 20:38:40 +0000 Removing uninitialized_var() https://lwn.net/Articles/530413/ https://lwn.net/Articles/530413/ apoelstra <div class="FormattedComment"> <font class="QuotedText">&gt; My point is that, as easy as it is to confuse gcc or clang, confusong the programmer who has to read the code after you is even easier!</font><br> <p> This is not always true. For a small-enough function (or small-enough block), a programmer can probably build a mental parse tree just as well as the compiler can -- and then, because he is a human gifted with all manner of high-level knowledge and context, he can easily see things that the compiler might miss.<br> <p> And in my experience, "small enough" includes many non-trivial functions of 5-10 lines. Also in my experience, these are the sorts of functions that trigger uninitialized variable warnings.<br> </div> Fri, 21 Dec 2012 23:13:30 +0000 Removing uninitialized_var() https://lwn.net/Articles/530406/ https://lwn.net/Articles/530406/ hummassa <div class="FormattedComment"> My point is that, as easy as it is to confuse gcc or clang, confusong the programmer who has to read the code after you is even easier! Let is substitute your question #2 for "did you ever have had eclampsia?" And now the comparison is fair. 50 percent of male respondents will not know what it is and many will answer wrongly.<br> </div> Fri, 21 Dec 2012 21:22:27 +0000 Removing uninitialized_var() https://lwn.net/Articles/530398/ https://lwn.net/Articles/530398/ giraffedata As the article points out, it's amazingly easy to confuse the compiler. In the cases where I've had to deal with this, there was clearly no practical alternative way to write the code. The only practical ways to shut the compiler up were to give a variable a fake value or disable the warning class. <p> I do agree that code with variables that are sometimes meaningless is harder to read than code without. I don't agree that when such meaningless variables exist, the code is equally hard to read when you assign values to them. <p> Here's the analogy: You're filling in an online medical history form. Question 1: sex. Question 2: if female, when was your last menstruation? No doctor will be the least bit concerned when he sees a man leave Question 2 blank. He might be confused if he sees a date in there for a man. In fact, he would be well justified in thinking Question 1 might be an error in that case. GCC is the automatic field checker that notices Question 2 is blank and makes the man put a date in there before it will accept the form, because it isn't smart enough to know what menstruation is and simply insists that every field be filled in, to avoid accidental omissions. Fri, 21 Dec 2012 20:14:35 +0000 Removing uninitialized_var() https://lwn.net/Articles/530389/ https://lwn.net/Articles/530389/ bronson <div class="FormattedComment"> Yes, well said! That's worth hanging on the wall.<br> </div> Fri, 21 Dec 2012 17:58:07 +0000 Removing uninitialized_var() https://lwn.net/Articles/530312/ https://lwn.net/Articles/530312/ hummassa <div class="FormattedComment"> My response was going to be similar; "if you managed to confuse the compiler, you lost your human readers a long time ago."<br> </div> Fri, 21 Dec 2012 09:02:40 +0000 Removing uninitialized_var() https://lwn.net/Articles/530294/ https://lwn.net/Articles/530294/ dvdeug <div class="FormattedComment"> Anytime this comes up, the code is hard to read. Variables that don't have valid values in chunks of code are problematic, and GCC wouldn't issue this error in a case where there's no branching or other stuff to confuse the issue. If you can't rearrange it so that it's clear to GCC that it's set before used, whether you initialize it isn't going to make it significantly more unclear to readers.<br> </div> Fri, 21 Dec 2012 05:32:57 +0000 Removing uninitialized_var() https://lwn.net/Articles/530301/ https://lwn.net/Articles/530301/ nevets <div class="FormattedComment"> No, NULL may be better than uninitialized_var(), but it is not better than hiding gcc from warning about it.<br> <p> Yes, a NULL pointer is easy to debug after a crash, but if it requires a tight race to get to a point where NULL will crash, that means you wont detect the bug until the crash happens. If that crash happens while on a production system, it's still a major issue.<br> <p> My point is that uninitialized_var() isn't very good because it may hide bugs, but so is blindly initializing something, even with NULL. It's still hiding a bug.<br> </div> Fri, 21 Dec 2012 05:29:28 +0000 Removing uninitialized_var() https://lwn.net/Articles/530274/ https://lwn.net/Articles/530274/ giraffedata The reason I don't initialize a variable to avoid the false positive warning is that it makes the code harder to read. It suggests that the variable's value is meaningful in places where it isn't. That requires more time and short term memory for the reader to understand the code. <p> I just disable the warning (by compiler option). I do appreciate it finding my used-before-set variables, but it isn't worth the false positives or dirtying of the code. Fri, 21 Dec 2012 02:22:47 +0000 Removing uninitialized_var() https://lwn.net/Articles/530208/ https://lwn.net/Articles/530208/ zlynx <div class="FormattedComment"> I recently read something discussing common threading pitfalls and code similar to yours came up.<br> <p> It is kind of amazing how many non-intuitive optimizations can be made on code like that.<br> <p> For example, it might be rewritten to something like:<br> <p> initialize a<br> do stuff<br> use a<br> if (!foo)<br> undo using a<br> <p> or rewritten into two function blocks, one for if (foo) and one for if (!foo).<br> <p> or the initialization of a might be moved down into the other if (foo) block.<br> <p> So anyway, it is entirely possible that after GCC swizzles the code around it cannot tell anymore. It might have actually used a without even looking at foo, intending to undo it later.<br> </div> Thu, 20 Dec 2012 19:38:27 +0000 Removing uninitialized_var() https://lwn.net/Articles/530129/ https://lwn.net/Articles/530129/ nix <div class="FormattedComment"> It can, of course, never be right in all circumstances (that would require a solution to the halting problem). It can sometimes be sure, and says as much ('is used uninitialized'). Often, it's not sure. What's odious is that there are lots of apparently simple cases that GCC can't yet handle, e.g.<br> <p> if (foo)<br> /* initialize a */<br> <p> /* later */<br> <p> if (foo) /* value not affected by code above */<br> /* use a */<br> <p> is a particularly notorious case.<br> </div> Thu, 20 Dec 2012 15:03:10 +0000 Removing uninitialized_var() https://lwn.net/Articles/530123/ https://lwn.net/Articles/530123/ kpfleming <div class="FormattedComment"> The primarily complication is that developers (and users) use widely varying versions of GCC. Over time, of course, GCC gets better and better at avoiding false warnings, and developers tend to use the latest-and-greatest version on their systems. Users, though, like to stick with long-term-support distributions that have older compiler versions (some still using GCC 4.3.x), but also want their software to build without warnings. When the user reports a bug about a compiler warning, the developer frequently responds that it doesn't happen with the latest GCC, and wants to close it.<br> <p> Solving the user's problem ends up requiring ugly workarounds that change the code (typically in non-functional ways) just to silence compiler warnings. Given that, it's quite reasonable to classify a compiler warning that is generated from code that cannot be trusted to have exhaustive proof of the potential failure as a 'bug' in the compiler.<br> </div> Thu, 20 Dec 2012 14:48:52 +0000 Removing uninitialized_var() https://lwn.net/Articles/530112/ https://lwn.net/Articles/530112/ clugstj <div class="FormattedComment"> NULL is better than "anything else". The reason to set a variable to a consistent invalid value is that then the code fails consistently. Consistent bugs are much easier to track down.<br> </div> Thu, 20 Dec 2012 14:18:07 +0000 Removing uninitialized_var() https://lwn.net/Articles/530089/ https://lwn.net/Articles/530089/ mjthayer <div class="FormattedComment"> I wonder whether it is quite fair to the gcc people to call this a "compiler [bug] (*stupid* gcc behavior)"? Not that I know very much about compiler internals, but instinctively I wouldn't expect getting this sort of warning right to be an entirely trivial task.<br> </div> Thu, 20 Dec 2012 12:11:28 +0000 Removing uninitialized_var() https://lwn.net/Articles/530037/ https://lwn.net/Articles/530037/ error27 <div class="FormattedComment"> I've always thought uninitialize_var() macro was a bit ugly to look at.<br> <p> The problem with setting pointers to NULL is that it silences the GCC warning, but it now triggers a "dereferencing NULL" warning in Smatch. Uninitialize_var() solved this problem nicely. I wish there were some macro that Smatch could understand.<br> <p> struct foo *p = SILENCE_GCC;<br> <p> <p> </div> Thu, 20 Dec 2012 06:50:32 +0000 Removing uninitialized_var() https://lwn.net/Articles/530013/ https://lwn.net/Articles/530013/ nevets <div class="FormattedComment"> I remember when uninitialized_var() was introduced. I actually liked the idea. IIRC, the idea was you could turn it off in one place and recheck all the locations that it was used. Even though I liked it, I always avoided using it. I rather be reminded of things that may be uninitialized.<br> <p> My experience was that, depending on which version of gcc you used, it may or may not complain about a possible uninitialized var. I had some code that gcc 4.6+ had no issue with, but 4.5.x did. I would constantly get a patch to initialize the variable (a pointer) to NULL. I refused each patch simply because 4.6 didn't complain, and more importantly, if the variable was used outside of the expected path, a NULL pointer would crash it. If I later changed the code where the variable was used without the proper initialization, the NULL was no better than anything else, and it would hide the bug just as much as uninitialized_var() would.<br> <p> I'm a bit weary of default initialization. You need to look at the code to determine if what you initialized the variable to is any better than it being random.<br> </div> Thu, 20 Dec 2012 02:47:12 +0000