@@ -165,23 +165,29 @@ static bool process_empty_tx(const s_tx_ctx *tx_ctx) {
165165}
166166
167167bool process_empty_txs_before (void ) {
168- for (list_node_t * tmp = ((list_node_t * ) g_tx_ctx_current )-> prev ;
169- (tmp != NULL ) && (((s_tx_ctx * ) tmp )-> calldata == NULL );
170- tmp = tmp -> prev ) {
168+ list_node_t * tmp = ((list_node_t * ) g_tx_ctx_current )-> prev ;
169+ while ((tmp != NULL ) && (((s_tx_ctx * ) tmp )-> calldata == NULL )) {
170+ // process_empty_tx calls list_remove + delete_tx_ctx, which frees tmp.
171+ // Ensure reading tmp->prev before the call to avoid use-after-free.
172+ list_node_t * prev = tmp -> prev ;
171173 if (!process_empty_tx ((s_tx_ctx * ) tmp )) {
172174 return false;
173175 }
176+ tmp = prev ;
174177 }
175178 return true;
176179}
177180
178181bool process_empty_txs_after (void ) {
179- for (flist_node_t * tmp = ((flist_node_t * ) g_tx_ctx_current )-> next ;
180- (tmp != NULL ) && (((s_tx_ctx * ) tmp )-> calldata == NULL );
181- tmp = tmp -> next ) {
182+ flist_node_t * tmp = ((flist_node_t * ) g_tx_ctx_current )-> next ;
183+ while ((tmp != NULL ) && (((s_tx_ctx * ) tmp )-> calldata == NULL )) {
184+ // process_empty_tx calls list_remove + delete_tx_ctx, which frees tmp.
185+ // Ensure reading tmp->next before the call to avoid use-after-free.
186+ flist_node_t * next = tmp -> next ;
182187 if (!process_empty_tx ((s_tx_ctx * ) tmp )) {
183188 return false;
184189 }
190+ tmp = next ;
185191 }
186192 return true;
187193}
@@ -291,6 +297,15 @@ bool tx_ctx_init(s_calldata *calldata,
291297 return false;
292298 }
293299 list_push_back ((list_node_t * * ) & g_tx_ctx_list , (list_node_t * ) node );
300+
301+ // Ownership of the calldata has been transferred to the node.
302+ // Clear g_parked_calldata now so callers cannot double-free it if we return
303+ // false below (e.g. when field_table_init fails after the node is in the list
304+ // and will be freed by tx_ctx_cleanup via delete_tx_ctx).
305+ if (g_parked_calldata == calldata ) {
306+ g_parked_calldata = NULL ;
307+ }
308+
294309 if ((appState == APP_STATE_SIGNING_TX ) && (node == g_tx_ctx_list )) {
295310 return field_table_init ();
296311 }
0 commit comments