It is no longer possible to modify string constants in the ChorusOS 4.0 operating system. As a result, the strsep() function will cause a segmentation fault if called with a pointer to a literal string. This is demonstrated in Example 3-1.
int main(int argc, char **argv) { char *s = "aaaa/bbbb"; char *r; char **sp = &s; r = strsep(sp, "/"); /* a segmentation fault is raised */ }
Example 3-1 does not cause a segmentation fault
in the ChorusOS 3.2 operating system because the gcc .rodata
section was mapped to a writable memory region. In the ChorusOS 4.0
operating system, the gcc .rodata
section is mapped
to a read-only memory region.
Example 3-1 can be corrected by calling strsep() with a non-literal string, shown in Example 3-2.
int main(int argc, char **argv) { char *s = "aaaa/bbbb"; char *r; char **sp = &s; char *tmp; tmp = strdup(s); if (tmp == NULL) { printf("out of memory\n"); return 0; } sp = &tmp; r = strsep(sp, "/"); /* this works */ }
See the strsep(3STDC) man page for more information.