Re: [PATCH] driver core: faux: fix Undefined Behavior in faux_device_destroy()
From: Marc Herbert
Date: Mon Jun 30 2025 - 19:25:05 EST
On 2025-06-25 17:55, Kent Overstreet wrote:
> the big con:
> - they interact badly with gotos, you can get undefined behaviour from
> using a variable that wasn't actually defined _and the compiler will
> not warn you_
> [...]
> But the issue with gotos is worth highlighting. Be careful when using
> them in code that hasn't been converted to __cleanup.
Thanks Kent for sharing this.
I got curious and found that clang -Wall is actually able to warn,
at least in simple cases:
int goto_uninitialized_C99(int *ptr)
{
if (!ptr)
goto cleanup;
const int i = 42;
cleanup:
// clang warning, no gcc warning
printf("fin: i=%d\n", i);
warning: variable 'i' is used uninitialized whenever 'if' condition
is true [-Wsometimes-uninitialized]
gcc -Wall -Wextra does not say anything.
Tested with clang version 18.1.3 and gcc 13.3.0
Interestingly, there is no warning difference between C89 and C99 code
for such a simple example. gcc warns for neither C89 code nor C99 code
and clang warns for both.
int goto_uninitialized_C89(int *ptr)
{
int i;
if (!ptr)
goto cleanup;
i = 42
cleanup:
/* clang warning, no gcc warning */
printf("fin: i=%d\n", i);
(finally getting rid of gotos is one of the main purposes of RAII)