Merge branch 'fix/initialize_s_spi_transaction_to_0_v5.2' into 'release/v5.2'

fix(openthread): replace all malloc calls with calloc to avoid hidden uninitialized memory issues (v5.2)

See merge request espressif/esp-idf!49971
This commit is contained in:
Zhang Wen Xu
2026-06-24 10:45:42 +00:00
7 changed files with 19 additions and 20 deletions
@@ -38,7 +38,7 @@ esp_err_t esp_openthread_platform_workflow_register(esp_openthread_update_func u
esp_openthread_platform_workflow_t *current_workflow = s_workflow_list; esp_openthread_platform_workflow_t *current_workflow = s_workflow_list;
esp_openthread_platform_workflow_t *before_workflow = NULL; esp_openthread_platform_workflow_t *before_workflow = NULL;
esp_openthread_platform_workflow_t *add_workflow = esp_openthread_platform_workflow_t *add_workflow =
static_cast<esp_openthread_platform_workflow_t *>(malloc(sizeof(esp_openthread_platform_workflow_t))); static_cast<esp_openthread_platform_workflow_t *>(calloc(1, sizeof(esp_openthread_platform_workflow_t)));
ESP_RETURN_ON_FALSE(add_workflow != NULL, ESP_ERR_NO_MEM, OT_PLAT_LOG_TAG, ESP_RETURN_ON_FALSE(add_workflow != NULL, ESP_ERR_NO_MEM, OT_PLAT_LOG_TAG,
"Failed to alloc memory for esp_openthread_workflow"); "Failed to alloc memory for esp_openthread_workflow");
strncpy(add_workflow->name, name, name_len); strncpy(add_workflow->name, name, name_len);
@@ -86,7 +86,7 @@ esp_err_t esp_openthread_host_rcp_spi_init(const esp_openthread_platform_config_
{ {
esp_err_t ret = ESP_OK; esp_err_t ret = ESP_OK;
s_spi_config = heap_caps_malloc(sizeof(esp_openthread_spi_slave_config_t), MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT); s_spi_config = heap_caps_calloc(1, sizeof(esp_openthread_spi_slave_config_t), MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
ESP_GOTO_ON_FALSE(s_spi_config != NULL, ESP_ERR_NO_MEM, err, OT_PLAT_LOG_TAG, ESP_GOTO_ON_FALSE(s_spi_config != NULL, ESP_ERR_NO_MEM, err, OT_PLAT_LOG_TAG,
"failed to allocate memory for SPI transaction on internal heap"); "failed to allocate memory for SPI transaction on internal heap");
memcpy(s_spi_config, &(config->host_config.spi_slave_config), sizeof(esp_openthread_spi_slave_config_t)); memcpy(s_spi_config, &(config->host_config.spi_slave_config), sizeof(esp_openthread_spi_slave_config_t));
@@ -104,11 +104,11 @@ esp_err_t esp_openthread_host_rcp_spi_init(const esp_openthread_platform_config_
gpio_set_pull_mode(s_spi_config->bus_config.sclk_io_num, GPIO_PULLUP_ONLY); gpio_set_pull_mode(s_spi_config->bus_config.sclk_io_num, GPIO_PULLUP_ONLY);
gpio_set_pull_mode(s_spi_config->slave_config.spics_io_num, GPIO_PULLUP_ONLY); gpio_set_pull_mode(s_spi_config->slave_config.spics_io_num, GPIO_PULLUP_ONLY);
s_spi_transaction = heap_caps_malloc(sizeof(spi_slave_transaction_t), MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT); s_spi_transaction = heap_caps_calloc(1, sizeof(spi_slave_transaction_t), MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
ESP_GOTO_ON_FALSE(s_spi_transaction != NULL, ESP_ERR_NO_MEM, err, OT_PLAT_LOG_TAG, "failed to allocate memory for SPI transaction on internal heap"); ESP_GOTO_ON_FALSE(s_spi_transaction != NULL, ESP_ERR_NO_MEM, err, OT_PLAT_LOG_TAG, "failed to allocate memory for SPI transaction on internal heap");
s_pending_transaction = heap_caps_malloc(sizeof(pending_transaction_t), MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT); s_pending_transaction = heap_caps_calloc(1, sizeof(pending_transaction_t), MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
ESP_GOTO_ON_FALSE(s_pending_transaction != NULL, ESP_ERR_NO_MEM, err, OT_PLAT_LOG_TAG, "failed to allocate memory for pending transaction on internal heap"); ESP_GOTO_ON_FALSE(s_pending_transaction != NULL, ESP_ERR_NO_MEM, err, OT_PLAT_LOG_TAG, "failed to allocate memory for pending transaction on internal heap");
s_rx_dma_buf = heap_caps_malloc(SPI_SLAVE_RX_DMA_BUF_SIZE, MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL); s_rx_dma_buf = heap_caps_calloc(1, SPI_SLAVE_RX_DMA_BUF_SIZE, MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL);
ESP_GOTO_ON_FALSE(s_rx_dma_buf != NULL, ESP_ERR_NO_MEM, err, OT_PLAT_LOG_TAG, "failed to allocate memory for RX DMA buffer on internal heap"); ESP_GOTO_ON_FALSE(s_rx_dma_buf != NULL, ESP_ERR_NO_MEM, err, OT_PLAT_LOG_TAG, "failed to allocate memory for RX DMA buffer on internal heap");
s_spi_transaction->user = (void *)s_pending_transaction; s_spi_transaction->user = (void *)s_pending_transaction;
@@ -86,7 +86,7 @@ static void trel_browse_notifier(mdns_result_t *result)
result = result->next; result = result->next;
continue; continue;
} }
trel_txt = malloc(trel_txt_len); trel_txt = calloc(1, trel_txt_len);
ESP_RETURN_ON_FALSE(trel_txt != NULL, , OT_PLAT_LOG_TAG, "Failed to malloc buffer for TREL TXT"); ESP_RETURN_ON_FALSE(trel_txt != NULL, , OT_PLAT_LOG_TAG, "Failed to malloc buffer for TREL TXT");
size_t offset = 0; size_t offset = 0;
@@ -123,10 +123,9 @@ static void handle_trel_udp_recv(void *ctx, struct udp_pcb *pcb, struct pbuf *p,
uint64_t event_trel_rx = 1; uint64_t event_trel_rx = 1;
ESP_LOGD(OT_PLAT_LOG_TAG, "Receive from %s:%d", ip6addr_ntoa(&(addr->u_addr.ip6)), port); ESP_LOGD(OT_PLAT_LOG_TAG, "Receive from %s:%d", ip6addr_ntoa(&(addr->u_addr.ip6)), port);
ESP_GOTO_ON_FALSE(atomic_load(&s_recv_queue.used) < CONFIG_OPENTHREAD_TREL_BUFFER_SIZE, ESP_ERR_NO_MEM, exit, OT_PLAT_LOG_TAG, "trel receive buffer full!"); ESP_GOTO_ON_FALSE(atomic_load(&s_recv_queue.used) < CONFIG_OPENTHREAD_TREL_BUFFER_SIZE, ESP_ERR_NO_MEM, exit, OT_PLAT_LOG_TAG, "trel receive buffer full!");
source_addr = (otSockAddr *)malloc(sizeof(otSockAddr)); source_addr = (otSockAddr *)calloc(1, sizeof(otSockAddr));
ESP_GOTO_ON_FALSE(source_addr, ESP_ERR_NO_MEM, exit, OT_PLAT_LOG_TAG, "Failed to allocate buf for Thread TREL"); ESP_GOTO_ON_FALSE(source_addr, ESP_ERR_NO_MEM, exit, OT_PLAT_LOG_TAG, "Failed to allocate buf for Thread TREL");
memset(source_addr, 0, sizeof(otSockAddr));
source_addr->mPort = port; source_addr->mPort = port;
memcpy(&source_addr->mAddress.mFields.m32, addr->u_addr.ip6.addr, sizeof(addr->u_addr.ip6.addr)); memcpy(&source_addr->mAddress.mFields.m32, addr->u_addr.ip6.addr, sizeof(addr->u_addr.ip6.addr));
s_trel_receive_buffer[s_recv_queue.tail].source_addr = source_addr; s_trel_receive_buffer[s_recv_queue.tail].source_addr = source_addr;
@@ -180,7 +179,7 @@ esp_err_t esp_openthread_trel_process(otInstance *aInstance, const esp_openthrea
source_addr = s_trel_receive_buffer[s_recv_queue.head].source_addr; source_addr = s_trel_receive_buffer[s_recv_queue.head].source_addr;
if (recv_buf->next != NULL) { if (recv_buf->next != NULL) {
data_buf = (uint8_t *)malloc(recv_buf->tot_len); data_buf = (uint8_t *)calloc(1, recv_buf->tot_len);
if (data_buf) { if (data_buf) {
pbuf_copy_partial(recv_buf, data_buf, recv_buf->tot_len, 0); pbuf_copy_partial(recv_buf, data_buf, recv_buf->tot_len, 0);
} else { } else {
@@ -120,7 +120,7 @@ static void udp_recv_task(void *ctx)
memcpy(&message_info.mPeerAddr, ip_2_ip6(&task->addr)->addr, sizeof(message_info.mPeerAddr)); memcpy(&message_info.mPeerAddr, ip_2_ip6(&task->addr)->addr, sizeof(message_info.mPeerAddr));
if (recv_buf->next != NULL) { if (recv_buf->next != NULL) {
data_buf = (uint8_t *)malloc(recv_buf->tot_len); data_buf = (uint8_t *)calloc(1, recv_buf->tot_len);
if (data_buf != NULL) { if (data_buf != NULL) {
data_buf_to_free = data_buf; data_buf_to_free = data_buf;
pbuf_copy_partial(recv_buf, data_buf, recv_buf->tot_len, 0); pbuf_copy_partial(recv_buf, data_buf, recv_buf->tot_len, 0);
@@ -151,7 +151,7 @@ exit:
static void handle_udp_recv(void *ctx, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, uint16_t port) static void handle_udp_recv(void *ctx, struct udp_pcb *pcb, struct pbuf *p, const ip_addr_t *addr, uint16_t port)
{ {
udp_recv_task_t *task = (udp_recv_task_t *)malloc(sizeof(udp_recv_task_t)); udp_recv_task_t *task = (udp_recv_task_t *)calloc(1, sizeof(udp_recv_task_t));
const struct ip6_hdr *ip6_hdr = ip6_current_header(); const struct ip6_hdr *ip6_hdr = ip6_current_header();
#if CONFIG_LWIP_IPV4 #if CONFIG_LWIP_IPV4
const struct ip_hdr *ip4_hdr = ip4_current_header(); const struct ip_hdr *ip4_hdr = ip4_current_header();
@@ -389,7 +389,7 @@ static inline bool is_addr_ip6_any(const ip_addr_t *addr)
otError otPlatUdpSend(otUdpSocket *udp_socket, otMessage *message, const otMessageInfo *message_info) otError otPlatUdpSend(otUdpSocket *udp_socket, otMessage *message, const otMessageInfo *message_info)
{ {
udp_send_task_t *task = (udp_send_task_t *)malloc(sizeof(udp_send_task_t)); udp_send_task_t *task = (udp_send_task_t *)calloc(1, sizeof(udp_send_task_t));
otError error = OT_ERROR_NONE; otError error = OT_ERROR_NONE;
VerifyOrExit(task != NULL, error = OT_ERROR_NO_BUFS); VerifyOrExit(task != NULL, error = OT_ERROR_NO_BUFS);
task->pcb = (struct udp_pcb *)udp_socket->mHandle; task->pcb = (struct udp_pcb *)udp_socket->mHandle;
@@ -448,7 +448,7 @@ static void udp_multicast_join_leave_task(void *ctx)
otError otPlatUdpJoinMulticastGroup(otUdpSocket *socket, otNetifIdentifier netif_id, const otIp6Address *addr) otError otPlatUdpJoinMulticastGroup(otUdpSocket *socket, otNetifIdentifier netif_id, const otIp6Address *addr)
{ {
udp_multicast_join_leave_task_t *task = udp_multicast_join_leave_task_t *task =
(udp_multicast_join_leave_task_t *)malloc(sizeof(udp_multicast_join_leave_task_t)); (udp_multicast_join_leave_task_t *)calloc(1, sizeof(udp_multicast_join_leave_task_t));
otError error = OT_ERROR_NONE; otError error = OT_ERROR_NONE;
VerifyOrExit(task != NULL, error = OT_ERROR_NO_BUFS); VerifyOrExit(task != NULL, error = OT_ERROR_NO_BUFS);
@@ -467,7 +467,7 @@ exit:
otError otPlatUdpLeaveMulticastGroup(otUdpSocket *socket, otNetifIdentifier netif_id, const otIp6Address *addr) otError otPlatUdpLeaveMulticastGroup(otUdpSocket *socket, otNetifIdentifier netif_id, const otIp6Address *addr)
{ {
udp_multicast_join_leave_task_t *task = udp_multicast_join_leave_task_t *task =
(udp_multicast_join_leave_task_t *)malloc(sizeof(udp_multicast_join_leave_task_t)); (udp_multicast_join_leave_task_t *)calloc(1, sizeof(udp_multicast_join_leave_task_t));
otError error = OT_ERROR_NONE; otError error = OT_ERROR_NONE;
VerifyOrExit(task != NULL, error = OT_ERROR_NO_BUFS); VerifyOrExit(task != NULL, error = OT_ERROR_NO_BUFS);
@@ -85,7 +85,7 @@ esp_err_t SpiSpinelInterface::Enable(const esp_openthread_spi_host_config_t &spi
ESP_RETURN_ON_FALSE(m_event_fd >= 0, ESP_FAIL, OT_PLAT_LOG_TAG, "fail to get event fd"); ESP_RETURN_ON_FALSE(m_event_fd >= 0, ESP_FAIL, OT_PLAT_LOG_TAG, "fail to get event fd");
m_rx_dma_buf = (uint8_t *)heap_caps_malloc(kSPIFrameSize, MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL); m_rx_dma_buf = (uint8_t *)heap_caps_calloc(1, kSPIFrameSize, MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL);
ESP_RETURN_ON_FALSE(m_rx_dma_buf != nullptr, ESP_ERR_NO_MEM, OT_PLAT_LOG_TAG, "fail to alloc SPI RX DMA buffer"); ESP_RETURN_ON_FALSE(m_rx_dma_buf != nullptr, ESP_ERR_NO_MEM, OT_PLAT_LOG_TAG, "fail to alloc SPI RX DMA buffer");
ESP_LOGI(OT_PLAT_LOG_TAG, "spinel SPI interface initialization completed"); ESP_LOGI(OT_PLAT_LOG_TAG, "spinel SPI interface initialization completed");
@@ -103,7 +103,7 @@ void ReceiveDone(otInstance *aInstance, otRadioFrame *aFrame, otError aError)
{ {
esp_radio_spinel_idx_t idx = get_index_from_instance(aInstance); esp_radio_spinel_idx_t idx = get_index_from_instance(aInstance);
assert(s_esp_radio_spinel_callbacks[idx].receive_done); assert(s_esp_radio_spinel_callbacks[idx].receive_done);
uint8_t *frame = (uint8_t *)malloc(aFrame->mLength + 1); uint8_t *frame = (uint8_t *)calloc(1, aFrame->mLength + 1);
esp_ieee802154_frame_info_t frame_info; esp_ieee802154_frame_info_t frame_info;
if (frame) { if (frame) {
frame[0] = aFrame->mLength; frame[0] = aFrame->mLength;
@@ -123,14 +123,14 @@ void TransmitDone(otInstance *aInstance, otRadioFrame *aFrame, otRadioFrame *aAc
esp_radio_spinel_idx_t idx = get_index_from_instance(aInstance); esp_radio_spinel_idx_t idx = get_index_from_instance(aInstance);
assert(s_esp_radio_spinel_callbacks[idx].transmit_done && s_esp_radio_spinel_callbacks[idx].transmit_failed); assert(s_esp_radio_spinel_callbacks[idx].transmit_done && s_esp_radio_spinel_callbacks[idx].transmit_failed);
if (aError == OT_ERROR_NONE) { if (aError == OT_ERROR_NONE) {
uint8_t *frame = (uint8_t *)malloc(aFrame->mLength + 1); uint8_t *frame = (uint8_t *)calloc(1, aFrame->mLength + 1);
uint8_t *ack = nullptr; uint8_t *ack = nullptr;
if (frame) { if (frame) {
esp_ieee802154_frame_info_t ack_info; esp_ieee802154_frame_info_t ack_info;
frame[0] = aFrame->mLength; frame[0] = aFrame->mLength;
memcpy((void *)(frame + 1), aFrame->mPsdu, frame[0]); memcpy((void *)(frame + 1), aFrame->mPsdu, frame[0]);
if (aAckFrame) { if (aAckFrame) {
ack = (uint8_t *)malloc(aAckFrame->mLength + 1); ack = (uint8_t *)calloc(1, aAckFrame->mLength + 1);
if (ack) { if (ack) {
ack[0] = aAckFrame->mLength; ack[0] = aAckFrame->mLength;
memcpy((void *)(ack + 1), aAckFrame->mPsdu, ack[0]); memcpy((void *)(ack + 1), aAckFrame->mPsdu, ack[0]);
@@ -170,7 +170,7 @@ void TxStarted(otInstance *aInstance, otRadioFrame *aFrame)
{ {
esp_radio_spinel_idx_t idx = get_index_from_instance(aInstance); esp_radio_spinel_idx_t idx = get_index_from_instance(aInstance);
assert(s_esp_radio_spinel_callbacks[idx].transmit_started); assert(s_esp_radio_spinel_callbacks[idx].transmit_started);
uint8_t *frame = (uint8_t *)malloc(aFrame->mLength + 1); uint8_t *frame = (uint8_t *)calloc(1, aFrame->mLength + 1);
if (frame) { if (frame) {
frame[0] = aFrame->mLength; frame[0] = aFrame->mLength;
memcpy((void *)(frame + 1), aFrame->mPsdu, frame[0]); memcpy((void *)(frame + 1), aFrame->mPsdu, frame[0]);
@@ -89,7 +89,7 @@ esp_err_t UartSpinelInterface::Enable(const esp_radio_spinel_uart_config_t &radi
return ESP_ERR_INVALID_STATE; return ESP_ERR_INVALID_STATE;
} }
m_uart_rx_buffer = static_cast<uint8_t *>(heap_caps_malloc(kMaxFrameSize, MALLOC_CAP_8BIT)); m_uart_rx_buffer = static_cast<uint8_t *>(heap_caps_calloc(1, kMaxFrameSize, MALLOC_CAP_8BIT));
if (m_uart_rx_buffer == NULL) { if (m_uart_rx_buffer == NULL) {
return ESP_ERR_NO_MEM; return ESP_ERR_NO_MEM;
} }