Re: warning on unsafe cast

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

 



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
> 

[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux