Search this site


Metadata

Articles

Projects

Presentations

Chasing down a bug while using strtok.

While coding tonight, I spent *at least* 20 minutes trying to figure out why my program was crashing in strtok_r. Turns out I forgot that strtok (and strtok_r) modify the string in-place (with null bytes), and I was passing in a string literal.

In C, "string literals" are stored in a special read-only bit of the program, so if you try to modify them your program crashes. I was calling a function several levels up with a string literal trying to test functionality and it kept crashing with sigbus. Wee. Lesson relearned ;)


3 responses to 'Chasing down a bug while using strtok.'

Showing last 3 comments... (Click here to view all comments)

candice wrote at Tue Jun 19 08:18:40 2007...
If you must, -fwritable-strings to gcc.  Useful when porting non-ansi code where people assume they can write into strings.

Jordan Sissel wrote at Tue Jun 19 09:31:30 2007...
@wxs, yeah, but I wasn't sure if strsep worked on Linux or Solaris. I can't figure out how to get libc manpages installed on ubuntu; despite 'glibc-doc' being installed, 'man strtok' doesn't show up. Sigh.

@candice, yeah, I wasn't going to go down that road. My solution was wrapping with a lame strdup()

void foo(char *x) {
  strtok(x ... )
}
...
foo(strdup("one two three"));

candice wrote at Tue Jun 19 11:28:53 2007...
That'll do... 

And that road I mentioned is not for the faint of heart... it's just for a big chunk of code that expects it can do things that it can't.


Leave a reply

You need javascript enabled to use this form. Anti-spam efforts ongoing. Also, if the comment doesn't show up, it's because the form expired. Go back and copy your comment, reload the form, and resubmit. Apologies if this is a hassle, I'm just playing with antispam methods right now. If this insists on not working, please email me about it.

Name (required)
E-mail (optional, if you want me to be able to email you back)
URL (also optional)
Comment: