Thread overview
[Issue 821] New: segfault with char array copy
Jan 09, 2007
d-bugmail
Jan 09, 2007
d-bugmail
Jan 09, 2007
d-bugmail
Jan 09, 2007
d-bugmail
[Issue 821] segfault with char array copy; mistaken samples in doc
Jan 10, 2007
d-bugmail
Jan 11, 2007
d-bugmail
January 09, 2007
http://d.puremagic.com/issues/show_bug.cgi?id=821

           Summary: segfault with char array copy
           Product: D
           Version: 1.00
          Platform: PC
        OS/Version: Linux
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla@digitalmars.com
        ReportedBy: ch@lathspell.de


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 +++


-- 

January 09, 2007
http://d.puremagic.com/issues/show_bug.cgi?id=821





------- Comment #1 from afb@algonet.se  2007-01-09 02:56 -------
String constants are read-only on Linux, you need a .dup in your code (COW).


-- 

January 09, 2007
http://d.puremagic.com/issues/show_bug.cgi?id=821


bugzilla@digitalmars.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |INVALID




------- Comment #2 from bugzilla@digitalmars.com  2007-01-09 03:35 -------
String literals are read only, hence attempting to write to them will produce undefined behavior.


-- 

January 09, 2007
http://d.puremagic.com/issues/show_bug.cgi?id=821





------- Comment #3 from ch@lathspell.de  2007-01-09 07:50 -------
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-


-- 

January 10, 2007
http://d.puremagic.com/issues/show_bug.cgi?id=821


lio@lunesu.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |REOPENED
          Component|DMD                         |www.digitalmars.com
         Resolution|INVALID                     |
            Summary|segfault with char array    |segfault with char array
                   |copy                        |copy; mistaken samples in
                   |                            |doc




------- Comment #4 from lio@lunesu.com  2007-01-10 01:00 -------
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);


-- 

January 11, 2007
http://d.puremagic.com/issues/show_bug.cgi?id=821





------- Comment #5 from ch@lathspell.de  2007-01-10 18:33 -------
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-


--