[gnu.g++.bug] BUG in GCC & G++ 1.32 using -O & inline & narrowing implicit casts

rfg@MCC.COM (Ron Guilmette) (02/01/89)

The following short program prints FAILED when compiled using either
GCC 1.32 or G++ 1.32.0 and (only) when the -O option is used.  This
occurs on a Sun3 with the following links:

	tm-sun3.h	=>	tm.h
	xm-m68k.h	=>	config.h
	m68k.md		=>	md
	output-m68k.c	=>	aux-output.c

I believe that this program also illustrates the need for warnings
(in both G++ and GCC) whenever a value of a given width is implicitly
cast to a value of a smaller width (e.g. int to short int).  Such
implicit casts can cause a loss of information, and they should be
flagged as dangerous via warnings.  It appears that even with -Wall,
such situations are not being flagged with warnings.

// Ron Guilmette  -  MCC  -  Experimental (parallel) Systems Kit Project
// 3500 West Balcones Center Drive,  Austin, TX  78759  -  (512)338-3740
// ARPA: rfg@mcc.com
// UUCP: {rutgers,uunet,gatech,ames,pyramid}!cs.utexas.edu!pp!rfg

/* ------------------------------------------------------------------------- */
/*
Description - check that when an int value is assigned to a short int,
		the proper half of the int (i.e. the low order half) ends
		up in the short.

		This fails with 1.32.0 if -O is used and f1() is inline.

		Workaround - declare "f1_arg" as type "short int".
*/

short int v2;

int v1 = 0x11117777;

inline void f1 (int f1_arg)
{
	v2 = f1_arg;
}

void test()
{
	f1 (v1);
	if (v2 != 0x00007777) {
		printf ("v2 = 0x%08x\n", v2);
		printf ("FAILED - assignment of int to short does not work right\n");
		exit (0);
	}
}

main()
{
	test ();
	printf ("PASSED\n");
	exit (0);
}

paul@UUNET.UU.NET (Paul Hudson) (02/01/89)

The warning that Ron Guilmette wants (implicit narrowing) is on my list to
do, so if the FSF aren't going to do it, and I find time ;-) it will appear
eventually.

As before, suggestions of futher warnings welcome.
Paul Hudson 

Snail mail: Monotype ADG	Email:	...!ukc!acorn!moncam!paul
	    Science Park,		paul@moncam.co.uk
	    Milton Road,	"Sun Microsysytems:
	    Cambridge,		 The Company is Arrogant (TM)"
	    CB4 4FQ

schmidt@crimee.ics.uci.edu (Doug Schmidt) (02/03/89)

In article <8902010855.AA06237@marvin.moncam.uucp> mcvax!moncam!paul@UUNET.UU.NET (Paul Hudson) writes:
++
++As before, suggestions of futher warnings welcome.
++Paul Hudson 
++

Here's a useful warning we could use in gcc and g++, given that it is now
possible to declare ``const'' functions (read gcc.texinfo for gcc 1.33
for details).

Imagine the following perverse program:

----------------------------------------
int external = 10;

int bar () {
   external++;        
}

const int foo (int *i) {
   bar();
   return (external = *i = 10);
}

void main () {
   external = foo(&external) + external;
}
----------------------------------------

Function foo violates just about all the restrictions on const
functions, since it references global variables, changes its
parameters, and calls a non-const function.  However, gcc doesn't
complain about this, yet.  It would be nice to have a -W option that
detected these transgressions.

Doug
--
schmidt@ics.uci.edu (ARPA) |   Per me si va nella citta' dolente.
office: (714) 856-4043     |   Per me si va nell'eterno dolore.
                           |   Per me si va tra la perduta gente.
                           |   Lasciate ogni speranza o voi ch'entrate.