photo
Jordan Sissel
geek

Tue, 19 Jun 2007

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 ;)

Comments: 4 (view comments)
Tags: , ,
Permalink: /geekery/strtok-gotchas
posted at: 06:15


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

Wesley Shields posted at Tue Jun 19 09:59:06 2007...
Assuming you're doing this on a FreeBSD box, strtok(3) is obsoleted in favor of strsep(3).

And I see no point in parsing a literal, it's not going to change and you already know where the tokens are.  That is, unless this was just a quick test case and the literal will eventually be replaced with a variable.

Now I'm rambling...

candice posted at Tue Jun 19 11: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 posted at Tue Jun 19 12: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 posted at Tue Jun 19 14: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:


Search this site

Navigation

Metadata

Home About Resume My Code (SVN)

Articles

ARP Security Dynamic DNS with DHCP OpenLDAP+Kerberos+SASL PPP over SSH SSH Security: /bin/false Week of Unix Tools Work Efficiency

Projects

fex firefox tabsearch firefox urledit grok keynav liboverride newpsm (FreeBSD) nis2ldap pam_captcha poor man's backup Solaris audio utility xboxproxy xdotool xmlpresenter xpathtool misc scripts

Presentations

Yahoo! Hack Day '06 Unix Essentials Vi/Vim Essentials

Tag Cloud

Calendar

< June 2007 >
SuMoTuWeThFrSa
      1 2
3 4 5 6 7 8 9
10111213141516
17181920212223
24252627282930

Friends

BarCamp Kent Brewster Tantek Çelik John Resig Wesley Shields Tyler Shields

Technorati