Always abort on allocation when out-of-memory (#2129)

Co-authored-by: Otto winter <otto@otto-winter.com>
This commit is contained in:
Oxan van Leeuwen 2021-08-10 10:46:46 +02:00 committed by GitHub
parent f26767b65e
commit c6c2842bdb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 3 deletions

View File

@ -14,7 +14,7 @@ const Color COLOR_OFF(0, 0, 0, 0);
const Color COLOR_ON(255, 255, 255, 255); const Color COLOR_ON(255, 255, 255, 255);
void DisplayBuffer::init_internal_(uint32_t buffer_length) { void DisplayBuffer::init_internal_(uint32_t buffer_length) {
this->buffer_ = new uint8_t[buffer_length]; this->buffer_ = new (std::nothrow) uint8_t[buffer_length];
if (this->buffer_ == nullptr) { if (this->buffer_ == nullptr) {
ESP_LOGE(TAG, "Could not allocate buffer for display!"); ESP_LOGE(TAG, "Could not allocate buffer for display!");
return; return;

View File

@ -275,8 +275,8 @@ void Nextion::upload_tft() {
} else { } else {
#endif #endif
ESP_LOGD(TAG, "Allocating buffer size %d, Heap size is %u", chunk_size, ESP.getFreeHeap()); ESP_LOGD(TAG, "Allocating buffer size %d, Heap size is %u", chunk_size, ESP.getFreeHeap());
this->transfer_buffer_ = new uint8_t[chunk_size]; this->transfer_buffer_ = new (std::nothrow) uint8_t[chunk_size];
if (!this->transfer_buffer_) { // Try a smaller size if (this->transfer_buffer_ == nullptr) { // Try a smaller size
ESP_LOGD(TAG, "Could not allocate buffer size: %d trying 4096 instead", chunk_size); ESP_LOGD(TAG, "Could not allocate buffer size: %d trying 4096 instead", chunk_size);
chunk_size = 4096; chunk_size = 4096;
ESP_LOGD(TAG, "Allocating %d buffer", chunk_size); ESP_LOGD(TAG, "Allocating %d buffer", chunk_size);

View File

@ -344,6 +344,15 @@ async def to_code(config):
else: else:
cg.add_library(lib, None) cg.add_library(lib, None)
if CORE.is_esp8266:
# Arduino 2 has a non-standards conformant new that returns a nullptr instead of failing when
# out of memory and exceptions are disabled. Since Arduino 2.6.0, this flag can be used to make
# new abort instead. Use it so that OOM fails early (on allocation) instead of on dereference of
# a NULL pointer (so the stacktrace makes more sense), and for consistency with Arduino 3,
# which always aborts if exceptions are disabled.
# For cases where nullptrs can be handled, use nothrow: `new (std::nothrow) T;`
cg.add_build_flag("-DNEW_OOM_ABORT")
cg.add_build_flag("-Wno-unused-variable") cg.add_build_flag("-Wno-unused-variable")
cg.add_build_flag("-Wno-unused-but-set-variable") cg.add_build_flag("-Wno-unused-but-set-variable")
cg.add_build_flag("-Wno-sign-compare") cg.add_build_flag("-Wno-sign-compare")