public inbox for libc-hacker@sourceware.org
 help / color / mirror / Atom feed
* Fix memrchr
@ 2003-01-06 15:49 Andreas Jaeger
  2003-01-06 16:04 ` Jakub Jelinek
  0 siblings, 1 reply; 4+ messages in thread
From: Andreas Jaeger @ 2003-01-06 15:49 UTC (permalink / raw)
  To: GNU libc hacker


The first memrchr testcase in string/inl-tester failed for me with GCC
3.4.  Looking closer into it, Honza noticed that it is not valid C,
the arithmetic is wrong.

The appended patch fixes the bug.  Ok to commit?

Andreas

2003-01-06  Andreas Jaeger  <aj@suse.de>, Jan Hubicka <jh@suse.cz>

	* sysdeps/i386/i486/bits/string.h (__memrchr): Fix arithmetic.
	* sysdeps/i386/bits/string.h (__memrchr): Likewise.

============================================================
Index: sysdeps/i386/i486/bits/string.h
--- sysdeps/i386/i486/bits/string.h	4 Dec 2002 12:27:43 -0000	1.51
+++ sysdeps/i386/i486/bits/string.h	6 Jan 2003 15:10:00 -0000
@@ -1,5 +1,5 @@
 /* Optimized, inlined string functions.  i486 version.
-   Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
+   Copyright (C) 1997,1998,1999,2000,2001,2002,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
@@ -472,7 +472,7 @@ __memrchr (__const void *__s, int __c, s
 # ifdef __i686__
   register unsigned long int __d1;
 # endif
-  register void *__res;
+  register int __res;
   if (__n == 0)
     return NULL;
 # ifdef __i686__
@@ -497,7 +497,7 @@ __memrchr (__const void *__s, int __c, s
        "m" ( *(struct { __extension__ char __x[__n]; } *)__s)
      : "cc");
 # endif
-  return __res + 1;
+  return (void *)(__res + 1);
 }
 # ifdef __USE_GNU
 #  define memrchr(s, c, n) __memrchr (s, c, n)
============================================================
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	6 Jan 2003 15:10:00 -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
@@ -313,7 +313,7 @@ __STRING_INLINE void *
 __memrchr (__const void *__s, int __c, size_t __n)
 {
   register unsigned long int __d0;
-  register void *__res;
+  register int __res;
   if (__n == 0)
     return NULL;
   __asm__ __volatile__
@@ -325,7 +325,7 @@ __memrchr (__const void *__s, int __c, s
      : "=D" (__res), "=&c" (__d0)
      : "a" (__c), "0" (__s + __n - 1), "1" (__n)
      : "cc");
-  return __res + 1;
+  return (void *) (__res + 1);
 }
 # ifdef __USE_GNU
 #  define memrchr(s, c, n) __memrchr (s, c, n)


-- 
 Andreas Jaeger
  SuSE Labs aj@suse.de
   private aj@arthur.inka.de
    http://www.suse.de/~aj

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Fix memrchr
  2003-01-06 15:49 Fix memrchr Andreas Jaeger
@ 2003-01-06 16:04 ` Jakub Jelinek
  2003-01-06 16:26   ` Andreas Jaeger
  0 siblings, 1 reply; 4+ messages in thread
From: Jakub Jelinek @ 2003-01-06 16:04 UTC (permalink / raw)
  To: Andreas Jaeger; +Cc: GNU libc hacker

On Mon, Jan 06, 2003 at 04:33:21PM +0100, Andreas Jaeger wrote:
> 
> The first memrchr testcase in string/inl-tester failed for me with GCC
> 3.4.  Looking closer into it, Honza noticed that it is not valid C,
> the arithmetic is wrong.

Not valid C, but a documented GCC extension (see extend.texi).
If it doesn't work, it is a GCC bug.

> --- sysdeps/i386/i486/bits/string.h	4 Dec 2002 12:27:43 -0000	1.51
> +++ sysdeps/i386/i486/bits/string.h	6 Jan 2003 15:10:00 -0000
> @@ -1,5 +1,5 @@
>  /* Optimized, inlined string functions.  i486 version.
> -   Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
> +   Copyright (C) 1997,1998,1999,2000,2001,2002,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
> @@ -472,7 +472,7 @@ __memrchr (__const void *__s, int __c, s
>  # ifdef __i686__
>    register unsigned long int __d1;
>  # endif
> -  register void *__res;
> +  register int __res;
>    if (__n == 0)
>      return NULL;
>  # ifdef __i686__
> @@ -497,7 +497,7 @@ __memrchr (__const void *__s, int __c, s
>         "m" ( *(struct { __extension__ char __x[__n]; } *)__s)
>       : "cc");
>  # endif
> -  return __res + 1;
> +  return (void *)(__res + 1);
>  }
>  # ifdef __USE_GNU
>  #  define memrchr(s, c, n) __memrchr (s, c, n)

	Jakub

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Fix memrchr
  2003-01-06 16:04 ` Jakub Jelinek
@ 2003-01-06 16:26   ` Andreas Jaeger
  2003-01-06 18:39     ` Andreas Jaeger
  0 siblings, 1 reply; 4+ messages in thread
From: Andreas Jaeger @ 2003-01-06 16:26 UTC (permalink / raw)
  To: Jakub Jelinek; +Cc: GNU libc hacker

