[PATCH] Fix PR42667

Richard Guenther rguenther@suse.de
Sat Jan 9 13:59:00 GMT 2010


fold_builtin_strlen doesn't honor the return type of the strlen
builtin but assumes it is size_t - which is not correct if the
user prototyped strlen with a slightly incompatible version.
The following simply makes fold_builtin_strlen return the result
in whatever return type the builtin has.

OTOH we should probably make builtins unavailable on this kind
of incompatibilities or at least retain the correct prototype
and override the user.

Whatever.  This is enough to fix the ICE during type verification
in PRE.

Bootstrapped on x86_64-unknown-linux-gnu, testing in progress.

Thanks,
Richard.

2010-01-09  Richard Guenther  <rguenther@suse.de>

	PR middle-end/42667
	* builtins.c (fold_builtin_strlen): Add type argument and
	convert the resulting length to it.
	(fold_builtin_1): Adjust.

	* gcc.dg/torture/pr42667.c: New testcase.

Index: gcc/testsuite/gcc.dg/torture/pr42667.c
===================================================================
*** gcc/testsuite/gcc.dg/torture/pr42667.c	(revision 0)
--- gcc/testsuite/gcc.dg/torture/pr42667.c	(revision 0)
***************
*** 0 ****
--- 1,13 ----
+ /* { dg-do compile } */
+ /* { dg-options "-w" } */
+ 
+ extern int strlen(const char *);
+ void WriteTextDots(int len);
+ 
+ void OnDisplay(char * string)
+ {
+   if (!string)
+     string = "(none)";
+   WriteTextDots(strlen(string));
+ }
+ 
Index: gcc/builtins.c
===================================================================
*** gcc/builtins.c	(revision 155757)
--- gcc/builtins.c	(working copy)
*************** static rtx expand_builtin_expect (tree,
*** 135,141 ****
  static tree fold_builtin_constant_p (tree);
  static tree fold_builtin_expect (location_t, tree, tree);
  static tree fold_builtin_classify_type (tree);
! static tree fold_builtin_strlen (location_t, tree);
  static tree fold_builtin_inf (location_t, tree, int);
  static tree fold_builtin_nan (tree, tree, int);
  static tree rewrite_call_expr (location_t, tree, int, tree, int, ...);
--- 135,141 ----
  static tree fold_builtin_constant_p (tree);
  static tree fold_builtin_expect (location_t, tree, tree);
  static tree fold_builtin_classify_type (tree);
! static tree fold_builtin_strlen (location_t, tree, tree);
  static tree fold_builtin_inf (location_t, tree, int);
  static tree fold_builtin_nan (tree, tree, int);
  static tree rewrite_call_expr (location_t, tree, int, tree, int, ...);
*************** fold_builtin_classify_type (tree arg)
*** 6617,6623 ****
  /* Fold a call to __builtin_strlen with argument ARG.  */
  
  static tree
! fold_builtin_strlen (location_t loc, tree arg)
  {
    if (!validate_arg (arg, POINTER_TYPE))
      return NULL_TREE;
--- 6617,6623 ----
  /* Fold a call to __builtin_strlen with argument ARG.  */
  
  static tree
! fold_builtin_strlen (location_t loc, tree type, tree arg)
  {
    if (!validate_arg (arg, POINTER_TYPE))
      return NULL_TREE;
*************** fold_builtin_strlen (location_t loc, tre
*** 6626,6637 ****
        tree len = c_strlen (arg, 0);
  
        if (len)
! 	{
! 	  /* Convert from the internal "sizetype" type to "size_t".  */
! 	  if (size_type_node)
! 	    len = fold_convert_loc (loc, size_type_node, len);
! 	  return len;
! 	}
  
        return NULL_TREE;
      }
--- 6626,6632 ----
        tree len = c_strlen (arg, 0);
  
        if (len)
! 	return fold_convert_loc (loc, type, len);
  
        return NULL_TREE;
      }
*************** fold_builtin_1 (location_t loc, tree fnd
*** 9659,9665 ****
        return fold_builtin_classify_type (arg0);
  
      case BUILT_IN_STRLEN:
!       return fold_builtin_strlen (loc, arg0);
  
      CASE_FLT_FN (BUILT_IN_FABS):
        return fold_builtin_fabs (loc, arg0, type);
--- 9654,9660 ----
        return fold_builtin_classify_type (arg0);
  
      case BUILT_IN_STRLEN:
!       return fold_builtin_strlen (loc, type, arg0);
  
      CASE_FLT_FN (BUILT_IN_FABS):
        return fold_builtin_fabs (loc, arg0, type);



More information about the Gcc-patches mailing list