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

Catch and avoid using CHIPMem.h uninitialized #2918

Merged
merged 7 commits into from
Oct 5, 2020

Conversation

kpschoedel
Copy link
Contributor

Problem

Some code calls Platform::Memory…() allocation functions without
having first called Platform::MemoryInit(). This is easy to miss because
it silently works for the CHIP_CONFIG_MEMORY_MGMT_MALLOC configuration.

Summary of Changes

  • In debug builds, have Platform::Memory…() verify that
    Platform::MemoryInit() has been called.

  • Fix some existing tests to call Platform::MemoryInit().

fixes #2899 CHIPMem.h is sometimes used uninitialized

#### Problem

Some code calls `Platform::Memory…()` allocation functions without
having first called `Platform::MemoryInit()`. This is easy to miss because
it silently works for the `CHIP_CONFIG_MEMORY_MGMT_MALLOC` configuration.

#### Summary of Changes

- In debug builds, have `Platform::Memory…()` verify that
  `Platform::MemoryInit()` has been called.

- Fix some existing tests to call `Platform::MemoryInit()`.

fixes project-chip#2899

#else

static int memoryInitialized = 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be atomic in builds w/ locking?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, good point. And should also check against calling Init while Init'd and Shutdown while shut down.

@mlepage-google
Copy link
Contributor

mlepage-google commented Sep 30, 2020

Possibly a good use for the "nifty counter" (AKA "Schwarz counter") idiom for initializing static objects before first use (as used by iostream to initialize cout)? https://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Nifty_Counter
[Just ignore if this is C (not C++) code.]

@BroderickCarlin
Copy link
Contributor

@kpschoedel can you rebase? should be good to go after that!

@andy31415 andy31415 merged commit f788937 into project-chip:master Oct 5, 2020
@kpschoedel kpschoedel deleted the x2899-mem-init branch October 5, 2020 15:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

CHIPMem.h is sometimes used uninitialized
7 participants