@@ -265,12 +265,6 @@ void sn_coap_protocol_clear_sent_blockwise_messages(struct coap_s *handle)
265
265
266
266
/* Loop all stored Blockwise messages in Linked list */
267
267
ns_list_foreach_safe (coap_blockwise_msg_s , removed_blocwise_msg_ptr , & handle -> linked_list_blockwise_sent_msgs ) {
268
- if (removed_blocwise_msg_ptr -> coap_msg_ptr ) {
269
- handle -> sn_coap_protocol_free (removed_blocwise_msg_ptr -> coap_msg_ptr -> payload_ptr );
270
- removed_blocwise_msg_ptr -> coap_msg_ptr -> payload_ptr = 0 ;
271
- sn_coap_parser_release_allocated_coap_msg_mem (handle , removed_blocwise_msg_ptr -> coap_msg_ptr );
272
- removed_blocwise_msg_ptr -> coap_msg_ptr = 0 ;
273
- }
274
268
sn_coap_protocol_linked_list_blockwise_msg_remove (handle , removed_blocwise_msg_ptr );
275
269
}
276
270
#endif
@@ -1472,27 +1466,28 @@ static void sn_coap_protocol_handle_blockwise_timout(struct coap_s *handle)
1472
1466
ns_list_foreach_safe (coap_blockwise_msg_s , removed_blocwise_msg_ptr , & handle -> linked_list_blockwise_sent_msgs ) {
1473
1467
if ((handle -> system_time - removed_blocwise_msg_ptr -> timestamp ) > SN_COAP_BLOCKWISE_MAX_TIME_DATA_STORED ) {
1474
1468
1469
+ // Item must be removed from the list before calling the rx_callback function.
1470
+ // Callback could actually clear the list and free the item and cause a use after free when callback returns.
1471
+ ns_list_remove (& handle -> linked_list_blockwise_sent_msgs , removed_blocwise_msg_ptr );
1472
+
1475
1473
/* * * * This messages has timed out, remove it from Linked list * * * */
1476
1474
if ( removed_blocwise_msg_ptr -> coap_msg_ptr ){
1477
-
1478
1475
if (handle -> sn_coap_rx_callback ) {
1479
1476
/* Notify the application about the time out */
1480
1477
removed_blocwise_msg_ptr -> coap_msg_ptr -> coap_status = COAP_STATUS_BUILDER_BLOCK_SENDING_FAILED ;
1481
1478
removed_blocwise_msg_ptr -> coap_msg_ptr -> msg_id = removed_blocwise_msg_ptr -> msg_id ;
1482
1479
handle -> sn_coap_rx_callback (removed_blocwise_msg_ptr -> coap_msg_ptr , NULL , removed_blocwise_msg_ptr -> param );
1483
1480
}
1484
1481
1485
- if (removed_blocwise_msg_ptr -> coap_msg_ptr -> payload_ptr ){
1486
- handle -> sn_coap_protocol_free (removed_blocwise_msg_ptr -> coap_msg_ptr -> payload_ptr );
1487
- removed_blocwise_msg_ptr -> coap_msg_ptr -> payload_ptr = 0 ;
1488
- }
1482
+ handle -> sn_coap_protocol_free (removed_blocwise_msg_ptr -> coap_msg_ptr -> payload_ptr );
1489
1483
sn_coap_parser_release_allocated_coap_msg_mem (handle , removed_blocwise_msg_ptr -> coap_msg_ptr );
1490
- removed_blocwise_msg_ptr -> coap_msg_ptr = 0 ;
1491
1484
}
1492
- sn_coap_protocol_linked_list_blockwise_msg_remove (handle , removed_blocwise_msg_ptr );
1485
+
1486
+ handle -> sn_coap_protocol_free (removed_blocwise_msg_ptr );
1493
1487
}
1494
1488
}
1495
1489
1490
+
1496
1491
/* Loop all incoming Blockwise messages */
1497
1492
ns_list_foreach_safe (coap_blockwise_payload_s , removed_blocwise_payload_ptr , & handle -> linked_list_blockwise_received_payloads ) {
1498
1493
if ((handle -> system_time - removed_blocwise_payload_ptr -> timestamp ) > SN_COAP_BLOCKWISE_MAX_TIME_DATA_STORED ) {
0 commit comments