Request for a C++ warning for undefined behaviour

skaller skaller@ozemail.com.au
Wed Aug 13 12:15:00 GMT 2003


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).




More information about the Gcc mailing list