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]
Other format: [Raw text]

[patch] integrate.c: sizeof(char *) > sizeof(int) bug



Found and tested on sparc64-sun-solaris2.8.  The testcase is 0.5 Mb
(from samba, with heavy optimizations).  I don't know what the
standard says about comparing pointers vs subtracting pointers, but
what I saw was that it seemed to be sorting on the lower 32 bits of
the pointers, not the whole pointer, and the comparison in find_block
didn't have the same incorrect behavior, so it wouldn't find the
blocks in the list.  With this patch, at least the logic is identical,
and I've verified that the list is now sorted by the whole 64-bit
pointer.

And we can't change the return type to HOST_WIDE_INT because these are
used with qsort() and bsearch(), which want an int value.

2002-04-12  DJ Delorie  <dj@redhat.com>

	* integrate.c (compare_blocks): Make comparisons safe for when
	sizeof(int) < sizeof(char *).
	(find_block): Likewise.

Index: integrate.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/integrate.c,v
retrieving revision 1.187
diff -p -2 -r1.187 integrate.c
*** integrate.c	21 Mar 2002 18:39:13 -0000	1.187
--- integrate.c	12 Apr 2002 18:06:09 -0000
*************** process_reg_param (map, loc, copy)
*** 596,600 ****
  
  /* Compare two BLOCKs for qsort.  The key we sort on is the
!    BLOCK_ABSTRACT_ORIGIN of the blocks.  */
  
  static int
--- 596,601 ----
  
  /* Compare two BLOCKs for qsort.  The key we sort on is the
!    BLOCK_ABSTRACT_ORIGIN of the blocks.  We cannot just subtract the
!    two pointers, because it may overflow sizeof(int).  */
  
  static int
*************** compare_blocks (v1, v2)
*** 605,611 ****
    tree b1 = *((const tree *) v1);
    tree b2 = *((const tree *) v2);
  
!   return ((char *) BLOCK_ABSTRACT_ORIGIN (b1)
! 	  - (char *) BLOCK_ABSTRACT_ORIGIN (b2));
  }
  
--- 606,615 ----
    tree b1 = *((const tree *) v1);
    tree b2 = *((const tree *) v2);
+   char *p1 = (char *) BLOCK_ABSTRACT_ORIGIN (b1);
+   char *p2 = (char *) BLOCK_ABSTRACT_ORIGIN (b2);
  
!   if (p1 == p2)
!     return 0;
!   return p1 < p2 ? -1 : 1;
  }
  
*************** find_block (v1, v2)
*** 620,625 ****
    const union tree_node *b1 = (const union tree_node *) v1;
    tree b2 = *((const tree *) v2);
  
!   return ((const char *) b1 - (char *) BLOCK_ABSTRACT_ORIGIN (b2));
  }
  
--- 624,633 ----
    const union tree_node *b1 = (const union tree_node *) v1;
    tree b2 = *((const tree *) v2);
+   char *p1 = (char *) b1;
+   char *p2 = (char *) BLOCK_ABSTRACT_ORIGIN (b2);
  
!   if (p1 == p2)
!     return 0;
!   return p1 < p2 ? -1 : 1;
  }
  


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