DescentDevelopers / Descent3

Descent 3 by Outrage Entertainment
GNU General Public License v3.0
2.88k stars 251 forks source link

Fix undefined behavior in memory allocation #626

Closed sebholt closed 1 month ago

sebholt commented 1 month ago

Pull Request Type

Description

Currently mem_rmalloc uses std::malloc for memory allocation and the deallocation is performed in mem_free which calls ::free. This does seem to work but is undefined behavior because memory allocated with std::malloc should be freed with std::free not ::free.

We should use either

The mem_malloc/mem_free functions already use the C interface so it makes sense to let mem_rmalloc simply call mem_malloc instead of std::malloc directly. There's also no speed difference because everything is inline or a macro.

This changes the mem_rmalloc function to call mem_malloc instead of std::malloc to match the mem_free call.

Related Issues

Screenshots (if applicable)

Checklist

Additional Comments

pzychotic commented 1 month ago

LGTM But since this code was just recently introduced by @jengelh, I would like to get his opinion before merging.

sebholt commented 1 month ago

Ok. I've updated the PR description to make the issue more clear.

jengelh commented 1 month ago

It's a sound argument.

pzychotic commented 1 month ago

Thanks both of you!