From 706d6cce33732c3469b61088ad783f50bef58926 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Al=D0=B5x=D0=B0nder=20Lysik=D0=BEv?=
 <alexander.lysikov@demlabs.net>
Date: Tue, 19 Nov 2019 22:21:05 +0500
Subject: [PATCH] fixed callback for atom_iter_get_links

---
 dap_chain_cs_dag.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

diff --git a/dap_chain_cs_dag.c b/dap_chain_cs_dag.c
index 4ee00df..3ae3565 100755
--- a/dap_chain_cs_dag.c
+++ b/dap_chain_cs_dag.c
@@ -766,8 +766,12 @@ static dap_chain_atom_ptr_t* s_chain_callback_atom_iter_get_lasts( dap_chain_ato
 static dap_chain_atom_ptr_t* s_chain_callback_atom_iter_get_links( dap_chain_atom_iter_t * a_atom_iter ,
                                                                   size_t * a_links_size_ptr )
 {
-    if ( a_atom_iter->cur ){
+    if ( a_atom_iter->cur && a_atom_iter->chain){
         dap_chain_cs_dag_t * l_dag = DAP_CHAIN_CS_DAG( a_atom_iter->chain );
+        if(!l_dag){
+            log_it(L_ERROR,"Chain %s have DAP_CHAIN_CS_DAG() = NULL", a_atom_iter->chain->name);
+            return NULL;
+        }
         dap_chain_cs_dag_event_t * l_event =(dap_chain_cs_dag_event_t *) a_atom_iter->cur;
         dap_chain_cs_dag_event_item_t * l_event_item = (dap_chain_cs_dag_event_item_t *) a_atom_iter->cur_item;
         if ( l_event->header.hash_count > 0){
@@ -784,12 +788,17 @@ static dap_chain_atom_ptr_t* s_chain_callback_atom_iter_get_links( dap_chain_ato
                     l_ret[i] = l_link_item->event;
                 }else {
                     char * l_link_hash_str = dap_chain_hash_fast_to_str_new(l_link_hash);
-                    char * l_event_hash_str = dap_chain_hash_fast_to_str_new(&l_event_item->hash);
-                    log_it(L_ERROR,"Can't find %s->%s links", l_event_hash_str, l_link_hash_str);
+                    char * l_event_hash_str = l_event_item ? dap_chain_hash_fast_to_str_new(&l_event_item->hash) : NULL;
+                    log_it(L_ERROR,"Can't find %s->%s links", l_event_hash_str ? l_event_hash_str : "[null]", l_link_hash_str);
                     DAP_DELETE(l_event_hash_str);
                     DAP_DELETE(l_link_hash_str);
+                    (*a_links_size_ptr)--;
                 }
             }
+            if(!(*a_links_size_ptr)){
+                DAP_DELETE(l_ret);
+                l_ret = NULL;
+            }
             return l_ret;
         }
     }
-- 
GitLab