This is the mail archive of the gcc-patches@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]

Patch to c-common.c (printf-format checking)


Hello,

I have made a patch that addresses a minor problem when checking 
arguments of a printf format string.

Currently arguments of a *-precision must be of type signed int, 
unsigned type arguments give a warning.
Here's an example that shows the problem (hello.c):


	# include	<stdio.h>
	# include	<stdlib.h>

	int	main(int argc, char **argv)
	{
		char	myname[64];

		memset(myname, 0, sizeof msg);
		sprintf(myname, "%.*s", sizeof msg - 1, argv[0]);

		printf("%s: hello world.\n", myname);
		return EXIT_SUCCESS;
	}


If you compile it with "gcc -Wformat -o hello hello.c" you'll
get the warning:


	hello.c:9: warning: field width is not type int (arg 3)


The following patch permits unsigned int type arguments to 
*-precision (in fact it's just a copy of the *-width checking code):


	*** gcc/c-common.c.old	Thu Sep  7 11:11:16 2000
	--- gcc/c-common.c	Wed Feb 23 10:34:17 2000
	***************
	*** 1606,1613 ****
	  		      cur_param = TREE_VALUE (params);
	  		      params = TREE_CHAIN (params);
	  		      ++arg_num;
	! 		      if (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
	  			  != integer_type_node)
	  			warning ("field width is not type int (arg %d)",
	  				 arg_num);
	  		    }
	--- 1606,1616 ----
	  		      cur_param = TREE_VALUE (params);
	  		      params = TREE_CHAIN (params);
	  		      ++arg_num;
	! 		      if ((TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
	  			  != integer_type_node)
	+ 			  &&
	+ 			  (TYPE_MAIN_VARIANT (TREE_TYPE (cur_param))
	+ 			   != unsigned_type_node))
	  			warning ("field width is not type int (arg %d)",
	  				 arg_num);
	  		    }


This is a ChangeLog of the patch:

	Wed Feb 23 10:34:17 2000  J"orn Padeken <joern.padeken@dsa-ac.de>
        * c-common.c (check_format_info): permit both signed and
	unsigned int type for *-precision argument


Feel free to contact me, if you need one of the original 
files hello.c, c-common.c, c-common.c.old or ChangeLog.


Thanks
Joern Padeken
DSA - Daten- und Systemtechnik GmbH
Joern Padeken
am Laubberg 20
38518 Gifhorn

EMail: Joern.Padeken@dsa-ac.de

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