Bug 1766 – segfault writing to a string variable...

Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Linux
Creation time
2008-01-02T17:17:00Z
Last change time
2015-06-09T05:14:50Z
Assigned to
dvdfrdmn
Creator
funisher

Comments

Comment #0 by funisher — 2008-01-02T17:17:37Z
gentoo gdc 0.24, x86_64, no cflags here is a simple test case: void main() { string lala = " "; lala[0] = '1'; } however, reading is fine... void main() { string lala; lala = "-----"; printf("%c %d", lala[0], lala.length); // prints '- 5' }
Comment #1 by ddparnell — 2008-01-02T19:25:45Z
I don't think this is a bug. Its working as I would expect it to. The identifier 'lala' is not a VARIABLE, its a reference to a string literal and literals are in read-only memory on Linux, thus you can't write to it. The segfault is the operating system's reaction to the attempt. Use this instead ... void main() { string lala = " ".dup; // Take a writable copy of the literal. lala[0] = '1'; }
Comment #2 by bugzilla — 2008-01-02T20:57:06Z
This is not a bug, string literals are read-only.
Comment #3 by funisher — 2008-01-03T17:05:28Z
[email protected] wrote: > http://d.puremagic.com/issues/show_bug.cgi?id=1766 > > > [email protected] changed: > > What |Removed |Added > ---------------------------------------------------------------------------- > Status|NEW |RESOLVED > Resolution| |INVALID > > > > > ------- Comment #2 from [email protected] 2008-01-02 20:57 ------- > This is not a bug, string literals are read-only. > > Ok, I understand it's not a bug and it makes perfect sense, but why did the same exact code work as I originally expected with dmd-2.003? shouldn't that also be read-only? Is it a bug that dmd works?