[Bug c/17968] New: assigning size_t to int - warning needs to be flagged .
kaykaylance at yahoo dot com
gcc-bugzilla@gcc.gnu.org
Wed Oct 13 04:01:00 GMT 2004
Description:
============
If I were to assign a size_t to an int, there is a possible data loss
(overflow error) because of that conversion, especially if the value of size_t
is > INT_MAX (as defined in limits.h ), there would be an overflow error then
and the compiler would overflow to given -ve numbers then.
Example - Compilable code fragment:
===================================
#include <stdlib.h>
#include <limits.h>
#include <stdio.h>
int main() {
size_t ok;
int overflow;
ok = (size_t)INT_MAX + 1;
overflow = ok; /* Well, It would overflow here */
printf("Int: %d , Size_t: %u", overflow, ok );
system("pause");
return EXIT_SUCCESS;
}
Expected Result:
================
When compiling this code fragment, I am essentially assigning a size_t value
to an int (implicitly ). So it would be nice if the compiler flags a warning
there, indicating there is possible loss of data or an overflow error or a
similar appropriate warning message.
Actual Result:
==============
Right, now even though I compiled with the highest warning level, it is not
flagging a warning.
C:\>gcc -W -Wall -ansi -pedantic sizet_int.c
C:\>gcc -W -Wall -ansi sizet_int.c
System Information:
======================
C:\>gcc -v
Reading specs from C:/Programs/MinGW/bin/../lib/gcc/mingw32/3.4.2/specs
Configured with: ../gcc/configure --with-gcc --with-gnu-ld --with-gnu-as --host=
mingw32 --target=mingw32 --prefix=/mingw --enable-threads --disable-nls --enable
-languages=c,c++,f77,ada,objc,java --disable-win32-registry --disable-shared --e
nable-sjlj-exceptions --enable-libgcj --disable-java-awt --without-x --enable-ja
va-gc=boehm --disable-libgcj-debug --enable-interpreter --enable-hash-synchroniz
ation --enable-libstdcxx-debug
Thread model: win32
gcc version 3.4.2 (mingw-special)
System Type: Win XP - Service Pack 2 / Pentium 4 .
Let me know if I need to provide more information here.
--
Summary: assigning size_t to int - warning needs to be flagged .
Product: gcc
Version: 3.4.2
Status: UNCONFIRMED
Severity: enhancement
Priority: P3
Component: c
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: kaykaylance at yahoo dot com
CC: gcc-bugs at gcc dot gnu dot org
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=17968
More information about the Gcc-bugs
mailing list