coreutils
[Top][All Lists]
Advanced

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

Re: error: jump skips variable initialization


From: Bruno Haible
Subject: Re: error: jump skips variable initialization
Date: Mon, 25 Jun 2018 21:07:26 +0200
User-agent: KMail/5.1.3 (Linux/4.4.0-128-generic; KDE/5.18.0; x86_64; ; )

Paul Eggert wrote:
> the warning is not emitted if the program simply does this instead:
> 
>     int x; x = 10;
> 

Indeed: While
===========================================================
extern void bar (int);
int foo (int a)
{
  if (a == 1) goto one;
  int b = 4;
  bar (b);
 one:
  return a;
}
===========================================================
produces a warning,

===========================================================
extern void bar (int);
int foo (int a)
{
  if (a == 1) goto one;
  int b; b = 4;
  bar (b);
 one:
  return a;
}
===========================================================
does not.

> That is, although it's natural when seeing this line:
> 
>     int x = 10;
> 
> to assume that X must always be initialized, this assumption is incorrect if 
> a 
> goto jumps over the declaration.

If this was the purpose, why do we get a warning in my first example, where
'b' is not used after label 'one:'?

And when 'b' is used after the label: Tim's example
===========================================================
extern int bar (int);
int foo (int a)
{
  if (a == 1) goto one;
  int b = bar (a);
 one:
  return b;
}
===========================================================
produces a warning.

But this variation
===========================================================
extern int bar (int);
int foo (int a)
{
  if (a == 1) goto one;
  int b; b = bar (a);
 one:
  return b;
}
===========================================================
gives no warning. THIS IS THE DANGEROUS CASE. To get a warning on
this one, use -O -Wmaybe-uninitialized.

Jim Meyering wrote:
> Didn't this warning or one very much like it catch real bugs, e.g.,
> where the initialization would be omitted, and the result would be to
> use an uninitialized variable somewhere after the jumped-to label?

The warning options that catches real bugs is -Wuninitialized
-Wmaybe-uninitialized (part of -Wall) in combination with -O.

I conclude that the only point of this warning is to help people write code
that can be compiled with a C++ compiler. Which is why it is included in
-Wc++-compat.

Paul Eggert wrote:
> so it does seem a bit ridiculous to enable the warning, and it'd be fine with 
> me 
> if we removed it from manywarnings.

OK, done as follows:


2018-06-25  Bruno Haible  <address@hidden>

        manywarnings: Don't enable -Wjump-misses-init warnings by default.
        * build-aux/gcc-warning.spec: Add -Wjump-misses-init.
        * m4/manywarnings.m4 (gl_MANYWARN_ALL_GCC(C)): Remove
        -Wjump-misses-init.

diff --git a/build-aux/gcc-warning.spec b/build-aux/gcc-warning.spec
index e2625ea..c47298e 100644
--- a/build-aux/gcc-warning.spec
+++ b/build-aux/gcc-warning.spec
@@ -18,7 +18,7 @@
 -Warray-bounds=<0,2>                   handled specially by gl_MANYWARN_ALL_GCC
 -Warray-temporaries                    fortran
 -Wassign-intercept                     objc/objc++
--Wc++-compat                           FIXME maybe? borderline.  some will 
want this
+-Wc++-compat                           only useful for code meant to be 
compiled by a C++ compiler
 -Wc++0x-compat                         c++
 -Wc++11-compat                         c++
 -Wc++14-compat                         c++
@@ -65,6 +65,7 @@
 -Wintrinsic-shadow                     fortran
 -Wintrinsics-std                       fortran
 -Winvalid-offsetof                     c++ and objc++
+-Wjump-misses-init                     only useful for code meant to be 
compiled by a C++ compiler
 -Wlarger-than-                         gcc --help=warnings artifact
 -Wlarger-than=<number>                 FIXME: choose something sane?
 -Wline-truncation                      fortran
diff --git a/m4/manywarnings.m4 b/m4/manywarnings.m4
index 350c135..925c40e 100644
--- a/m4/manywarnings.m4
+++ b/m4/manywarnings.m4
@@ -1,4 +1,4 @@
-# manywarnings.m4 serial 14
+# manywarnings.m4 serial 15
 dnl Copyright (C) 2008-2018 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -108,7 +108,7 @@ m4_defun([gl_MANYWARN_ALL_GCC(C)],
   # comm -3 \
   #  <((sed -n 's/^  *\(-[^ 0-9][^ ]*\) .*/\1/p' manywarnings.m4; \
   #     awk '/^[^#]/ {print $1}' ../build-aux/gcc-warning.spec) | sort) \
-  #  <(gcc --help=warnings | sed -n 's/^  \(-[^ ]*\) .*/\1/p' | sort)
+  #  <(LC_ALL=C gcc --help=warnings | sed -n 's/^  \(-[^ ]*\) .*/\1/p' | sort)
 
   gl_manywarn_set=
   for gl_manywarn_item in -fno-common \
@@ -174,7 +174,6 @@ m4_defun([gl_MANYWARN_ALL_GCC(C)],
     -Wint-to-pointer-cast \
     -Winvalid-memory-model \
     -Winvalid-pch \
-    -Wjump-misses-init \
     -Wlogical-not-parentheses \
     -Wlogical-op \
     -Wmain \




reply via email to

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