821 – segfault with char array copy; mistaken samples in doc

D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 821 - segfault with char array copy; mistaken samples in doc
Summary: segfault with char array copy; mistaken samples in doc
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dlang.org (show other issues)
Version: D1 (retired)
Hardware: x86 Linux
: P2 normal
Assignee: Walter Bright
URL:
Keywords:
Depends on:
Blocks:
Reported: 2007年01月09日 02:00 UTC by Christian Hammers
Modified: 2014年02月15日 13:13 UTC (History)
0 users

See Also:


Attachments
Add an attachment (proposed patch, testcase, etc.)

Note You need to log in before you can comment on or make changes to this issue.
Description Christian Hammers 2007年01月09日 02:00:55 UTC
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 Anders F Bj 2007年01月09日 02:56:17 UTC
String constants are read-only on Linux, you need a .dup in your code (COW). 
Comment 2 Walter Bright 2007年01月09日 03:35:57 UTC
String literals are read only, hence attempting to write to them will produce undefined behavior.
Comment 3 Christian Hammers 2007年01月09日 07:50:33 UTC
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 Lionello Lunesu 2007年01月10日 01:00:37 UTC
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 Christian Hammers 2007年01月10日 18:33:25 UTC
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-
Comment 6 Walter Bright 2007年02月12日 03:37:56 UTC
Fixed DMD 1.005


AltStyle によって変換されたページ (->オリジナル) /