473,698 Members | 2,588 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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!

Nov 11 '07
12 3636
Hi James,

Thanks for you reply.

On Nov 12, 5:35 pm, James Kanze <james.ka...@gm ail.comwrote:
On Nov 12, 5:53 am, Rolf Magnus <ramag...@t-online.dewrote:
elliot.li.t...@ gmail.com wrote:
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.

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.
Dec 3 '07 #11
On Nov 11, 6:39 am, elliot.li.t...@ gmail.com wrote:
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

Dec 3 '07 #12
On Dec 3, 3:59 am, elliot.li.t...@ gmail.com wrote:
On Nov 12, 5:35 pm, James Kanze <james.ka...@gm ail.comwrote:
On Nov 12, 5:53 am, Rolf Magnus <ramag...@t-online.dewrote:
elliot.li.t...@ gmail.com wrote:
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.
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.
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.
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++.
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).

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Dec 3 '07 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3845
by: Logan | last post by:
Several people asked me for the following HOWTO, so I decided to post it here (though it is still very 'alpha' and might contain many (?) mistakes; didn't test what I wrote, but wrote it - more or less - during my own installation of Python 2.3 on Fedora Core 1 Linux for a friend of mine). Anyway, HTH, L.
11
1872
by: Johan | last post by:
Hi Can somebody explain to me why I get this warning message and how I can solve this warning message. Thanks a lot Johan In member function `void
9
7774
by: Ape Ricket | last post by:
Hi. During my program's set-up phase where it reads in the arguments it was invoked with, I programmed this: if (strcmp(argv,"-G") ==0) { geom_scaling = ON; if (i < argc-1) strcpy(geom_string,argv); }
16
5840
by: jose_luis_fdez_diaz_news | last post by:
Hi, If I don't include <libgen.h> I get then warnig below in regcmp call: warning: improper pointer/integer combination: op "=" but if I include it the warning is not shown, but them program compiles fine. What is the warning shown ?
29
2518
by: junky_fellow | last post by:
Consider the following piece of code: struct junk { int i_val; int i_val1; char c_val; }; int main(void) {
11
7610
by: Steve Gough | last post by:
Could anyone please help me to understand what is happening here? The commented line produces an error, which is what I expected given that there is no conversion defined from type double to type Test. I expected the same error from the following line, but it compiles fine. The double is silently truncated to an int and then fed in to the implicit conversion operator. Why does this happen? Is there any way that I can keep the implicit...
1
1351
by: Josué Andrade Gomes | last post by:
Take this code: 1: int main() 2: { 3: unsigned short x = { 1, 2, 3, 4, 5 }; 4: unsigned short sum = 0; 5: 6: for (int i = 0; i < 5; ++i) { 7: sum += x; 8: } 9: }
82
4601
by: robert bristow-johnson | last post by:
here is a post i put out (using Google Groups) that got dropped by google: i am using gcc as so: $ gcc -v Using built-in specs. Target: i386-redhat-linux Configured with: ../configure --prefix=/usr --mandir=/usr/share/man -- infodir=/usr/share/info --enable-shared --enable-threads=posix -- enable-checking=release --with-system-zlib --enable-__cxa_atexit --
13
9744
by: Rex Mottram | last post by:
I'm using an API which does a lot of callbacks. In classic callback style, each routine provides a void * pointer to carry user-defined data. Sometimes, however, the user-defined pointer is not needed which causes the compiler to spit out a "helpful" warning. For example: % cat unused.c #include <stdio.h> int foo(char *str, void *data) {
0
8609
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8899
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8871
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7738
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6528
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5861
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4371
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2335
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.