HOWTO: Get Warning on implicit type casts that may lead to data loss

E

elliot.li.tech

Hello everyone,

I'm migrating a C++ program from 32-bit systems to 64-bit systems. The
old programmers used some annoying assignments like assigning "long"
to "int", which may lead to data loss.

Is it possible to let the compiler (specifically, GCC) spew some
warnings on this kind of implicit type casts?

Or you suggest use some other static source codes analysis tools?

Thanks in advance!
 
G

Guest

Hello everyone,

I'm migrating a C++ program from 32-bit systems to 64-bit systems. The
old programmers used some annoying assignments like assigning "long"
to "int", which may lead to data loss.

Is it possible to let the compiler (specifically, GCC) spew some
warnings on this kind of implicit type casts?

Or you suggest use some other static source codes analysis tools?

Any good compiler should warn when there is a risk of data loss, have
you turned up the warning levels? Good flags to always include when
compiling with gcc are -Wall and -std=c++98, there are probably more but
those are the ones I can remember right now.
 
R

Rolf Magnus

There is no such thing as an implicit type cast. It's an implicit
conversion.
Any good compiler should warn when there is a risk of data loss, have
you turned up the warning levels? Good flags to always include when
compiling with gcc are -Wall and -std=c++98, there are probably more but
those are the ones I can remember right now.

I would suggest to also add -pedantic and -Wextra.
 
E

elliot.li.tech

Thanks Erik and Rolf!

There is no such thing as an implicit type cast. It's an implicit
conversion.

OK, thanks!
I would suggest to also add -pedantic and -Wextra.

I've tried "-pedantic -Wall -Wextra -std=c++98" but see no warning
about such kind of error. Just tried a little program like this:

#include <iostream>

using namespace std;
int main ()
{
long l;
int i;

cout << "input a long number: ";
cin >> l;
i = l;
cout << "the int number is: " << i << endl;

return 0;
}
 
E

elliot.li.tech

I've tried options "-m64 -pedantic -Wall -Wextra -std=c++98" on both
x86-64 and PPC64, neither issued a single waring on assigning a "long"
to "int."
 
R

Rolf Magnus

I've tried options "-m64 -pedantic -Wall -Wextra -std=c++98" on both
x86-64 and PPC64, neither issued a single waring on assigning a "long"
to "int."

You are right. I could have sworn that there is a warning for that, but I
looked into the manual and didn't find any option that gets me such a
warning. You could ask in gnu.g++.help, but it seems to me there is no
warning for that.
 
J

James Kanze

You are right. I could have sworn that there is a warning for that, but I
looked into the manual and didn't find any option that gets me such a
warning. You could ask in gnu.g++.help, but it seems to me there is no
warning for that.

The problem is that it would also warn on things like:

char* p ;
int ch = getchar() ;
if ( ch != EOF ) {
*p ++ = ch ;
// ...
}

or even:

short a, b, c ;
a = b + c ;

The first is an extremely common idiom in C, and is probably
used in quite a lot of C++ code as well. The second will occur
anytime arithmetic is done on short or character types, because
of integral promotion.

The utility of the warning may be less that you might think,
too. An expression like:

int a, b, c ;
a = b + c ;

is probably even more common, and has the same risks.
 
K

karpov2007

Hello everyone,

I'm migrating a C++ program from 32-bit systems to 64-bit systems. The
old programmers used some annoying assignments like assigning "long"
to "int", which may lead to data loss.

Is it possible to let the compiler (specifically, GCC) spew some
warnings on this kind of implicit type casts?

Or you suggest use some other static source codes analysis tools?

Thanks in advance!

Viva64 tool can find dangerous implicit and explicit type conversion.
Viva64 is the lint-like tool for searching of errors and possible
problems in C/C++ code while porting the application to the 64-bit
platform.
http://www.viva64.com/viva64.php
 
E

elliot.li.tech

You are right. I could have sworn that there is a warning for that, but I
looked into the manual and didn't find any option that gets me such a
warning. You could ask in gnu.g++.help, but it seems to me there is no
warning for that.

Thanks, I'll ask this question over there. I've read through GCC's
man page and info docs, but found nothing regards this neither.
 
E

elliot.li.tech

Viva64 tool can find dangerous implicit and explicit type conversion.
Viva64 is the lint-like tool for searching of errors and possible
problems in C/C++ code while porting the application to the 64-bit
platform.http://www.viva64.com/viva64.php

Thanks Karpov! It seems a good tool. But it's hard for us to adopt non-
FLOSS software in our development cycle currently. Do you have any
other suggestions?
 
