From 14a96e95537d37e4f643ef92e38e8c8fc88baa8a Mon Sep 17 00:00:00 2001
From: "Dmitriy A. Gerasimov" <dmitriy.gerasimov@demlabs.net>
Date: Mon, 19 Jul 2021 15:44:35 +0700
Subject: [PATCH] [*] Error fixes

---
 dap-sdk/core/src/dap_list.c                           | 2 ++
 modules/global-db/dap_chain_global_db_driver_sqlite.c | 4 ++--
 modules/global-db/dap_chain_global_db_hist.c          | 7 +++++--
 modules/net/dap_chain_net.c                           | 2 ++
 modules/service/vpn/dap_chain_net_vpn_client.c        | 2 +-
 5 files changed, 12 insertions(+), 5 deletions(-)

diff --git a/dap-sdk/core/src/dap_list.c b/dap-sdk/core/src/dap_list.c
index 6ce09fb2c9..6308e74123 100755
--- a/dap-sdk/core/src/dap_list.c
+++ b/dap-sdk/core/src/dap_list.c
@@ -351,6 +351,8 @@ dap_list_t *dap_list_remove(dap_list_t *list, const void * data)
         else
         {
             list = _dap_list_remove_link(list, tmp);
+            if (list == tmp)
+                list = NULL;
             dap_list_free1(tmp);
             break;
         }
diff --git a/modules/global-db/dap_chain_global_db_driver_sqlite.c b/modules/global-db/dap_chain_global_db_driver_sqlite.c
index 43a2e07297..7f8a9ba6a7 100644
--- a/modules/global-db/dap_chain_global_db_driver_sqlite.c
+++ b/modules/global-db/dap_chain_global_db_driver_sqlite.c
@@ -253,11 +253,11 @@ int dap_db_driver_sqlite_flush()
         return -666;
     }
     dap_db_driver_sqlite_close(s_db);
-    char *l_error_message;
+    char *l_error_message = NULL;
     s_db = dap_db_driver_sqlite_open(s_filename_db, SQLITE_OPEN_READWRITE, &l_error_message);
     if(!s_db) {
         pthread_rwlock_unlock(&s_db_rwlock);
-        log_it(L_ERROR, "Can't init sqlite err: \"%s\"", l_error_message);
+        log_it(L_ERROR, "Can't init sqlite err: \"%s\"", l_error_message? l_error_message: "UNKNOWN");
         dap_db_driver_sqlite_free(l_error_message);
         return -3;
     }
diff --git a/modules/global-db/dap_chain_global_db_hist.c b/modules/global-db/dap_chain_global_db_hist.c
index 1429d6238e..3cb6272af5 100644
--- a/modules/global-db/dap_chain_global_db_hist.c
+++ b/modules/global-db/dap_chain_global_db_hist.c
@@ -1325,7 +1325,6 @@ dap_db_log_list_t* dap_db_log_list_start(uint64_t first_id, dap_list_t *a_add_gr
 {
 
     //log_it(L_DEBUG, "Start loading db list_write...");
-    dap_db_log_list_t *l_dap_db_log_list = DAP_NEW_Z(dap_db_log_list_t);
 
     size_t l_add_groups_num = 0;// number of group
     dap_list_t *l_add_groups_mask = a_add_groups_mask;
@@ -1337,6 +1336,8 @@ dap_db_log_list_t* dap_db_log_list_start(uint64_t first_id, dap_list_t *a_add_gr
         dap_list_free_full(l_groups, (dap_callback_destroyed_t) free);
         l_add_groups_mask = dap_list_next(l_add_groups_mask);
     }
+    if(l_add_groups_num == 0)
+        return NULL;
 
     size_t l_data_size_out_main = dap_db_log_get_last_id() - first_id + 1;
             //dap_chain_global_db_driver_count(GROUP_LOCAL_HISTORY, first_id); - not working for sqlite
@@ -1365,8 +1366,10 @@ dap_db_log_list_t* dap_db_log_list_start(uint64_t first_id, dap_list_t *a_add_gr
         dap_list_free_full(l_groups0, (dap_callback_destroyed_t) free);
         l_add_groups_mask = dap_list_next(l_add_groups_mask);
     }
-    if(!(l_data_size_out_main + l_data_size_out_add_items_count))
+    if(!(l_data_size_out_main + l_data_size_out_add_items_count)){
         return NULL;
+    }
+    dap_db_log_list_t *l_dap_db_log_list = DAP_NEW_Z(dap_db_log_list_t);
     l_dap_db_log_list->item_start = first_id;
     l_dap_db_log_list->item_last = first_id + l_data_size_out_main;
     l_dap_db_log_list->items_number_main = l_data_size_out_main;
diff --git a/modules/net/dap_chain_net.c b/modules/net/dap_chain_net.c
index 9c5e4a39c0..4bc508ce9f 100644
--- a/modules/net/dap_chain_net.c
+++ b/modules/net/dap_chain_net.c
@@ -2117,6 +2117,8 @@ dap_chain_net_t **dap_chain_net_list(uint16_t *a_size)
         int i = 0;
         HASH_ITER(hh, s_net_items, l_current_item, l_tmp) {
             l_net_list[i++] = l_current_item->chain_net;
+            if(i > *a_size)
+                break;
         }
         return l_net_list;
     }else
diff --git a/modules/service/vpn/dap_chain_net_vpn_client.c b/modules/service/vpn/dap_chain_net_vpn_client.c
index c442464d79..c41ca37e10 100644
--- a/modules/service/vpn/dap_chain_net_vpn_client.c
+++ b/modules/service/vpn/dap_chain_net_vpn_client.c
@@ -370,7 +370,7 @@ char *dap_chain_net_vpn_client_check_result(dap_chain_net_t *a_net, const char*
         for(size_t i = 0; i < l_orders_num; i++) {
             dap_chain_net_srv_order_t *l_order = (dap_chain_net_srv_order_t *) (((byte_t*) l_orders) + l_orders_size);
             //dap_chain_net_srv_order_dump_to_string(l_order, l_string_ret, l_hash_out_type);
-            dap_chain_hash_fast_t l_hash;
+            dap_chain_hash_fast_t l_hash={0};
             char *l_hash_str;
             dap_hash_fast(l_order, dap_chain_net_srv_order_get_size(l_order), &l_hash);
             if(!dap_strcmp(a_hash_out_type, "hex"))
-- 
GitLab