kohi icon indicating copy to clipboard operation
kohi copied to clipboard

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

Open beewyka819 opened this issue 1 year ago • 1 comments

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

beewyka819 avatar Dec 31 '23 03:12 beewyka819

Maybe replace temp with _temp ## __LINE__.

tstanisl avatar Apr 24 '24 19:04 tstanisl