Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > strtok segfaults in CLI but not in GDB

Reply
Thread Tools

strtok segfaults in CLI but not in GDB

 
 
Pietro Cerutti
Guest
Posts: n/a
 
      05-16-2007
Hello,
here I have a strange problem with a real simple strtok example.

The program is as follows:

### BEGIN STRTOK ###

#include <string.h>
#include <stdio.h>

int main()
{
char *input1 = "Hello, World!";

char *tok;

tok = strtok(input1, " ");
if(tok) printf("%s\n", tok);

tok = strtok(NULL, " ");
if(tok) printf("%s\n", tok);

return(0);

}

### END STRTOK ###


Now, when I run it from the command line, I get a bus error:

### BEGIN COMMAND LINE OUTPUT ###

> gcc -ggdb -Wall -o strtok strtok.c
> ./strtok

Bus error (core dumped)
Exit 138

### END COMMAND LINE OUTPUT ###

When I run it step by step in GDB, the program terminates normally:

### BEGIN DEBUGGER OUTPUT ###

> gdb ./strtok

GNU gdb 6.1.1 [FreeBSD]
[snip]GDB copyright and bla bla[/snip]
(gdb) break main
Breakpoint 1 at 0x8048570: file strtok.c, line 6.
(gdb) run
Starting program: /home/piter/strtok

Breakpoint 1, main () at strtok.c:6
6 char *input1 = "Hello, World!";
(gdb) next
10 tok = strtok(input1, " ");
(gdb)
11 if(tok) printf("%s\n", tok);
(gdb)
Hello,
13 tok = strtok(NULL, " ");
(gdb)
14 if(tok) printf("%s\n", tok);
(gdb)
World!
16 return(0);
(gdb)
18 }
(gdb)
0x08048485 in _start ()
(gdb)
Single stepping until exit from function _start,
which has no line number information.

Program exited normally.
(gdb)

### END DEBUGGER OUTPUT ###

Is there something I'm missing wrt C and/or strtok, or it's rather a
problem related to my environment (in which case I'll be happy to post
in the right newsgroup) ?

Thanx in advance

--
Pietro Cerutti

PGP Public Key ID:
http://gahr.ch/pgp
 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      05-16-2007
