www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 22936] New: Compiler creates char[] pointing to immutable

https://issues.dlang.org/show_bug.cgi?id=22936

          Issue ID: 22936
           Summary: Compiler creates char[] pointing to immutable memory
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: jephthah_divots aleeas.com

---
char[] s = "hello".dup;
void main () {
    s[0] = 'X'; // segfault
}
---
---
char[] h () {
    return ['h'];
}
char[] s = h() ~ "i";
void main () {
    s[0] = 'X'; // segfault
}
---

These bugs seem to be caused due to CTFE evaluating expresions to a diferent
type than what would be produced at runtime:
---
char[] h () {
    return ['h'];
}

void main () {
    // Literals seem to be evaluated similarly to runtime
    pragma(msg, (['h'] ~ "i").ptr); // &['h', 'i'][0]
    pragma(msg, (h()   ~ "i").ptr); // &"hi"[0]
}
---

--
Mar 25 2022