This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

signed/unsigned comparison warning level


Can the severity of signed/unsigned comparisons be raised, since GCC
does not properly handle the comparisons.
Every example below is false compiled with gcc 4.5.0


int main()

{
	int s = -2;
	unsigned int u = 0xFFFFFFFDU;

	if( s < u )
		printf( "okay\n" );
	else
		printf( "incorrect handling\n" ); // gets here

	{
		int i = -2;
		unsigned u = 2;
		if (i < u) {
			// Does GCC get here? no, it doesn't
			printf( "Ya, of course\n" );
		}
		else
			printf( "gcc puked\n" ); // gets here
	}

   {

	unsigned int i = -3;
	 if( i < 0 )
		 printf( "this can't happen, it's unsigned.\n" );
	 else
		printf( "this is actually a correct result here\n" ); // does get this
	}
   {

    int i = -3;
// visual studio does not warn on this one either... just the first
two comparisons
    if( i < (unsigned)0 )
		 printf( "-3 is < 0 \n" );
	 else
		printf( "-3 is more than 0?\n" ); // gets here
	}

	return 0;

}

---------------------------------------------------

I said a lot of words on
 http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/d0ec33f0-2534-48f8-9673-538d68d8ef86
to describe how this might be handled.... and even if 5% of the cases
were fixed, it would be 250% better overall.

I don't know why standards left this open, other than there isn't a
single-instruction translation from code to CPU for the comparison;

But if it's not fixed, this warning should definatly be issued at
default warning level.  This should be more like 'if this comparison
can be wrong, it will be wrong'.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]