E

elliot.li.tech

Hi James,

Thanks for you reply.

The problem is that it would also warn on things like:

char* p ;
int ch = getchar() ;
if ( ch != EOF ) {
*p ++ = ch ;
// ...
}

or even:

short a, b, c ;
a = b + c ;

The first is an extremely common idiom in C, and is probably
used in quite a lot of C++ code as well. The second will occur
anytime arithmetic is done on short or character types, because
of integral promotion.

Yes, I agree with you on this. These scenarios shouldn't trigger
warnings in _default_ compiler warning level. So my question should be
about 64-bit porting only, ie. it would be better for me if GCC
supported some thing like -Wporting that includes porting related
warnings, such as this assign-long-to-int thing.

The utility of the warning may be less that you might think,
too. An expression like:

int a, b, c ;
a = b + c ;

is probably even more common, and has the same risks.

I think overflow-checking and warning-on-implicit-conversion are
different games. Not checking for overflow is a good of C/C++. If we
need it, we can just write our own Int and overload the operator+() to
do the checking. And also it would be good if the compiler supports
warnings on porting specific errors.
 
G

Greg Herlihy

Hello everyone,

I'm migrating a C++ program from 32-bit systems to 64-bit systems. The
old programmers used some annoying assignments like assigning "long"
to "int", which may lead to data loss.

Is it possible to let the compiler (specifically, GCC) spew some
warnings on this kind of implicit type casts?

Yes, try the:

-Wshorten-64-to-32

gcc compiler flag to enable warnings about 64- to 32-bit implicit type
conversions that involve integer types.

Greg
 
J

James Kanze

Yes, I agree with you on this. These scenarios shouldn't trigger
warnings in _default_ compiler warning level. So my question should be
about 64-bit porting only, ie. it would be better for me if GCC
supported some thing like -Wporting that includes porting related
warnings, such as this assign-long-to-int thing.

That's a special case. Sun CC generates a number of warnings in
64 bit mode, when it thinks that the semantics of the program
might be different than they would be in 32 bit mode. I've not
found them very useful, however.
I think overflow-checking and warning-on-implicit-conversion are
different games. Not checking for overflow is a good of C/C++.

Allowing unspecified behavior is good? How can that be?
If we need it, we can just write our own Int and overload the
operator+() to do the checking.

At a very, very high runtime cost; the checking is expensive.
If the compiler did it, all it needs to do is check the overflow
status flag after the addition. Very, very cheap.

To be really useful, of course, you'd want some sort of global
optimization. If I'm doing something like:

int d1 = rand() % 6 + 1 ;
int d2 = rand() % 6 + 1 ;
int throw = d1 + d2 ;

overflow checking on that final addition really isn't necessary.
A good compiler should be able to recognize this, and suppress
it. And of course, you'd also want to suppress it if you had
something like:

int d1 = throwDice() ; // post-condition: return value in
int d2 = throwDice() ; // range [1..6].
int throw = d1 + d2 ;

Which means the compiler must do inter-module analysis.

Alternatively, a pragma could be used to turn it off locally, if
the profiler showed it necessary.
And also it would be good if the compiler supports warnings on
porting specific errors.

You mean, for example, any time you suppose an int is more than
16 bits? Or your code depends on it being 2's complement?

Sun CC supports warnings for porting from 32 bit to 64 bit,
because the compiler was 32 bit (and only 32 bit) for the
longest time. Their warnings are only relevant to users of the
older compilers, when porting their code to the newer one. They
concern customers of a specific compiler; they are not designed
to be general portability warnings. I'm not sure what should be
included in general portability warnings; most programs really
don't have to be 100% portable (e.g. to a 1's complement 36 bit
machine).
 
E

elliot.li.tech

Yes, try the:

-Wshorten-64-to-32

gcc compiler flag to enable warnings about 64- to 32-bit implicit type
conversions that involve integer types.

Oh thanks Greg! This seems to be exactly what I wanted. An
undocumented feature? I tried it on a PPC64 machine (with RHEL5) but
with no luck:

$ g++ -m64 -Wall -Wshorten-64-to-32 main.cpp
cc1plus: error: unrecognized command line option "-Wshorten-64-to-32"
$ g++ --version
g++ (GCC) 4.1.1 20070105 (Red Hat 4.1.1-52)

This option is not in my GCC's man page. I Googled for this option but
see no useful information suggest the version of GCC to use. Or is
this a GCC-on-Mac-only extra? Any help? Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top