This is the mail archive of the libc-alpha@sourceware.cygnus.com mailing list for the glibc project.


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

Re: strtok bug


Hi,

> > The problem is that the standard doesn't say that it saves a pointer
> > when the first call returns a null pointer.
> 
> This is not a problem at all. Why should it?  The strtok function is intended
> to extract tokens one by one. When it returns null, the search is over due to
> hitting the end of string.  You must start a new search.
> 
> There is some extra behavior that can be deduced from the standard's
> description; namely once you hit the end of the string after extracting one or
> more tokens, you can keep calling strtok with a null argument to try to extract
> more tokens, but it will keep failing.

That's exactly what I also think. And I have some programms here which
dump core on ppc becouse that strtok behavior. At least inetd (I'm sure
about inetd becouse I wroke a workarround there).

The other thing is that it only dumps core it the first argument is "".

strtok("a",":"); strtok(NULL,":");  strtok(NULL,":"); strtok(NULL,":");

Works fine on ppc becouse the pointer is saved as soon as the first
charecter has been procesed. There is only one execption: If there is
no character processed.

This code in the function should return NULL if the last token has been
allready processed and we are allready at the end of the string:

   if (*s == '\0')
     return NULL;

The value of 's' is saved in the static 'olds' variable and will be
restored when strotk(NULL,"foo") is called the next time - so it returns
NULL again. But this fails if no character has been processed so far
becouse the value if 's' is stored in 'olds' later in the function.

The easy way to fix it would be to replace the code shown above with:

   if (*s == '\0')
     { olds=""; return NULL; }

or probably better:

   if (*s == '\0')
     { olds=s; return NULL; }

The X/Open standard says:

"... If no such byte is found, the current token extends to the end of the
string pointed to by s1, and subsequent searches for a token will return a
null pointer. ..."

Sure - It's not defined in the standard what should happen for subsequent
searches when allready the first call returned a NULL pointer. But it does
not say 'Read and write from a NULL pointer and dump core'. It just says
nothing about it - so the most logical thing would be to do the same thing
as it's defined for the case that the last token has been reached and
another search is invoked: returning a NULL pointer.

> Ah well. I never want to think about strtok again! ;)

Me too. But I don't have a choice becouse I have to port programms which
depend on the function (like inetd) and I don't like to write workarrounds
for all the programms becouse a bug in the c library.

yours,
 - clifford

PS: I still don't know why I only get the core dumps on ppc. It seams like
some cracy pointer magic ...

--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
Clifford Wolf ............... www.clifford.at                 ICQ: 60702100
The ROCK Projects Workgoup .. www.rock-projects.com   Tel: +43-699-10063494
The ROCK Linux Workgoup ..... www.rocklinux.org       Fax: +43-2235-42788-4
The NTx Consulting Group .... www.ntx.at             email: god@clifford.at


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