warning on unsafe cast

David.Carter-Hitchin carterda@ubs.com
Fri Feb 27 18:10:00 GMT 2004


Hi,

If it is specifically this error you are looking for then it seems like an
ideal job for a language like perl.  cat your source code into something
like this:

#!/bin/perl
$l = 0;
while (<>) {
   if (/[ \t]int (\w+) /)   {$ints{$1}   = $l}
   if (/[ \t]short (\w+) /) {$shorts{$1} = $l}
   if (/(\w+)=(\w+)/)       {$equiv{$1} = $2}
   $l++
}
foreach $lhs (keys %equiv) {
   $rhs = $equiv{$lhs};
   if (defined $ints{$rhs} && defined $shorts{$lhs})
      {
	print "Potential mismatch with $rhs (defined on line $ints{$rhs})\
        and $lhs (defined on line $shorts)\n";
.
.
.

You'd need to add in something to remember the line number where the
equivalence was, and you'd also need to "beef up" the regexes to recognise
all the different ways you define your vars and function headers, but you
get the idea, I hope.  Hell, you could even write this in C++ if you don't
know perl :-)  I'm just making the point that if you know the specific error
you are search for you can just build some reasonably sophisticated regex
trap to capture it.

I really like the sound of FlexeLint - but I don't like the price tag
(anyone know where to get a cheap secondhand copy? - none I can find on ebay
etc).  Is FlexeLint just a collection of "regex traps"?  I googled a little
for "free lint c++" and came up with this site:

http://www.thefreecountry.com/compilers/cpp.shtml

The only mention of "lint" there is with "TenDRA C/C++" which I have never
tried.

Cheers,
HTH,

David Carter-Hitchin,
GSD-Risk-IT, UBS Investment Bank

On Fri, 27 Feb 2004, Scott Lipcon wrote:

> Hi Eljay - thanks for the quick response.
>
> Unfortunately, the problem is that we don't know where all the
> int->short (or short->char, or int->char, etc) assignments are - we're
> dealing with well over half a million lines of code, and these
> problems have crept in over several years.   If we had any way of
> replacing all the assignments with structs or AssignIntToShort()
> function calls, we'd know where they were and would just fix them :)
>
> Of course, the problem also exists in function calls:
>
> void myfunc(short s);
>
> ...
> int i;
> ...
> myfunc(i);
>
> We're looking in to Gimpel FlexeLint already, thanks for that
> recommendation.    If you have any other suggestions I'd appreciate
> it.
>
> Thanks,
> Scott
>
>
> On Friday, 27 Feb 2004, Eljay Love-Jensen wrote:
>
> > Hi Scott,
> >
> > This will address your C issue:
> >
> > #include <limits.h>
> > #include <stdio.h>
> > int main(int argc, char *argv[]) {
> >    int i;
> >    short s;
> >
> >    i = atoi(argv[1]);
> >    assert(i >= SHRT_MIN);
> >    assert(i <= SHRT_MAX);
> >    s = i;
> >    printf("%d\n", s);
> >    return 0;
> > }
> >
> > If you need this in many places:
> >
> > #include <limits.h>
> > #include <stdio.h>
> > void ShortAssignInt(short* s, int i)
> > {
> >    assert(i >= SHRT_MIN);
> >    assert(i <= SHRT_MAX);
> >    *s = i;
> > }
> >
> > int main(int argc, char *argv[]) {
> >    int i;
> >    short s;
> >
> >    i = atoi(argv[1]);
> >    ShortAssignInt(&s, i);
> >    printf("%d\n", s);
> >    return 0;
> > }
> >
> > Another solution (more friendly in C++ than in C) is:
> >
> > #include <stdio.h>
> > struct Short { short m; };
> > struct Int { int m; }
> >
> > int main(int argc, char* argv[])
> > {
> >    Int i;
> >    Short s;
> >    i.m = atoi(argv[1]);
> >    s = i;  // Error generated.
> >    printf("%d\n", s.m);
> >    return 0;
> > }
> >
> > Another alternative would be to use Ada instead of C.
> >
> > If you want a Lint tool, I've had good luck with Gimpel's PC-lint or
> > FlexeLint.  (http://www.gimpel.com/).
> >
> > HTH,
> > --Eljay
> >
>



More information about the Gcc-help mailing list