This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Request for a C++ warning for undefined behaviour
- From: skaller <skaller at ozemail dot com dot au>
- To: Igor Bukanov <igor at fastmail dot fm>
- Cc: gcc at gcc dot gnu dot org
- Date: 13 Aug 2003 21:27:31 +1000
- Subject: Re: Request for a C++ warning for undefined behaviour
- Organization:
- References: <3F3A08E1.1000200@fastmail.fm>
- Reply-to: skaller at ozemail dot com dot au
On Wed, 2003-08-13 at 19:46, Igor Bukanov wrote:
> It would also be nice if GCC would warn about the following code:
>
> int x = x + 1;
This is also difficult to detect. In particular,
the following code is well defined:
void *p = (void*)&p; // p contains its own address
unsigned char x = x - x; // x is 0 afterwards
Unfortunately in C++ there are bad things called references
which take addresses silently. So for example:
int f(int&);
int x = f(x);
may or may not be using the contents of the uninitialised value
of x (dependeding on the definition of f).
In particular, there is not necessarily any undefined behaviour
in the following code:
T x = x + 1;
since T::operator+ is an overloaded function. All of which
means that a simple syntactically based detection could only
work in very limited cases (such as where the type was known
to be a built in type).
However, use of uninitialised variables *might* drop
out of a data flow analysis in some optimisation pass,
where it is possible to be a bit more certain that there
really is an error, and to detect cases that would be
much more difficult for a human to spot (due to complexity).