diff --git a/src/resources/i2c_esp32.cc b/src/resources/i2c_esp32.cc index 67702c9cf..ba7c8f500 100644 --- a/src/resources/i2c_esp32.cc +++ b/src/resources/i2c_esp32.cc @@ -135,9 +135,10 @@ static Object* write_i2c(Process* process, I2cResourceGroup* i2c, int i2c_addres const uint8* data = buffer.address(); word length = buffer.length(); - if (!esp_ptr_internal(data)) { - // Copy buffer to malloc heap, if the buffer is not in memory. - uint8* copy = unvoid_cast(malloc(length)); + if (length > 0 && !esp_ptr_internal(data)) { + // Copy buffer to internal malloc heap, if the buffer is not in memory. + const int caps_flags = MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT; + uint8* copy = unvoid_cast(heap_caps_malloc(length, caps_flags)); if (copy == null) FAIL(MALLOC_FAILED); memcpy(copy, data, length); data = copy; diff --git a/src/resources/uart_esp32.cc b/src/resources/uart_esp32.cc index 04864fb48..3cf138925 100644 --- a/src/resources/uart_esp32.cc +++ b/src/resources/uart_esp32.cc @@ -822,12 +822,12 @@ PRIMITIVE(create) { } const int caps_flags = MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT; - init.rx_buffer = static_cast(heap_caps_malloc(rx_buffer_size, caps_flags)); + init.rx_buffer = unvoid_cast(heap_caps_malloc(rx_buffer_size, caps_flags)); if (!init.rx_buffer) { FAIL(MALLOC_FAILED); } - init.tx_buffer = static_cast(heap_caps_malloc(tx_buffer_size, caps_flags)); + init.tx_buffer = unvoid_cast(heap_caps_malloc(tx_buffer_size, caps_flags)); if (!init.tx_buffer) { FAIL(MALLOC_FAILED); }