[-- Attachment #1: Type: text/plain, Size: 505 bytes --]

Jakub Jelinek <jakub@redhat.com> writes:

> On Mon, Jan 06, 2003 at 04:33:21PM +0100, Andreas Jaeger wrote:
>> 
>> The first memrchr testcase in string/inl-tester failed for me with GCC
>> 3.4.  Looking closer into it, Honza noticed that it is not valid C,
>> the arithmetic is wrong.
>
> Not valid C, but a documented GCC extension (see extend.texi).
> If it doesn't work, it is a GCC bug.

I'm appending the two test programs that I have.  What do you think?
Shall I report this as a GCC bug?

Andreas


[-- Attachment #2: Failed program --]
[-- Type: text/plain, Size: 1261 bytes --]


typedef unsigned int size_t;


extern int printf (__const char *__restrict __format, ...) ;

extern int puts (__const char *__s) ;


extern __inline void *__memrchr (__const void *__s, int __c, size_t __n);

extern __inline void *
__memrchr (__const void *__s, int __c, size_t __n)
{
  register unsigned long int __d0;



  register void *__res;
  if (__n == 0)
    return ((void *)0);

  __asm__ __volatile__
    ("std\n\t"
     "repne; scasb\n\t"
     "je 1f\n\t"
     "orl $-1,%0\n"
     "1:\tcld"
     : "=D" (__res), "=&c" (__d0)
     : "a" (__c), "0" (__s + __n - 1), "1" (__n),
       "m" ( *(struct { __extension__ char __x[__n]; } *)__s)
     : "cc");

  return __res + 1;
}

const char *it = "<UNSET>";
size_t errors = 0;


static void
check (int thing, int number)
{
  if (!thing)
    {
      printf("%s flunked test %d\n", it, number);
      ++errors;
    }
}

char one[50];
char two[50];
char *cp;

static void
test_memrchr (void)
{
  size_t l;
  it = "memrchr";
  check (__memrchr ("abcd", 'z', 5) == ((void *)0), 1);
}

int
main (void)
{
  int status;


  test_memrchr ();

  if (errors == 0)
    {
      status = 0;
      puts("No errors.");
    }
  else
    {
      status = 1;
      printf("%Zd errors.\n", errors);
    }

  return status;
}

[-- Attachment #3: Fixed program --]
[-- Type: text/plain, Size: 1269 bytes --]

typedef unsigned int size_t;


extern int printf (__const char *__restrict __format, ...) ;

extern int puts (__const char *__s) ;


extern __inline void *__memrchr (__const void *__s, int __c, size_t __n);

extern __inline void *
__memrchr (__const void *__s, int __c, size_t __n)
{
  register unsigned long int __d0;



  register int __res;
  if (__n == 0)
    return ((void *)0);

  __asm__ __volatile__
    ("std\n\t"
     "repne; scasb\n\t"
     "je 1f\n\t"
     "orl $-1,%0\n"
     "1:\tcld"
     : "=D" (__res), "=&c" (__d0)
     : "a" (__c), "0" (__s + __n - 1), "1" (__n),
       "m" ( *(struct { __extension__ char __x[__n]; } *)__s)
     : "cc");

  return (void *)(__res + 1);
}

const char *it = "<UNSET>";
size_t errors = 0;


static void
check (int thing, int number)
{
  if (!thing)
    {
      printf("%s flunked test %d\n", it, number);
      ++errors;
    }
}

char one[50];
char two[50];
char *cp;

static void
test_memrchr (void)
{
  size_t l;
  it = "memrchr";
  check (__memrchr ("abcd", 'z', 5) == ((void *)0), 1);
}

int
main (void)
{
  int status;


  test_memrchr ();

  if (errors == 0)
    {
      status = 0;
      puts("No errors.");
    }
  else
    {
      status = 1;
      printf("%Zd errors.\n", errors);
    }

  return status;
}


[-- Attachment #4: Type: text/plain, Size: 1177 bytes --]



>> --- sysdeps/i386/i486/bits/string.h	4 Dec 2002 12:27:43 -0000	1.51
>> +++ sysdeps/i386/i486/bits/string.h	6 Jan 2003 15:10:00 -0000
>> @@ -1,5 +1,5 @@
>>  /* Optimized, inlined string functions.  i486 version.
>> -   Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
>> +   Copyright (C) 1997,1998,1999,2000,2001,2002,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
>> @@ -472,7 +472,7 @@ __memrchr (__const void *__s, int __c, s
>>  # ifdef __i686__
>>    register unsigned long int __d1;
>>  # endif
>> -  register void *__res;
>> +  register int __res;
>>    if (__n == 0)
>>      return NULL;
>>  # ifdef __i686__
>> @@ -497,7 +497,7 @@ __memrchr (__const void *__s, int __c, s
>>         "m" ( *(struct { __extension__ char __x[__n]; } *)__s)
>>       : "cc");
>>  # endif
>> -  return __res + 1;
>> +  return (void *)(__res + 1);
>>  }
>>  # ifdef __USE_GNU
>>  #  define memrchr(s, c, n) __memrchr (s, c, n)
>
> 	Jakub
>

-- 
 Andreas Jaeger
  SuSE Labs aj@suse.de
   private aj@arthur.inka.de
    http://www.suse.de/~aj

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Fix memrchr
  2003-01-06 16:26   ` Andreas Jaeger
@ 2003-01-06 18:39     ` Andreas Jaeger
  0 siblings, 0 replies; 4+ messages in thread
From: Andreas Jaeger @ 2003-01-06 18:39 UTC (permalink / raw)
  To: Jakub Jelinek; +Cc: GNU libc hacker


I've reported this now as GCC bug #9196.  Thanks Jakub for the hint,

Andreas
-- 
 Andreas Jaeger
  SuSE Labs aj@suse.de
   private aj@arthur.inka.de
    http://www.suse.de/~aj

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2003-01-06 18:39 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-01-06 15:49 Fix memrchr Andreas Jaeger
2003-01-06 16:04 ` Jakub Jelinek
2003-01-06 16:26   ` Andreas Jaeger
2003-01-06 18:39     ` Andreas Jaeger

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).