2002-09-21 16:20:54

by Nicolas Pitre

[permalink] [raw]
Subject: [PATCH] fix to strchr() in lib/string.c


The return value of strchr("foo",0) should be the start address of
"foo" + 3, not NULL.


--- linux/lib/string.c Thu Aug 1 17:16:34 2002
+++ linux/lib/string.c Sat Sep 21 12:21:54 2002
@@ -190,10 +190,11 @@
*/
char * strchr(const char * s, int c)
{
- for(; *s != (char) c; ++s)
- if (*s == '\0')
- return NULL;
- return (char *) s;
+ do {
+ if (*s == (char) c)
+ return (char *) s;
+ } while (*s++);
+ return NULL;
}
#endif



2002-09-21 17:25:44

by Dan Aloni

[permalink] [raw]
Subject: Re: [PATCH] fix to strchr() in lib/string.c

On Sat, Sep 21, 2002 at 12:25:59PM -0400, Nicolas Pitre wrote:
>
> The return value of strchr("foo",0) should be the start address of
> "foo" + 3, not NULL.

Correct me if I'm wrong, but no fix is needed.

strchr("foo", 0) doesn't return NULL, for the simple fact that
the loop will stop when reaching '\0' before the 'if' that returns
NULL, and then s will be returned.

If it wasn't like this, add_stats() (in net/atm/proc.c)
would have Oopsed on us long ago.

> --- linux/lib/string.c Thu Aug 1 17:16:34 2002
> +++ linux/lib/string.c Sat Sep 21 12:21:54 2002
> @@ -190,10 +190,11 @@
> */
> char * strchr(const char * s, int c)
> {
> - for(; *s != (char) c; ++s)
> - if (*s == '\0')
> - return NULL;
> - return (char *) s;
> + do {
> + if (*s == (char) c)
> + return (char *) s;
> + } while (*s++);
> + return NULL;
> }
> #endif
>
>

--
Dan Aloni
[email protected]

2002-09-21 19:23:17

by Nicolas Pitre

[permalink] [raw]
Subject: Re: [PATCH] fix to strchr() in lib/string.c

On Sat, 21 Sep 2002, Dan Aloni wrote:

> On Sat, Sep 21, 2002 at 12:25:59PM -0400, Nicolas Pitre wrote:
> >
> > The return value of strchr("foo",0) should be the start address of
> > "foo" + 3, not NULL.
>
> Correct me if I'm wrong, but no fix is needed.
>
> strchr("foo", 0) doesn't return NULL, for the simple fact that
> the loop will stop when reaching '\0' before the 'if' that returns
> NULL, and then s will be returned.

Doh. You're right.

I was fixing some architecture specific version and someone I usually trust
led me to believe this one was broken too, and I obviously didn't look
carefully enough.

(no no I won't say it was you Russell) ;-)


Nicolas