Re: bus error in strsep
Stefan Sperling wrote: On Wed, Jul 06, 2005 at 12:10:23PM -0700, Maksim Yevmenkin wrote: Maksim Yevmenkin wrote: char *c = whats:your:name:buddy?; that is not read only copy. you can not write into it. replace it with made type. that should read that is read only copy :) Dark corners of C... So it's my own fault, as usual :) thanks a lot :) Or you can be a bad guy and compile with GCC and -fwritable-strings so that it puts the const string into a read-write section :-). This is an absolutely evil hack, of course. Maxime ___ freebsd-hackers@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-hackers To unsubscribe, send any mail to [EMAIL PROTECTED]
Re: bus error in strsep
In [EMAIL PROTECTED], Dimitry Andric [EMAIL PROTECTED] typed: On 2005-07-06 at 21:41:00 Stefan Sperling wrote: char *c = whats:your:name:buddy?; made type. that should read that is read only copy :) Dark corners of C... So it's my own fault, as usual :) Actually, this dark corner was enlightened not so long ago. String constants used to be writable for years, until someone decided that it was better not to. :) I think it used to be implementation-dependent. IIRC, the VMS C compiler didn't have writable string constants. I know there was something like that that bit attempts to port Unix programs to VMS. mike -- Mike Meyer [EMAIL PROTECTED] http://www.mired.org/consulting.html Independent Network/Unix/Perforce consultant, email for more information. ___ freebsd-hackers@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-hackers To unsubscribe, send any mail to [EMAIL PROTECTED]
bus error in strsep
Hello hackers, I am getting a bus error in my application when I call strsep and it matches a character. It doesn't matter whether I call the strsep from my libc or a freshly compiled one, the error stays the same. This is my test case: $ cat strsep.c #define NULL ((void*)0) /* copied verbatim from /usr/src/lib/libc/string/strsep.c, * except for the name change */ char * mystrsep(stringp, delim) char **stringp; const char *delim; { char *s; const char *spanp; int c, sc; char *tok; if ((s = *stringp) == NULL) return (NULL); for (tok = s;;) { c = *s++; spanp = delim; do { if ((sc = *spanp++) == c) { if (c == 0) s = NULL; else s[-1] = 0; *stringp = s; return (tok); } } while (sc != 0); } /* NOTREACHED */ } int main(int argc, char* argv[]) { char *c = whats:your:name:buddy?; (void*)mystrsep(c, :); } $ gcc -g -o strsep strsep.c $ gdb strsep gnu license (gdb) run Starting program: /home/stsp/test/strsep Program received signal SIGBUS, Bus error. 0x080484f2 in mystrsep (stringp=0xbfbfea34, delim=0x80485e6 :) at strsep.c:26 26 s[-1] = 0; (gdb) print s[-1] $1 = 58 ':' (gdb) When I single step through mystrsep the program works fine. I am running FreeBSD-current from 17th June 2005 on an Athlon-XP, no SMP involved. I can reproduce the error on a dual Celeron box running FreeBSD-5.4-RELEASE with SMP. And I also get the same error with similar code using strtok. Can anyone else reproduce this? thanks a lot, -- stefan http://stsp.in-berlin.de PGP Key: 0xF59D25F0 ___ freebsd-hackers@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-hackers To unsubscribe, send any mail to [EMAIL PROTECTED]
Re: bus error in strsep
Stefan, 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?); (void*)mystrsep(c, :); } and it should work. thanks, max ___ freebsd-hackers@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-hackers To unsubscribe, send any mail to [EMAIL PROTECTED]
Re: bus error in strsep
Maksim Yevmenkin wrote: Stefan, 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 made type. that should read that is read only copy :) char *c = strdup(whats:your:name:buddy?); (void*)mystrsep(c, :); } and it should work. thanks, max ___ freebsd-hackers@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-hackers To unsubscribe, send any mail to [EMAIL PROTECTED]
Re: bus error in strsep
On Wed, Jul 06, 2005 at 12:10:23PM -0700, Maksim Yevmenkin wrote: Maksim Yevmenkin wrote: char *c = whats:your:name:buddy?; that is not read only copy. you can not write into it. replace it with made type. that should read that is read only copy :) Dark corners of C... So it's my own fault, as usual :) thanks a lot :) -- stefan http://stsp.in-berlin.de PGP Key: 0xF59D25F0 ___ freebsd-hackers@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-hackers To unsubscribe, send any mail to [EMAIL PROTECTED]
Re: bus error in strsep
On 2005-07-06 at 21:41:00 Stefan Sperling wrote: char *c = whats:your:name:buddy?; made type. that should read that is read only copy :) Dark corners of C... So it's my own fault, as usual :) Actually, this dark corner was enlightened not so long ago. String constants used to be writable for years, until someone decided that it was better not to. :) Anyway, you can get the old (deprecated!) behaviour by using the -fwritable-strings option to gcc. pgpOjDCRlF0Rw.pgp Description: PGP signature
Re: bus error in strsep
On 2005-07-06 12:08, Maksim Yevmenkin [EMAIL PROTECTED] wrote: Stefan, 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[]. ___ freebsd-hackers@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-hackers To unsubscribe, send any mail to [EMAIL PROTECTED]
Re: bus error in strsep
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 ___ freebsd-hackers@freebsd.org mailing list http://lists.freebsd.org/mailman/listinfo/freebsd-hackers To unsubscribe, send any mail to [EMAIL PROTECTED]