Pietro Cerutti wrote:
> Hello,
> here I have a strange problem with a real simple strtok example.
>
> The program is as follows:
>
> ### BEGIN STRTOK ###
>
> #include <string.h>
> #include <stdio.h>
>
> int main()
> {
> char *input1 = "Hello, World!";
>
> char *tok;
>
> tok = strtok(input1, " ");


strtok alters its input. You are passing it a string literal, modifying
a string literal invokes the demons of undefined behavior. Don't.

--
Ian Collins.
 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      05-16-2007
Pietro Cerutti said:

> Hello,
> here I have a strange problem with a real simple strtok example.
>
> The program is as follows:
>
> ### BEGIN STRTOK ###
>
> #include <string.h>
> #include <stdio.h>
>
> int main()
> {
> char *input1 = "Hello, World!";
>
> char *tok;
>
> tok = strtok(input1, " ");


strtok modifies the string you pass it. You pass it a string literal.
You're not allowed to modify string literals.

Change

char *input1 = "Hello, World!";

to

char input1[] = "Hello, World!";


--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
 
Reply With Quote
 
Pietro Cerutti
Guest
Posts: n/a
 
      05-16-2007
Pietro Cerutti wrote:

> char *input1 = "Hello, World!";


just in case, I know that the string to be tokenized shouldn't be a
constant, but rather an array of chars.
So, it should be declared as

char input1[14] = "Hello, World!";

The thing I don't understand is: why does it works in GDB?

--
Pietro Cerutti

PGP Public Key:
http://gahr.ch/pgp
 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      05-16-2007
Pietro Cerutti wrote:
> Pietro Cerutti wrote:
>
>> char *input1 = "Hello, World!";

>
> just in case, I know that the string to be tokenized shouldn't be a
> constant, but rather an array of chars.
> So, it should be declared as
>
> char input1[14] = "Hello, World!";
>
> The thing I don't understand is: why does it works in GDB?
>

Luck?

--
Ian Collins.
 
Reply With Quote
 
Chris Dollin
Guest
Posts: n/a
 
      05-16-2007
Pietro Cerutti wrote:

> here I have a strange problem with a real simple strtok example.


Guess: you're trying to use it on a literal string.

> The program is as follows:
>
> ### BEGIN STRTOK ###
>
> #include <string.h>
> #include <stdio.h>
>
> int main()
> {
> char *input1 = "Hello, World!";
>
> char *tok;
>
> tok = strtok(input1, " ");
> if(tok) printf("%s\n", tok);
>
> tok = strtok(NULL, " ");
> if(tok) printf("%s\n", tok);
>
> return(0);
>
> }


(fx:dancing) Yes!

`strtok` writes to its argument -- it sticks nuls in there to make
the strings it returns.

You're not allowed to write into a string literal: that gets you
undefined behaviour.

An implementation may just write into the string. Or it may abort in
some way. Or it may ignore the write. Or it may write somewhere else
entirely. Or it may mail a report to your co-coders, or start a game
of rogue, or book you a holiday in the Lake District, or set fire to
your keyboard, or arrange a date with your Most Preferred Person.

[That last one never seems to happen, though.]

--
"You've spotted a flaw in my thinking, Trev" Big Al,/The Beiderbeck Connection/

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

 
Reply With Quote
 
Pietro Cerutti
Guest
Posts: n/a
 
      05-16-2007
Ian Collins wrote:
> Pietro Cerutti wrote:
>> Pietro Cerutti wrote:
>>
>>> char *input1 = "Hello, World!";

>> just in case, I know that the string to be tokenized shouldn't be a
>> constant, but rather an array of chars.
>> So, it should be declared as
>>
>> char input1[14] = "Hello, World!";
>>
>> The thing I don't understand is: why does it works in GDB?
>>

> Luck?
>


Ya, maybe.

The point is:
I understand what UB means, so WW3 could start now and I'd know why...

But if a string literal is - by definition - not modifiable, then how
can it happen that GDB actually modifies it using strtok?

--
Pietro Cerutti

PGP Public Key:
http://gahr.ch/pgp
 
Reply With Quote
 
Pietro Cerutti
Guest
Posts: n/a
 
      05-16-2007
Chris Dollin wrote:

> You're not allowed to write into a string literal: that gets you
> undefined behaviour.
>
> An implementation may just write into the string.


Uh? So you mean that a string literal isn't unmodifiable by definition?


--
Pietro Cerutti

PGP Public Key:
http://gahr.ch/pgp
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      05-16-2007
Pietro Cerutti <(E-Mail Removed)> writes:
> Pietro Cerutti wrote:
>
>> char *input1 = "Hello, World!";

>
> just in case, I know that the string to be tokenized shouldn't be a
> constant, but rather an array of chars.
> So, it should be declared as
>
> char input1[14] = "Hello, World!";
>
> The thing I don't understand is: why does it works in GDB?


Because it invokes undefined behavior. There are no rules about what
happens. It can crash, it can "work", it can make demons fly out of
your nose.

(I suppose string literals are stored in write-protected memory when
your program runs normally, but not when it runs under gdb -- which
seems odd.)

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Pietro Cerutti
Guest
Posts: n/a
 
      05-16-2007
Keith Thompson wrote:

> (I suppose string literals are stored in write-protected memory when
> your program runs normally, but not when it runs under gdb -- which
> seems odd.)


Yes it's weird, but it's a logical explanation.
I'll investigate with the freebsd people..
Thank you.

--
Pietro Cerutti

PGP Public Key:
http://gahr.ch/pgp
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Selling Routes Lebanon Premium CLI and Iran Premium CLI: Lubna Mahar VOIP 0 11-23-2010 11:20 AM
Selling Routes Lebanon Premium CLI and Pakistan Premium CLI: Lubna Mahar VOIP 0 11-04-2010 12:57 PM
Debugging with gdb: pystack macro for gdb to show python stack trace Timothy Madden Python 1 09-17-2009 11:47 AM
GDB - no debugging symbols found (GDB Vs GCC?) seba C++ 1 03-22-2007 02:02 PM
Capturing gdb output in pyhton script using gdb -batch -x arguments Surendra Python 0 03-23-2006 02:47 PM



Advertisments