Bug 20664 – Compiler generates string with bad pointer

Status
NEW
Severity
normal
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2020-03-11T02:26:51Z
Last change time
2024-12-13T19:07:30Z
Assigned to
No Owner
Creator
andy-hanson
Moved to GitHub: dmd#17944 →

Comments

Comment #0 by andy-hanson — 2020-03-11T02:26:51Z
``` import std.stdio : writeln; void main() { immutable Wrapper* a = getFieldName!S; immutable Wrapper* b = getFieldName!(immutable S); writeln(a, " ", b); writeln(a.value.ptr, " ", b.value.ptr); writeln(a.value.length, " ", b.value.length); writeln(a.value, " ", b.value); } struct S { int a; } template getFieldName(T) { immutable Wrapper* getFieldName = getFieldNameInner!(T.tupleof); } template getFieldNameInner(fields...) { immutable Wrapper w = Wrapper(fields[0].stringof); immutable Wrapper* getFieldNameInner = &w; } struct Wrapper { immutable string value; } ``` This code should print two arbitrary pointer values, then 1, then "a". If the variable `b` is removed, it does. When `b` is present, `a.value.ptr` is different and identical to `b.value.ptr`, and it segfaults when trying to print `a.value`. The issue doesn't occur if `b` uses just `getFieldName!S` instead of `getFieldName!(immutable S)` or it uses some other struct instead. Somehow, passing both `S` and `immutable S` to the template causes `a` and `b` to share the same `Wrapper*`, but the string it contains has an invalid pointer.
Comment #1 by robert.schadek — 2024-12-13T19:07:30Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/17944 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB