Skip to content

Bugfix: prevent reference counting from accessing freed memory #238

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

Closed
wants to merge 3 commits into from

Conversation

alranel
Copy link
Contributor

@alranel alranel commented May 12, 2022

The BLELocalAttribute and BLERemoteAttribute classes implement a rudimentary reference counting mechanism:

int BLELocalAttribute::retain()
{
  _refCount++;

  return _refCount;
}

int BLELocalAttribute::release()
{
  _refCount--;

  return _refCount;
}

However, _refCount is an instance variable so every call to release() on a pointer that was already deleted will try to access freed memory, defeating the purpose of refcounting. A more modern solution would be to replace this with std::shared_ptr, but this pull request provides a minimalistic fix based on making _refCount a static class variable that lives outside the lifecycle of the instance.

I noticed this issue after merging #237.

@alranel alranel added type: imperfection Perceived defect in any part of project topic: code Related to content of the project itself labels May 12, 2022
@alranel
Copy link
Contributor Author

alranel commented May 12, 2022

Hm, this does not work for arduino:megaavr:uno2018 (Uno WiFi rev2) as there's no std::map there.

@facchinm
Copy link
Contributor

Temporarily close since it looks like it creates another leak (and is not supported on AVR), keeping db2e240 as part of #237

@facchinm facchinm closed this May 24, 2022
@per1234 per1234 added the conclusion: invalid Issue/PR not valid label May 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
conclusion: invalid Issue/PR not valid topic: code Related to content of the project itself type: imperfection Perceived defect in any part of project
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants