@@ -265,12 +265,6 @@ void sn_coap_protocol_clear_sent_blockwise_messages(struct coap_s *handle)
265265
266266 /* Loop all stored Blockwise messages in Linked list */
267267 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- }
274268 sn_coap_protocol_linked_list_blockwise_msg_remove (handle , removed_blocwise_msg_ptr );
275269 }
276270#endif
@@ -1472,27 +1466,28 @@ static void sn_coap_protocol_handle_blockwise_timout(struct coap_s *handle)
14721466 ns_list_foreach_safe (coap_blockwise_msg_s , removed_blocwise_msg_ptr , & handle -> linked_list_blockwise_sent_msgs ) {
14731467 if ((handle -> system_time - removed_blocwise_msg_ptr -> timestamp ) > SN_COAP_BLOCKWISE_MAX_TIME_DATA_STORED ) {
14741468
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+
14751473 /* * * * This messages has timed out, remove it from Linked list * * * */
14761474 if ( removed_blocwise_msg_ptr -> coap_msg_ptr ){
1477-
14781475 if (handle -> sn_coap_rx_callback ) {
14791476 /* Notify the application about the time out */
14801477 removed_blocwise_msg_ptr -> coap_msg_ptr -> coap_status = COAP_STATUS_BUILDER_BLOCK_SENDING_FAILED ;
14811478 removed_blocwise_msg_ptr -> coap_msg_ptr -> msg_id = removed_blocwise_msg_ptr -> msg_id ;
14821479 handle -> sn_coap_rx_callback (removed_blocwise_msg_ptr -> coap_msg_ptr , NULL , removed_blocwise_msg_ptr -> param );
14831480 }
14841481
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 );
14891483 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 ;
14911484 }
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 );
14931487 }
14941488 }
14951489
1490+
14961491 /* Loop all incoming Blockwise messages */
14971492 ns_list_foreach_safe (coap_blockwise_payload_s , removed_blocwise_payload_ptr , & handle -> linked_list_blockwise_received_payloads ) {
14981493 if ((handle -> system_time - removed_blocwise_payload_ptr -> timestamp ) > SN_COAP_BLOCKWISE_MAX_TIME_DATA_STORED ) {
0 commit comments