Skip to content

Modifying global char[] initialized with dup of string literal causes segfault #21194

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
pbackus opened this issue Apr 10, 2025 · 1 comment

Comments

@pbackus
Copy link
Contributor

pbackus commented Apr 10, 2025

As of DMD v2.111.0, compiling and executing the following program causes a segmentation fault:

char[] a = "foo".dup;

void main()
{
    a[0] = 'b'; // Error: program killed by signal 11
}

Examining the compiled code with objdump reveals that this happens because the data for the array is stored in read-only memory, even though the array's element type is mutable:

$ objdump -d -j .rodata.str1.1 bug.o

bug.o:     file format elf64-x86-64


Disassembly of section .rodata.str1.1:

0000000000000000 <_TMP0>:
   0:	66 6f 6f 00                                         foo.

If the array is initialized with an array literal (['f', 'o', 'o']) instead of a .duped string literal, the program runs successfully to completion.

@ibuclaw
Copy link
Member

ibuclaw commented Apr 10, 2025

Not a regression. The same happens with DMD 2.076.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants