Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix undefined behavior in memory allocation #626

Merged
merged 1 commit into from
Oct 12, 2024

Conversation

sebholt
Copy link
Contributor

@sebholt sebholt commented Oct 7, 2024

Pull Request Type

  • GitHub Workflow changes
  • Documentation or Wiki changes
  • Build and Dependency changes
  • Runtime changes
    • Render changes
    • Audio changes
    • Input changes
    • Network changes
    • Other changes

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 C interface: ::malloc and ::free, or
  • C++ interface: std::malloc and std::free.

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

  • I have tested my changes locally and verified that they work as intended.
  • I have documented any new or modified functionality.
  • I have reviewed the changes to ensure they do not introduce any unnecessary complexity or duplicate code.
  • I understand that by submitting this pull request, I am agreeing to license my contributions under the project's license.

Additional Comments

`mem_rmalloc` should use `mem_malloc` instead of `std::malloc`
to match the `mem_free` deallocation call.
@sebholt sebholt changed the title Let mem_rmalloc call mem_malloc instead of std::malloc Fix undefined behavior in memory allocation Oct 12, 2024
@pzychotic
Copy link
Contributor

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

@sebholt
Copy link
Contributor Author

sebholt commented Oct 12, 2024

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

@jengelh
Copy link
Contributor

jengelh commented Oct 12, 2024

It's a sound argument.

@pzychotic
Copy link
Contributor

Thanks both of you!

@pzychotic pzychotic merged commit 512d441 into DescentDevelopers:main Oct 12, 2024
10 checks passed
@sebholt sebholt deleted the mem_rmalloc_fix branch October 12, 2024 17:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants