gotcha in strtok(3)

Stephen J. Friedl friedl at vsi.UUCP
Wed Feb 24 17:49:57 AEST 1988


Netpeople,

     I'd like to relate a gotcha that could drive you crazy.  We
often use strtok(3) to run through a PATH-type variable to pick
apart the individual pieces.  Be careful when doing this with
strings obtained from the environment because strtok(3) puts in
little nul characters at the delimiters (modifying the string).
If the program later forks a child, the child's environment gets
a truncated version:

 BEFORE:    PATH=/bin:/usr/bin:/etc\0       \0 is a nul
 AFTER:     PATH=/bin\0/usr/bin\0/etc\0

     The remedy is to copy the variable into a local or static
buffer and run strtok(3) from that, leaving the environment
alone.

     We had a large program that picked apart a $DBPATH variable
(for the Informix database) but had no child processes until
about six months later.  At that point the child database opens
were failing and we spent a lot of time suspecting file and data-
base permissions, memory errors, etc.  *Many* debug statements
later we found it.  Sigh.

     Steve
-- 
Life : Stephen J. Friedl @ V-Systems Inc/Santa Ana, CA     *Hi Mom*
CSNet: friedl%vsi.uucp at kent.edu  ARPA: friedl%vsi.uucp at uunet.uu.net
uucp : {kentvax, uunet, attmail, ihnp4!amdcad!uport}!vsi!friedl



More information about the Comp.lang.c mailing list