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


> Mailing-List: contact libc-alpha-help@sourceware.cygnus.com; run by ezmlm
> List-Unsubscribe: <mailto:libc-alpha-unsubscribe-geoffk=cygnus.com@sourceware.cygnus.com>
> List-Subscribe: <mailto:libc-alpha-subscribe@sourceware.cygnus.com>
> List-Archive: <http://sourceware.cygnus.com/ml/libc-alpha/>
> List-Post: <mailto:libc-alpha@sourceware.cygnus.com>
> List-Help: <mailto:libc-alpha-help@sourceware.cygnus.com>, <http://sourceware.cygnus.com/ml/#faqs>
> Date: Thu, 13 Jan 2000 12:56:54 +0100 (CET)
> From: Clifford Wolf <clifford@clifford.at>
> 
> Hi,
> 
> I'm currently porting ROCK Linux (www.rocklinux.org) to the PPC
> architecture. When porting telnetd (and some other packages) I found
> the following bug which doesn't seam to be architecture depended but
> only happens on PPC for me. Here is an example program:
> 
> --snip--
> #include <string.h>
> 
> int main() {
> 	strtok("",":");    /* returns NULL */
> 	strtok(NULL,":");  /* should return NULL - but dups core on ppc */
> }

I think you've found a bug in the C standard.  The standard says:


A sequence of calls to the strtok function breaks the string pointed
to by s1 into a sequence of tokens, each of which is delimited by a
character from the string pointed to by s2. The first call in the
sequence has a non-null first argument; subsequent calls in the
sequence have a null first argument. The separator string pointed to
by s2 may be different from call to call.

The first call in the sequence searches the string pointed to by s1
for the first character that is not contained in the current separator
string pointed to by s2. If no such character is found, then there are
no tokens in the string pointed to by s1 and the strtok function
returns a null pointer. If such a character is found, it is the start
of the first token. 

The strtok function then searches from there for a character that is
contained in the current separator string. If no such character 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. If such a character is found, it is overwritten by a null
character, which terminates the current token. The strtok function
saves a pointer to the following character, from which the next search
for a token will start. 

Each subsequent call, with a null pointer as the value of the first
argument, starts searching from the saved pointer and behaves as
described above.


The problem is that the standard doesn't say that it saves a pointer
when the first call returns a null pointer.

-- 
- Geoffrey Keating <geoffk@cygnus.com>

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