DFHack / df-structures

Dwarf Fortress data structure descriptions
https://github.com/DFHack/dfhack
114 stars 80 forks source link

zero-init otherwise uninitialized unions #759

Closed myk002 closed 1 month ago

myk002 commented 1 month ago

as per Discord discussion: https://discord.com/channels/793331351645323264/807444047215853578/1241770443371839488

This came up because the new df::job::job_spec union was not getting initialized on construction, even if init-value attributes were added to the fields.

This appears to be because the union is a mixture of plain data types (uint32_t) and bitfield types, which have non-default constructors. The end result is that no constructors get called and the resulting value is garbage.

This change zero-inits union structures. Existing code that does set the union fields will still work since the fields are set in the constructor for the containing class, which executes after the memset introduced here.

myk002 commented 1 month ago

testing shows this helps and does not seem to have any drawbacks; merging