implicit narrowing conversion.
Adam Sulmicki
adam@cfar.umd.edu
Wed Jun 23 18:58:00 GMT 2004
> > shouldn't this code below generate warnings on "implicit narrowing
> > conversion", on:
> > 1) "i=f" assignment as well as
> > 2) on conversion on return from float to int?
>
> The desire for a better -Wconversion has been known for years. I have a
> notional specification for it, "warn for any implicit conversion that may
> change a value", as discussed at
> <http://www.srcf.ucam.org/~jsm28/gcc/#Wconversion>, arising from a
> discussion on security-audit (there having been a spate of security
> problems relating to implicit signed/unsigned conversions)
> <http://archives.neohapsis.com/archives/linux/lsap/2000-q4/0153.html>, and
> also discussed in bug 6614 and elsewhere. There are far more features
> that would be nice to have than there is implementation time, and so far
> no-one has contributed an implementation of this one.
hmm that stuff is 4 years old. In that case how about the attached patch?
It seems to work for me. It probably doesn't cover every case mentioned in
the document but I suppose it is better than waiting another 4 years.
//-----------------------------------------------
[adam@mtdew gcc]$ cat test3.c
int main() {
double f;
int i;
i = f;
return f;
}
//-----------------------------------------------
[adam@mtdew gcc]$ /usr/local/gcc/bin/gcc -Wall -W -Wconversion test3.c
test3.c: In function `main':
test3.c:6: warning: real type implicitly converted to integer type
test3.c:8: warning: real type implicitly converted to integer type
[adam@mtdew gcc]$ /usr/local/gcc/bin/gcc -Wall -W test3.c
[adam@mtdew gcc]$ /usr/local/gcc/bin/gcc --version
gcc (GCC) 3.4.0
Copyright (C) 2004 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.
//-----------------------------------------------
-------------- next part --------------
--- gcc-3.4.0/gcc/c-typeck.c-backup 2004-06-23 10:31:45.000000000 -0600
+++ gcc-3.4.0/gcc/c-typeck.c 2004-06-23 12:44:05.000000000 -0600
@@ -3455,9 +3455,12 @@
|| codel == BOOLEAN_TYPE)
&& (coder == INTEGER_TYPE || coder == REAL_TYPE
|| coder == ENUMERAL_TYPE || coder == COMPLEX_TYPE
- || coder == BOOLEAN_TYPE))
+ || coder == BOOLEAN_TYPE)) {
+ if (codel == INTEGER_TYPE && coder == REAL_TYPE)
+ if (warn_conversion)
+ warning ("real type implicitly converted to integer type");
return convert_and_check (type, rhs);
-
+ }
/* Conversion to a transparent union from its member types.
This applies only to function arguments. */
else if (codel == UNION_TYPE && TYPE_TRANSPARENT_UNION (type) && ! errtype)
More information about the Gcc
mailing list