On Thu, Aug 25, 2022 at 09:33:18AM -0700, Linus Torvalds wrote: > On Thu, Aug 25, 2022 at 8:40 AM Alexander Potapenko <glider@xxxxxxxxxx> wrote: > > > > On Mon, Jul 4, 2022 at 10:07 PM Matthew Wilcox <willy@xxxxxxxxxxxxx> wrote: > > > > > > ... wait, passing an uninitialised variable to a function *which doesn't > > > actually use it* is now UB? What genius came up with that rule? What > > > purpose does it serve? > > > > > > > There is a discussion at [1], with Segher pointing out a reason for > > this rule [2] and Linus requesting that we should be warning about the > > cases where uninitialized variables are passed by value. > > I think Matthew was actually more wondering how that UB rule came to be. > > Personally, I pretty much despise *all* cases of "undefined behavior", Let me start by saying you're not alone. But some UB *cannot* be worked around by compilers (we cannot solve the halting problem), and some is very expensive to work around (initialising huge structures is a typical example). Many (if not most) instances of undefined behaviour are unavoidable with a language like C. A very big part of this is separate compilation, that is, compiling translation units separately from each other, so that the compiler does not see all the ways that something is used when it is compiling it. There only is UB if something is *used*. > but "uninitialized argument" across a function call is one of the more > understandable ones. Allowing this essentially never allows generating better machine code, so there are no real arguments for ever allowing it, other than just inertia: uninitialised everything else is allowed just fine, and only actually *using* such data is UB. Passing it around is not! That is how everything used to work (with static data, automatic data, function parameters, the lot). But it now is clarified that passing data to a function as function argument is a use of that data by itself, even if the function will not even look at it ever. > I personally was actually surprised compilers didn't warn for "you are > using an uninitialized value" for a function call argument, because I > mentally consider function call arguments to *be* a use of a value. The function call is a use of all passed arguments. > Except when the function is inlined, and then it's all different - the > call itself goes away, and I *expect* the compiler to DTRT and not > "use" the argument except when it's used inside the inlined function. > Because hey, that's literally the whole point of inlining, and it > makes the "static checking" problem go away at least for a compiler. But UB is defined in terms of the abstract machine (like *all* of C), not in terms of the generated machine code. Typically things will work fine if they "become invisible" by inlining, but this does not make the program a correct program ever. Sorry :-( Segher