travisvroman / kohi

A game engine made as part of the Kohi Game Engine series on YouTube (and Twitch!), where we make a game engine from the ground up using C and Vulkan.
https://kohiengine.com
Apache License 2.0
946 stars 92 forks source link

[BUG] darray_push and darray_insert_at create UB when called on array named "temp" #207

Open beewyka819 opened 6 months ago

beewyka819 commented 6 months ago

Describe the bug When using a darray named "temp", darray_push and darray_insert_at cause UB as the "temp" variable within the macro overrides the darray, resulting in the incorrect memory address being passed to the functions as the array. This causes the memory copies to write to invalid addresses as well as similar problems with the darray_length_set and potential darray_expand calls.

To Reproduce Steps to reproduce the behavior:

  1. Create a darray named "temp"
  2. Call darray_insert_at and/or darray_push

Expected behavior "temp" is a reasonable name to expect for darray variables. It should not be a special edge case. This should work as any other darray name does.

Fix proposal A simple way to fix this is to just make the temp variable in the macro. Perhaps something like _darray_insert_at_temp_value and _darray_push_temp_value respectively. Either that or obfuscate them as nonsense names, but that's less readable.

Relevant Lines https://github.com/travisvroman/kohi/blob/873e3a9ccf91dcda626592901c19408256ad51f5/engine/src/containers/darray.h#L124 and https://github.com/travisvroman/kohi/blob/873e3a9ccf91dcda626592901c19408256ad51f5/engine/src/containers/darray.h#L148

tstanisl commented 2 months ago

Maybe replace temp with _temp ## __LINE__.