Bug 821 – segfault with char array copy; mistaken samples in doc
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dlang.org
Product
D
Version
D1 (retired)
Platform
x86
OS
Linux
Creation time
2007-01-09T02:00:00Z
Last change time
2014-02-15T13:13:42Z
Assigned to
bugzilla
Creator
ch
Comments
Comment #0 by ch — 2007-01-09T02:00:55Z
Hello
I tried the example from the d homepage but it segfaults using both DMD and GDC (in the binary versions that are linked from the homepage).
My host is a AMD Athlon64 running in i386 chroot using 'Debian sid'. Other D programs run fine.
$ cat segfault.d
import std.stdio;
int main(char[][] args) {
char[] s1 = "hello world";
char[] s2 = "goodbye ";
s2[8..13] = s1[6..11]; // s2 is "goodbye world"
return 0;
}
$ ltrace ./segfault
...
calloc(1, 68) = 0x8061150
mmap(0, 0x100000, 3, 34, -1) = 0xf7d06000
calloc(2050, 4) = 0x8061198
calloc(2050, 4) = 0x80631a8
calloc(2050, 4) = 0x80651b8
malloc(256) = 0x80671c8
memset(0x80671c8, '\013', 256) = 0x80671c8
realloc(NULL, 4) = 0x80672d0
memmove(0x80672d4, 0x80672d0, 0, 0, 0x8061008) = 0x80672d4
memset(0x80671c8, '\n', 16) = 0x80671c8
pthread_mutex_lock(0x80610a8, 0x8061008, 0x1f35850, 0x8061008, 0x805ec58) = 0
calloc(2050, 4) = 0x80672e0
pthread_mutex_unlock(0x80610a8, 0x80672d8, 8200, 0x8061150, 0xf7d06ff0) = 0
pthread_mutex_lock(0x80610a8, 0x8061008, 0x80672d8, 0x8061008, 0x805f814) = 0
pthread_mutex_unlock(0x80610a8, 0x80610a8, 0x8061008, 0x80672d8, 0x8061008) = 0
pthread_attr_init(0xf7d07f20, 0x8061008, 0xffcd551c, 0x805959c, 0) = 0
pthread_mutex_init(0xf7d07f40, 0, 0x8061008, 0xffcd551c, 0x805959c) = 0
pthread_cond_init(0xf7d07f58, 0, 0x8061008, 0xffcd551c, 0x805959c) = 0
pthread_self(0x805ec58, 0x805b1c9, 0x805d2ec, 0x8061008, 1) = 0xf7e066c0
sigfillset(0xffcd553c) = 0
sigaction(10, 0xffcd5538, NULL) = 0
sigaction(12, 0xffcd5538, NULL) = 0
sem_init(0x8060ac8, 0, 0, 0x805b1c9, 0x805d2ec) = 0
malloc(8) = 0x80692f0
memset(0xf7d08f80, '\000', 124) = 0xf7d08f80
memset(0xf7d08f00, '\000', 124) = 0xf7d08f00
strlen("./segfault") = 10
memcpy(0x805a0bc, "world", 5 <unfinished ...>
--- SIGSEGV (Segmentation fault) ---
+++ killed by SIGSEGV +++
Comment #1 by afb — 2007-01-09T02:56:17Z
String constants are read-only on Linux, you need a .dup in your code (COW).
Comment #2 by bugzilla — 2007-01-09T03:35:57Z
String literals are read only, hence attempting to write to them will produce undefined behavior.
Comment #3 by ch — 2007-01-09T07:50:33Z
So many examples from http://digitalmars.com/d/cppstrings.html do not work at all in Linux?
Or not at all with the latest language specs?
This is sad. I hope the rest of the advertised features from the example pages do at least work without unexpected segfaults...
bye,
-christian-
Comment #4 by lio+bugzilla — 2007-01-10T01:00:37Z
I've changed the component to "www.digitalmars.com", since the samples are definitely wrong.
http://digitalmars.com/d/cppstrings.html
#Copying a String
#Filling a String
These two need .dup added to prevent modifying read-only strings.
#Conversions to C Strings
The second sample needs foo(s1.ptr);
Comment #5 by ch — 2007-01-10T18:33:25Z
Hello
Do you consider this a bug in the Linux compiler or just one in the documentation?
As it is possible to modify a string made out of character constants like
char[] s3 = ['H', 'e', 'l', 'l', 'o'];
but not
char[] s3 = "Hello";
I would argue that this is a bug.
The language description on http://www.digitalmars.com/d/arrays.html does not
mention the existance of "read-only" arrays neither. If they was supposed to
exists there should be at least a boolean .readonly attribute so that functions
that accepts char[] and even check their length can decide if they may or may not be able to modify the provided char[] buffer.
So at least document this in the language reference, too, as the easy string handling through arrays with builtin length attribute will surely be one of the things C programmers attracts...
bye,
-christian-
bye,
-christian-