Messages in this thread | | | Date | Tue, 14 Jun 2022 16:40:39 -0500 | From | Segher Boessenkool <> | Subject | Re: [PATCH] [RFC] Initialization of unused function parameters |
| |
Hi!
On Tue, Jun 14, 2022 at 10:19:53PM +0200, Alexander Potapenko wrote: > ================ > char *kmalloc(int size); > > char *kmalloc_or_not(int flag, int size, char *p) { > if (flag) > return kmalloc(size); > else > return p; > } > > char global[16]; > > char *p(int flag) { > char *c; > int size; > if (flag) > return kmalloc_or_not(1, 4, c); > else > return kmalloc_or_not(0, size, global); > } > ================
Since C11, lvalue conversion of an automatic variable that does not have its address taken is explicitly undefined behaviour (6.3.2.1/2). So in function "p", both where "c" and where "size" are passed causes UB (so that executing "p" always causes UB btw).
> In this example `size` is passed into kmalloc_or_not() initialized, > however it is never used, so the code probably has defined behavior.
No such luck: the passing itself already causes UB.
GCC does not warn, it has already optimised the code to what you expect by the time this warning is done. If you use -fno-inline it does warn for both "c" and "size" (via -Wmaybe-uninitialized).
But it is still UB! All bets are off, no compiler can do any correct translation of your program, since there *is none*.
Segher
| |