Skip to content

Commit

Permalink
Fix Platform::New to return nullptr on failure
Browse files Browse the repository at this point in the history
#### Problem

`Platform::New()` uses placment `new` into a possibly-null pointer.

#### Summary of Changes

Check it.

Fixes project-chip#4998 platform::New will crash or do other bad things on allocation failure
  • Loading branch information
kpschoedel committed Feb 24, 2021
1 parent 2891d6b commit ffb7484
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/lib/support/CHIPMem.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,12 @@ extern void MemoryFree(void * p);
template <typename T, typename... Args>
inline T * New(Args &&... args)
{
return new (MemoryAlloc(sizeof(T))) T(std::forward<Args>(args)...);
void * p = MemoryAlloc(sizeof(T));
if (p != nullptr)
{
return new (p) T(std::forward<Args>(args)...);
}
return nullptr;
}

/**
Expand Down

0 comments on commit ffb7484

Please sign in to comment.