www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 22171] New: Segfault in generated __xtoHash, related to unions

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

          Issue ID: 22171
           Summary: Segfault in generated __xtoHash, related to unions
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: kinke gmx.net

```
module current;

class C
{
    // needed for `S` to get a `__xtoHash`
    int opCmp(const C rhs) const { return 1; }
}

struct S
{
    C c;
    union
    {
        string str;
        S[] ss;
    }
    ulong[10_000] large;
}

void main()
{
    S s = {str:
"wsdpfijsdpfijspfklmxcö,,msdfkjsmndfpösdmnfpoisdmfüosijfposidj"};
    const h = typeid(S).getHash(&s);
}
```

The code above segfaults with DMD v2.097.1, with the following backtrace:


const ()

const ()


const ()

const ()

const(void*), const(ulong)) ()

const ()

const ()


const ()


I guess it's trying to compute the hashes of *all* members, so both `str` and
`ss`, with `ss` likely representing invalid memory if the `str` member with way
smaller element size (char vs. S) was set.

--
Aug 02 2021