This is the mail archive of the libc-hacker@sources.redhat.com mailing list for the glibc project.

Note that libc-hacker is a closed list. You may look at the archives of this list, but subscription and posting are not open.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

fix memrchr for GCC 3.4


GCC 3.4 miscompiles memrchr (the testsuite fails).  The GCC developers
closed my bugs as invalid:
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=9196

with the following comment:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The C semantics for pointer arithmetic mean that GCC is allowed to assume that
this line

  return __res + 1;

never causes a null pointer to be returned.  The "mis-optimized" code has been
optimized based on that assumption.  In short, the bug is in glibc.

The simplest fix is to write "incl %0" as the last instruction of the assembly
block rather than trying to do this calculation in C. 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Here's a patch for this, ok to commit?

Andreas

2003-09-04  Andreas Jaeger  <aj@suse.de>

	* sysdeps/i386/bits/string.h (__memrchr): Do addition in assembler
	to make it conforming C.
	* sysdeps/i386/i486/bits/string.h (__memrchr): Likewise.

============================================================
Index: sysdeps/i386/bits/string.h
--- sysdeps/i386/bits/string.h	6 Jul 2001 04:55:53 -0000	1.21
+++ sysdeps/i386/bits/string.h	4 Sep 2003 10:05:12 -0000
@@ -1,5 +1,5 @@
 /* Optimized, inlined string functions.  i386 version.
-   Copyright (C) 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
+   Copyright (C) 1997,1998,1999,2000,2003 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
 
    The GNU C Library is free software; you can redistribute it and/or
@@ -321,11 +321,12 @@ __memrchr (__const void *__s, int __c, s
      "repne; scasb\n\t"
      "je 1f\n\t"
      "orl $-1,%0\n"
-     "1:\tcld"
+     "1:\tcld\n\t"
+     "incl %0"
      : "=D" (__res), "=&c" (__d0)
      : "a" (__c), "0" (__s + __n - 1), "1" (__n)
      : "cc");
-  return __res + 1;
+  return __res;
 }
 # ifdef __USE_GNU
 #  define memrchr(s, c, n) __memrchr (s, c, n)
============================================================
Index: sysdeps/i386/i486/bits/string.h
--- sysdeps/i386/i486/bits/string.h	14 Jan 2003 07:44:02 -0000	1.55
+++ sysdeps/i386/i486/bits/string.h	4 Sep 2003 10:05:12 -0000
@@ -484,7 +484,8 @@ __memrchr (__const void *__s, int __c, s
     ("std\n\t"
      "repne; scasb\n\t"
      "cmovne %2,%0\n\t"
-     "cld"
+     "cld\n\t"
+     "incl %0"
      : "=D" (__res), "=&c" (__d0), "=&r" (__d1)
      : "a" (__c), "0" (__s + __n - 1), "1" (__n), "2" (-1),
        "m" ( *(struct { __extension__ char __x[__n]; } *)__s)
@@ -495,13 +496,14 @@ __memrchr (__const void *__s, int __c, s
      "repne; scasb\n\t"
      "je 1f\n\t"
      "orl $-1,%0\n"
-     "1:\tcld"
+     "1:\tcld\n\t"
+     "incl %0"
      : "=D" (__res), "=&c" (__d0)
      : "a" (__c), "0" (__s + __n - 1), "1" (__n),
        "m" ( *(struct { __extension__ char __x[__n]; } *)__s)
      : "cc");
 # endif
-  return __res + 1;
+  return __res;
 }
 # ifdef __USE_GNU
 #  define memrchr(s, c, n) __memrchr ((s), (c), (n))

-- 
 Andreas Jaeger, aj@suse.de, http://www.suse.de/~aj
  SuSE Linux AG, Deutschherrnstr. 15-19, 90429 Nürnberg, Germany
   GPG fingerprint = 93A3 365E CE47 B889 DF7F  FED1 389A 563C C272 A126

Attachment: pgp00000.pgp
Description: PGP signature


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