From 0a44eaba738f939fb1b219b9e4801d86a29d3d44 Mon Sep 17 00:00:00 2001
From: "pavel.uhanov" <pavel.uhanov@demlabs.net>
Date: Mon, 28 Aug 2023 06:02:02 +0000
Subject: [PATCH] hotfix-9276

---
 dap-sdk                                   |  2 +-
 modules/common/dap_chain_common.c         |  2 +-
 modules/common/include/dap_chain_common.h | 21 +++++-
 modules/net/srv/dap_chain_net_srv.c       | 88 +++++++++++------------
 4 files changed, 62 insertions(+), 51 deletions(-)

diff --git a/dap-sdk b/dap-sdk
index dd493a2095..6947b1238f 160000
--- a/dap-sdk
+++ b/dap-sdk
@@ -1 +1 @@
-Subproject commit dd493a209579b92a9935d59c546ce276d0e653c4
+Subproject commit 6947b1238f6057bce9733c4084cf41f2a01d434f
diff --git a/modules/common/dap_chain_common.c b/modules/common/dap_chain_common.c
index e528930e85..fca82eca81 100644
--- a/modules/common/dap_chain_common.c
+++ b/modules/common/dap_chain_common.c
@@ -43,7 +43,7 @@ const dap_chain_addr_t c_dap_chain_addr_blank = {0};
 
 int dap_id_uint64_parse(const char *a_id_str, uint64_t *a_id)
 {
-    if (sscanf(a_id_str, "0x%16"DAP_UINT64_FORMAT_X, a_id) != 1 &&
+    if (!a_id_str || !a_id || sscanf(a_id_str, "0x%16"DAP_UINT64_FORMAT_X, a_id) != 1 &&
             sscanf(a_id_str, "0x%16"DAP_UINT64_FORMAT_x, a_id) != 1 &&
             sscanf(a_id_str, "%"DAP_UINT64_FORMAT_U, a_id) != 1) {
         log_it (L_ERROR, "Can't recognize '%s' string as 64-bit id, hex or dec", a_id_str);
diff --git a/modules/common/include/dap_chain_common.h b/modules/common/include/dap_chain_common.h
index 92ab735e54..1745b44a58 100644
--- a/modules/common/include/dap_chain_common.h
+++ b/modules/common/include/dap_chain_common.h
@@ -108,7 +108,7 @@ DAP_STATIC_INLINE bool dap_chain_node_addr_str_check(const char *a_addr_str) {
 
 DAP_STATIC_INLINE int dap_chain_node_addr_from_str(dap_chain_node_addr_t *a_addr, const char *a_addr_str)
 {
-    if (!a_addr){
+    if (!a_addr || !a_addr_str){
         return -1;
     }
     if (sscanf(a_addr_str, NODE_ADDR_FP_STR, NODE_ADDR_FPS_ARGS(a_addr)) == 4)
@@ -207,6 +207,25 @@ DAP_STATIC_INLINE const char *dap_chain_srv_unit_enum_to_str(dap_chain_srv_unit_
     }
 }
 
+DAP_STATIC_INLINE dap_chain_srv_unit_enum_t dap_chain_srv_str_to_unit_enum(char* a_price_unit_str) {
+    if (!a_price_unit_str)
+        return SERV_UNIT_UNDEFINED;
+    if (!dap_strcmp(a_price_unit_str, "MB")){
+        return SERV_UNIT_MB;
+    } else if (!dap_strcmp(a_price_unit_str, "SEC")){
+        return SERV_UNIT_SEC;
+    } else if (!dap_strcmp(a_price_unit_str, "DAY")){
+        return SERV_UNIT_DAY;
+    } else if (!dap_strcmp(a_price_unit_str, "KB")){
+        return SERV_UNIT_KB;
+    } else if (!dap_strcmp(a_price_unit_str, "B")){
+        return SERV_UNIT_B;
+    } else if (!dap_strcmp(a_price_unit_str, "PCS")){
+        return SERV_UNIT_PCS;
+    }
+    return SERV_UNIT_UNDEFINED;
+}
+
 typedef union {
     uint8_t raw[4];
     uint32_t uint32;
diff --git a/modules/net/srv/dap_chain_net_srv.c b/modules/net/srv/dap_chain_net_srv.c
index 09bd87b62b..9ebb563bcd 100644
--- a/modules/net/srv/dap_chain_net_srv.c
+++ b/modules/net/srv/dap_chain_net_srv.c
@@ -80,6 +80,7 @@ static bool s_pay_verificator_callback(dap_ledger_t * a_ledger, dap_chain_tx_out
                                        dap_chain_datum_tx_t *a_tx_in, bool a_owner);
 static bool s_fee_verificator_callback(dap_ledger_t * a_ledger, dap_chain_tx_out_cond_t *a_cond,
                                        dap_chain_datum_tx_t *a_tx_in, bool a_owner);
+static int s_str_to_price_unit(char* a_price_unit_str, dap_chain_net_srv_price_unit_uid_t* a_price_unit);
 
 /**
  * @brief dap_chain_net_srv_init
@@ -248,18 +249,18 @@ static int s_cli_net_srv( int argc, char **argv, char **a_str_reply)
         const char *l_units_str = NULL;
         dap_cli_server_cmd_find_option_val(argv, arg_index, argc, "-units", &l_units_str);
 
-        char *l_order_hash_hex_str;
-        char *l_order_hash_base58_str;
+        char *l_order_hash_hex_str = NULL;
+        char *l_order_hash_base58_str = NULL;
         // datum hash may be in hex or base58 format
-        if(!dap_strncmp(l_order_hash_str, "0x", 2) || !dap_strncmp(l_order_hash_str, "0X", 2)) {
-            l_order_hash_hex_str = dap_strdup(l_order_hash_str);
-            l_order_hash_base58_str = dap_enc_base58_from_hex_str_to_str(l_order_hash_str);
-        }
-        else {
-            l_order_hash_hex_str = dap_enc_base58_to_hex_str_from_str(l_order_hash_str);
-            l_order_hash_base58_str = dap_strdup(l_order_hash_str);
+        if (l_order_hash_str) {
+            if(!dap_strncmp(l_order_hash_str, "0x", 2) || !dap_strncmp(l_order_hash_str, "0X", 2)) {
+                l_order_hash_hex_str = dap_strdup(l_order_hash_str);
+                l_order_hash_base58_str = dap_enc_base58_from_hex_str_to_str(l_order_hash_str);
+            } else {
+                l_order_hash_hex_str = dap_enc_base58_to_hex_str_from_str(l_order_hash_str);
+                l_order_hash_base58_str = dap_strdup(l_order_hash_str);
+            }
         }
-
         if(l_continent_str && l_continent_num <= 0) {
             dap_string_t *l_string_err = dap_string_new("Unrecognized \"-continent\" option=");
             dap_string_append_printf(l_string_err, "\"%s\". Variants: ", l_continent_str);
@@ -367,8 +368,10 @@ static int s_cli_net_srv( int argc, char **argv, char **a_str_reply)
                 }
             }
 
-            if ( l_srv_uid_str)
-                l_srv_uid.uint64 = (uint64_t) atoll( l_srv_uid_str);
+            if (l_srv_uid_str && dap_id_uint64_parse(l_srv_uid_str ,&l_srv_uid.uint64)) {
+                dap_cli_server_cmd_set_reply_text(a_str_reply, "Can't recognize '%s' string as 64-bit id, hex or dec.", l_srv_uid_str);
+                return -21;
+            }
 
             if ( l_price_min_str )
                 l_price_min = dap_chain_balance_scan(l_price_min_str);
@@ -400,7 +403,7 @@ static int s_cli_net_srv( int argc, char **argv, char **a_str_reply)
             // Select with specified service uid
             if ( l_order_hash_str ){
                 dap_chain_net_srv_order_t * l_order = dap_chain_net_srv_order_find_by_hash_str( l_net, l_order_hash_hex_str );
-                if (l_order){
+                if (l_order) {
                     dap_chain_net_srv_order_dump_to_string(l_order,l_string_ret, l_hash_out_type);
                     l_ret = 0;
                 }else{
@@ -410,17 +413,17 @@ static int s_cli_net_srv( int argc, char **argv, char **a_str_reply)
                     else
                         dap_string_append_printf(l_string_ret,"Can't find order with hash %s\n", l_order_hash_base58_str );
                 }
-            } else{
+            } else {
 
                 dap_chain_net_srv_order_t * l_orders = NULL;
-                size_t l_orders_num =0;
+                size_t l_orders_num = 0;
                 dap_chain_net_srv_uid_t l_srv_uid={{0}};
                 uint256_t l_price_min = {};
                 uint256_t l_price_max = {};
                 dap_chain_net_srv_price_unit_uid_t l_price_unit={{0}};
                 dap_chain_net_srv_order_direction_t l_direction = SERV_DIR_UNDEFINED;
 
-                if( dap_chain_net_srv_order_find_all_by( l_net,l_direction,l_srv_uid,l_price_unit, NULL, l_price_min, l_price_max,&l_orders,&l_orders_num) == 0 ){
+                if( !dap_chain_net_srv_order_find_all_by( l_net,l_direction,l_srv_uid,l_price_unit, NULL, l_price_min, l_price_max,&l_orders,&l_orders_num) ){
                     dap_string_append_printf(l_string_ret,"Found %zd orders:\n",l_orders_num);
                     size_t l_orders_size = 0;
                     for(size_t i = 0; i < l_orders_num; i++) {
@@ -492,10 +495,13 @@ static int s_cli_net_srv( int argc, char **argv, char **a_str_reply)
                     }
                 }
 
-
                 if (l_expires_str)
                     l_expires = (dap_time_t ) atoll( l_expires_str);
-                l_srv_uid.uint64 = (uint64_t) atoll( l_srv_uid_str);
+                if (l_srv_uid_str && dap_id_uint64_parse(l_srv_uid_str ,&l_srv_uid.uint64)) {
+                    dap_cli_server_cmd_set_reply_text(a_str_reply, "Can't recognize '%s' string as 64-bit id, hex or dec.", l_srv_uid_str);
+                    dap_string_free(l_string_ret, true);
+                    return -21;
+                }
                 if (l_node_addr_str){
                     if (dap_chain_node_addr_str_check(l_node_addr_str)) {
                         dap_chain_node_addr_from_str( &l_node_addr, l_node_addr_str );
@@ -514,20 +520,7 @@ static int s_cli_net_srv( int argc, char **argv, char **a_str_reply)
                     dap_chain_hash_fast_from_str (l_tx_cond_hash_str, &l_tx_cond_hash);
                 l_price = dap_chain_balance_scan(l_price_str);
 
-                if (!dap_strcmp(l_price_unit_str, "MB")){
-                    l_price_unit.uint32 = SERV_UNIT_MB;
-                } else if (!dap_strcmp(l_price_unit_str, "SEC")){
-                    l_price_unit.uint32 = SERV_UNIT_SEC;
-                } else if (!dap_strcmp(l_price_unit_str, "DAY")){
-                    l_price_unit.uint32 = SERV_UNIT_DAY;
-                } else if (!dap_strcmp(l_price_unit_str, "KB")){
-                    l_price_unit.uint32 = SERV_UNIT_KB;
-                } else if (!dap_strcmp(l_price_unit_str, "B")){
-                    l_price_unit.uint32 = SERV_UNIT_B;
-                } else if (!dap_strcmp(l_price_unit_str, "PCS")){
-                    l_price_unit.uint32 = SERV_UNIT_PCS;
-                } else {
-                    //l_price_unit.uint32 = SERV_UNIT_UNDEFINED;
+                if (s_str_to_price_unit(l_price_unit_str, &l_price_unit)){
                     log_it(L_ERROR, "Undefined price unit");
                     dap_string_free(l_string_ret, true);
                     dap_cli_server_cmd_set_reply_text(a_str_reply, "Wrong unit type sepcified, possible values: B, KB, MB, SEC, DAY, PCS");
@@ -542,9 +535,9 @@ static int s_cli_net_srv( int argc, char **argv, char **a_str_reply)
                 dap_enc_key_t *l_key = NULL;
                 if(l_order_cert_name) {
                     l_cert = dap_cert_find_by_name(l_order_cert_name);
-                    if(l_cert)
+                    if(l_cert) {
                         l_key = l_cert->enc_key;
-                    else {
+                    } else {
                         log_it(L_ERROR, "Can't load cert '%s' for sign order", l_order_cert_name);
                         dap_cli_server_cmd_set_reply_text(a_str_reply, "Can't load cert '%s' for sign "
                                                                        "order", l_order_cert_name);
@@ -932,19 +925,7 @@ int dap_chain_net_srv_parse_pricelist(dap_chain_net_srv_t *a_srv, const char *a_
                 }
                 continue;
             case 4:
-                if (!strcmp(l_price_token,      "SEC"))
-                    l_price->units_uid.enm = SERV_UNIT_SEC;
-                else if (!strcmp(l_price_token, "DAY"))
-                    l_price->units_uid.enm = SERV_UNIT_DAY;
-                else if (!strcmp(l_price_token, "MB"))
-                    l_price->units_uid.enm = SERV_UNIT_MB;
-                else if (!strcmp(l_price_token, "KB"))
-                    l_price->units_uid.enm = SERV_UNIT_KB;
-                else if (!strcmp(l_price_token, "B"))
-                    l_price->units_uid.enm = SERV_UNIT_B;
-                else if (!strcmp(l_price_token, "PCS"))
-                    l_price->units_uid.enm = SERV_UNIT_PCS;
-                else {
+                if (s_str_to_price_unit(l_price_token, &(l_price->units_uid))) {
                     log_it(L_ERROR, "Error parsing pricelist: wrong unit type \"%s\"", l_price_token);
                     l_iter = 0;
                     break;
@@ -1216,4 +1197,15 @@ dap_chain_datum_tx_receipt_t * dap_chain_net_srv_issue_receipt(dap_chain_net_srv
     return dap_chain_datum_tx_receipt_sign_add(l_receipt, dap_chain_wallet_get_key(a_price->wallet, 0));
 }
 
-
+/**
+ * @brief dap_chain_net_srv_issue_receipt
+ * @param a_str_price_unit
+ * @param a_price_unit
+ * @return 0 if OK, other if error
+ */
+int s_str_to_price_unit(char* a_price_unit_str, dap_chain_net_srv_price_unit_uid_t* a_price_unit) {
+    if (!a_price_unit_str || !a_price_unit)
+        return -1;
+    a_price_unit->enm = dap_chain_srv_str_to_unit_enum(a_price_unit_str);
+    return a_price_unit->enm != SERV_UNIT_UNDEFINED ? 0 : -1;
+}
-- 
GitLab