bus error in strsep

Maksim Yevmenkin maksim.yevmenkin at savvis.net
Wed Jul 6 20:46:48 GMT 2005


>>>int main(int argc, char* argv[])
>>>{
>>>	char *c = "whats:your:name:buddy?";
>>
>>        ^^^^^^^^^^^^^^^^ that is not read only copy. you can not write
>>into it. replace it with
>>
>>        char *c = strdup("whats:your:name:buddy?");
> 
> Or the following:
> 
> 	char c[] = "whats:your:name:buddy?";
> 
> which doesn't require a free() operation when you're done with c[].

actually it still will crash :)

beetle% cat 5.c
#include <string.h>

int
main(int argc, char* argv[])
{
         char c[] = "whats:your:name:buddy?";
         strsep((char **) &c, ":");
         return (0);
}

beetle% gcc -Wall -ggdb 5.c
beetle% ./a.out
Segmentation fault (core dumped)

so something like this

#include <string.h>

int
main(int argc, char* argv[])
{
         char c[] = "whats:your:name:buddy?", *s = c;
         strsep((char **) &s, ":");
         return (0);
}

will work too.

max


More information about the freebsd-hackers mailing list