[cfe-dev] Understand assumptions towards uninitialized variables on stack

via cfe-dev cfe-dev at lists.llvm.org
Wed Nov 21 07:03:21 PST 2018


> IMHO the inconsistency makes a lot of additional effort and perhaps is
> not preferred, but I guess that's eventually the programmer's
> responsibility to solve that? 

Correct. The program has undefined behavior, and it is the programmer's
responsibility to solve that.  The Undefined Behavior Sanitizer would
reveal the problem immediately.

> uninitialized variables on the stack is assumed to be zero due to
> some reason,

That is not exactly what happened.  The assignment is from uninitialized
memory, which will have an unknown value.  Because the value is unknown,
the assignment can be optimized to avoid a read from memory, and
substitute any convenient value, without perturbing any defined property
of the program. The most convenient value to use here is zero.

This is a different sequence of reasoning than what you suggested, which
is more like this:  The stack values are assumed to be zero, therefore 
we can use value propagation to assign the value zero instead of reading
memory with a known value.

I agree that the net effect here is the same, but the reasoning is
important for correct understanding of the program's semantics.
--paulr


More information about the cfe-dev mailing list