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
May 16 '07
29 2588
In article <40************ *************** @news.hispeed.c h>,
Pietro Cerutti <ga**@gahr.chwr ote:
>But if a string literal is - by definition - not modifiable, then how can it happen that GDB actually modifies it using strtok?
It's not modifiable in that you're not allowed to modify it. It's not
required that the implementation signal an error when you do it. It's
a constraint on you, not on the system.
My guess as to why you don't see an error with GDB is that the
debugger needs the text segment to be writable, so that it can set
breakpoints.
-- Richard
--
"Considerat ion shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Pietro Cerutti <ga**@gahr.chwr ites:
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?
I think you don't *quite* understand what UB means.
The actual definition (C99 3.4.3) is:
behavior, upon use of a nonportable or erroneous program construct
or of erroneous data, for which this International Standard
imposes no requirements
and C99 6.4.5p6 says:
[...] If the program attempts to modify such an array, the
behavior is undefined.
For example, consider this program:
#include <stdio.h>
int main(void)
{
char *s = "Hello, world";
s[0] = 'J'; /* attempt to modify a string literal */
puts(s);
return 0;
}
One of the infinitely many possibly results is that the string literal
is actually modified, and the program prints "Jello, world".
The standard doesn't say that string literals are not modifiable. It
says that attempting to modify a string literal invokes undefined
behavior.
--
Keith Thompson (The_Other_Keit h) ks***@mib.org <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"
Keith Thompson wrote:
The standard doesn't say that string literals are not modifiable. It
says that attempting to modify a string literal invokes undefined
behavior.
Got it. Thanks!
--
Pietro Cerutti
PGP Public Key: http://gahr.ch/pgp
Pietro Cerutti wrote:
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?
Yes, that's what I (well, the C standard) says.
Specifically, it says that if you attempt to write into a string literal,
/the effect is undefined/. Anything can happen. C washes it's hands of
your code. It cares not. Mind the gap. Do as you will.
An implementation may implement this freedom by changing the content of
the literal, if that's convenient.
Hence: don't go writing into string literals. Even though it /might/
get you a date, it probably won't, and I am assured that nasal demons
are not fun to have.
--
"I'm still here and I'm holding the answers" - Karnataka, /Love and Affection/
Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN
Richard Tobin wrote:
My guess as to why you don't see an error with GDB is that the
debugger needs the text segment to be writable, so that it can set
breakpoints.
GDB on Debian/GNU Linux gives an error when I try to modify it.
On FreeBSD it doesn't, that's why I'm asking right now the FreeBSD
people whether the behavior is wanted or erroneous.
Thanx
>
-- Richard
--
Pietro Cerutti
PGP Public Key: http://gahr.ch/pgp
Chris Dollin wrote:
Pietro Cerutti wrote:
>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?
Yes, that's what I (well, the C standard) says.
Specifically, it says that if you attempt to write into a string literal,
/the effect is undefined/. Anything can happen. C washes it's hands of
your code. It cares not. Mind the gap. Do as you will.
An implementation may implement this freedom by changing the content of
the literal, if that's convenient.
Hence: don't go writing into string literals. Even though it /might/
get you a date, it probably won't, and I am assured that nasal demons
are not fun to have.
Clear. Thanks to you too.
--
Pietro Cerutti
PGP Public Key: http://gahr.ch/pgp
Pietro Cerutti said:
Richard Tobin wrote:
>My guess as to why you don't see an error with GDB is that the debugger needs the text segment to be writable, so that it can set breakpoints.
GDB on Debian/GNU Linux gives an error when I try to modify it.
That's an acceptable outcome of undefined behaviour.
On FreeBSD it doesn't,
So's that.
that's why I'm asking right now the FreeBSD
people whether the behavior is wanted or erroneous.
It is neither Debian nor FreeBSD, but rather your program, that is
erroneous.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk
email: rjh at the above domain, - www.
In article <Qq************ *************** ***@bt.com>,
Richard Heathfield <rj*@see.sig.in validwrote:
>GDB on Debian/GNU Linux gives an error when I try to modify it.
That's an acceptable outcome of undefined behaviour.
>On FreeBSD it doesn't,
So's that.
that's why I'm asking right now the FreeBSD
>people whether the behavior is wanted or erroneous.
It is neither Debian nor FreeBSD, but rather your program, that is erroneous.
I think he meant "erroneous" in the sense of a mistake, rather than
a violation of the C standard.
It certainly seems desirable to have programs behave the same way
under the debugger as without it, so it would be good if the FreeBSD
version could be changed. Meanwhile, we at least have a clue that if
a segmentation fault goes away in the debugger then the cause may well
be attempted modification of literal strings.
-- Richard
--
"Considerat ion shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
"Keith Thompson" <ks***@mib.orgw rote in message news:ln******** ****@nuthaus.mi b.org...
Pietro Cerutti <ga**@gahr.chwr ites:
>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?
I think you don't *quite* understand what UB means.
The actual definition (C99 3.4.3) is:
behavior, upon use of a nonportable or erroneous program construct
or of erroneous data, for which this International Standard
imposes no requirements
and C99 6.4.5p6 says:
[...] If the program attempts to modify such an array, the
behavior is undefined.
For example, consider this program:
#include <stdio.h>
int main(void)
{
char *s = "Hello, world";
s[0] = 'J'; /* attempt to modify a string literal */
puts(s);
return 0;
}
One of the infinitely many possibly results is that the string literal
is actually modified, and the program prints "Jello, world".
The standard doesn't say that string literals are not modifiable. It
says that attempting to modify a string literal invokes undefined
behavior.
<OT>
Yes, _but_: from the point of view of gdb users and maintainers, they
may still consider it a gdb bug if, on a single platform, _any_ program
executes differently under gdb than it does when run normally. After all, the
underlying problem -- writing into r/o storage -- could be triggered from
an assembler program. And gdb doesn't have the same standards-contract
relationship with anything that a C implementation does.
It is, however, a separate issue from the fact that the program invokes UB.
</OT>
Richard Tobin wrote:
Pietro Cerutti <ga**@gahr.chwr ote:
>But if a string literal is - by definition - not modifiable, then how can it happen that GDB actually modifies it using strtok?
It's not modifiable in that you're not allowed to modify it. It's
not required that the implementation signal an error when you do
it. It's a constraint on you, not on the system.
My guess as to why you don't see an error with GDB is that the
debugger needs the text segment to be writable, so that it can set
breakpoints.
To get an error with gcc, add "-Wwrite-strings" to the command. No
quote chars used.
--
<http://www.cs.auckland .ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfoc us.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net
--
Posted via a free Usenet account from http://www.teranews.com This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Naveen Parihar |
last post by:
I've a binary that runs on most of our servers but segfaults on one of
the servers. Further, even on this specific machine, the binary runs
successfully sometimes but segfaults most of the time. While trying to
debug this behaviour using gdb, I found out that the segfaults occurs
randomly at different places in the code. For example, once a pointer
was not initiallized to NULL (it was pointing to x8 insterad of x0) in
a constructor, even...
|
by: Fatih Gey |
last post by:
Hi,
.. following causes a segfault. .. didn't
know why ?!
int main() {
char name;
strcpy (name, "ab8bc8cd8ed");
char cur;
|
by: ern |
last post by:
I'm using strtok( ) to capture lines of input. After I call
"splitCommand", I call strtok( ) again to get the next line. Strtok( )
returns NULL (but there is more in the file...). That didn't happen
before 'splitCommands' entered the picture. The problem is in
splitCommands( ) somehow modifying the pointer, but I HAVE to call that
function. Is there a way to make a copy of it or something ?
/* HERE IS MY CODE */
char *...
|
by: bofh1234 |
last post by:
I need to delimit a string. The delimiters are a semicolon and comma.
When I run the program I get a segmentation fault on the first strtok.
I followed the examples of others and from my old C books, but I can't
seem to find the problem. The accesslist has a format of
20,45;22,44;46,28;99,43,etc. What am I doing wrong?
Thanks,
#include <sys/signal.h>
#include <messages.h>
|
by: hu |
last post by:
hi, everybody!
I'm testing the fuction of strtok(). The environment is WinXP, VC++6.0.
Program is simple, but mistake is confusing. First, the below code can get
right outcome:"ello world, hello dreams."
#include <stdafx.h>
#include <string.h>
#include <stdio.h>
int main()
| |
by: Michael |
last post by:
Hi,
I have a proble I don't understand when using strtok(). It seems that if I
make a call to strtok(), then make a call to another function that also
makes use of strtok(), the original call is somehow confused or upset.
I have the following code, which I am using to tokenise some input which is
in th form x:y:1.2:
int tokenize_input(Sale *sale, char *string){
|
by: nomad5000 |
last post by:
Hi everybody!
I'm having trouble using strtok to fill a matrix with int nrs. from a file.
the code that is not working is the following:
#include <iostream>
#include <fstream>
#include <string>
#include <stdlib.h>
using namespace std;
|
by: magicman |
last post by:
can anyone point me out to its implementation in C before I roll my
own.
thx
|
by: Pilcrow |
last post by:
Here is a quick program, together with its output, that illustrates what
I consider to be a deficiency of the standard function strtok from
<string.h>:
I am using
C:\>gcc --version
gcc (GCC) 3.4.5 (mingw special)
I would like there to be a default, to be returned when two delimiters
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |