coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: error: jump skips variable initialization


From: Tim Rühsen
Subject: Re: error: jump skips variable initialization
Date: Mon, 25 Jun 2018 09:29:52 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.8.0

On 06/25/2018 02:00 AM, Bruno Haible wrote:
> Hi Jim, Paul,
> 
>> parse-datetime.y: In function 'parse_datetime2':
>> parse-datetime.y:1791:19: error: jump skips variable initialization \
>>   [-Werror=jump-misses-init]
> 
> First this occurred in af_alg.c with pretty standard Linux kernel style
> with gotos. [1] Now here.
> 
> What is the point of this warning?
> 
> I've read [2][3].
> 
> Test case:
> ===========================================================
> extern void bar (int);
> int foo (int a)
> {
>   if (a == 1) goto one;
>   int b = 4;
>   bar (b);
>  one:
>   return a;
> }
> ===========================================================
> 
> $ gcc-version 8.1.0 -Wall -Wjump-misses-init -c foo.c
> foo.c: In function 'foo':
> foo.c:4:15: warning: jump skips variable initialization [-Wjump-misses-init]
>    if (a == 1) goto one;
>                ^~~~
> foo.c:7:2: note: label 'one' defined here
>   one:
>   ^~~
> foo.c:5:7: note: 'b' declared here
>    int b = 4;
>        ^
> 
> In C++ such a warning makes sense, for types that have a destructor,
> because the destructor of 'b' would run even through its constructor
> has not run.
> 
> But here
>   1. The variable 'b' is of type 'int', which has a trivial destructor.
>   2. We are in C, no C++, where ISO C types don't have destructors anyway.
>      (And AFAICS there are no GCC extensions to C that would provide
>      destructors either.)
> 
> Is the point to help people reduce the scope of the variables?
> ===========================================================
> extern void bar (int);
> int foo (int a)
> {
>   if (a == 1) goto one;
>   {
>     int b = 4;
>     bar (b);
>   }
>  one:
>   return a;
> }
> ===========================================================
> 
> Is the point to force people to hoist the variable declarations
> at the top of the function (back to the BSD style of the 1980ies),
> like Paul did in [1]?
> ===========================================================
> extern void bar (int);
> int foo (int a)
> {
>   int b;
>   if (a == 1) goto one;
>   b = 4;
>   bar (b);
>  one:
>   return a;
> }
> ===========================================================
> 
> Is the point to help people write code that can be compiled with a C++ 
> compiler?
> If so, then there's no reason for it to be enabled on gnulib code.
> 
> Is the point merely "the standard says that a warning is possible, so let's
> implement it"?
> 
> Can someone answer this?

Your (first) example shouldn't trigger the warning. But this should:
===========================================================
extern int bar (int);
int foo (int a)
{
  if (a == 1) goto one;
  int b = bar (a);
 one:
  return b;
}
===========================================================

I can only assume that triggering the warning for your example is a side
effect. It might be worth a report.

BTW, I have been effected by a bug like in my example and at that time
opened a request for such a warning flag :-)

Regards, Tim

Attachment: signature.asc
Description: OpenPGP digital signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]