gcc3 vs 176.gcc
Joe Buck
jbuck@synopsys.COM
Thu Jan 10 11:37:00 GMT 2002
I wrote:
> > There are two ways of detecting it: based on offsets against registers
> > (which would exist only in some back ends), or based on types of pointers
> > closer to the source. The latter type of check would work on all targets.
geoffk writes:
> This sounds promising. Would anyone like to propose a patch?
First we need to settle on what we will warn about.
There's a fairly easy approach that some might consider overly aggressive
in that there will be some false positives: issue a warning when
a pointer type is cast to an incompatible (for purposes of the aliasing
rules) pointer type, (e.g. cast from int* to float*). So, something like
float* fp;
....
int* ip = (int *)fp;
would trigger the warning, even though nothing illegal has happened yet.
One possibility would be to have an optional warning that detects this.
I can't think of good wording, maybe just
warning: risky pointer cast
This feature might be valuable to people who want to audit their code
to find possible aliasing violations.
A more conservative approach might just flag the tree node corresponding
to the cast, indicating that dereferencing the pointer is problematic;
this flag would be propagated when an offset is added. We would then
issue the warning only when the pointer is dereferenced. This would give
a warning for the gcc spec test, but it would not give a warning if
the badly-cast pointer were copied to some other pointer first, as
in the above example.
There's still the potential for some false positives, as Joseph pointed
out, but only for folks using an obfuscated programming style.
More information about the Gcc
mailing list