This is the mail archive of the
binutils@sources.redhat.com
mailing list for the binutils project.
pointer comparison breaks Solaris' qsort
- From: Alexandre Oliva <aoliva at redhat dot com>
- To: binutils at sources dot redhat dot com
- Date: 08 Jun 2004 06:53:31 -0300
- Subject: pointer comparison breaks Solaris' qsort
- Organization: Red Hat Global Engineering Services Compiler Team
This patch fixes a linker crash caused by inconsistent results being
returned by the function passed to qsort(). In certain cases, when it
returns the same value when comparing both a with b and b with a, the
qsort function may end up being called with a pointer that's not
within the bounds of the array passed to qsort().
The inconsistent results were caused by computing the difference
between two pointers to objects that were not part of the same array,
which invokes undefined behavior. In the common failing cases, the
pointers to section do not differ by a multiple of 160 (the section
size), so when we perform an optimized divide of the byte difference
by the size, we get something that looks like garbage, and that
doesn't satisfy the property that compare (a, b) == - compare (b, a).
Anyway, comparing pointers is always a bad idea, since it means we may
get different results depending on the build machine.
I'm tempted to install it as obvious, but I'll ask... Ok to install?
Index: bfd/ChangeLog
from Alexandre Oliva <aoliva@redhat.com>
* elflink.c (elf_sort_symbol): Compare section id, not pointers.
Index: bfd/elflink.c
===================================================================
RCS file: /cvs/src/src/bfd/elflink.c,v
retrieving revision 1.71
diff -u -p -r1.71 elflink.c
--- bfd/elflink.c 25 May 2004 06:33:46 -0000 1.71
+++ bfd/elflink.c 8 Jun 2004 09:50:49 -0000
@@ -2728,7 +2728,7 @@ elf_sort_symbol (const void *arg1, const
return vdiff > 0 ? 1 : -1;
else
{
- long sdiff = h1->root.u.def.section - h2->root.u.def.section;
+ long sdiff = h1->root.u.def.section->id - h2->root.u.def.section->id;
if (sdiff != 0)
return sdiff > 0 ? 1 : -1;
}
--
Alexandre Oliva http://www.ic.unicamp.br/~oliva/
Red Hat Compiler Engineer aoliva@{redhat.com, gcc.gnu.org}
Free Software Evangelist oliva@{lsd.ic.unicamp.br, gnu.org}