From 5568a037bf4814dd0c3678212e1e462c987641f5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Al=D0=B5x=D0=B0nder=20Lysik=D0=BEv?=
 <alexander.lysikov@demlabs.net>
Date: Mon, 5 Oct 2020 21:06:35 +0500
Subject: [PATCH] fixed memory crash in console

---
 modules/app-cli/dap_app_cli_net.c     | 13 +++++++++++--
 modules/app-cli/include/dap_app_cli.h |  1 +
 2 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/modules/app-cli/dap_app_cli_net.c b/modules/app-cli/dap_app_cli_net.c
index 517a42d105..e545c76742 100644
--- a/modules/app-cli/dap_app_cli_net.c
+++ b/modules/app-cli/dap_app_cli_net.c
@@ -69,8 +69,16 @@ static void dap_app_cli_http_read(uint64_t *socket, dap_app_cli_cmd_state_t *l_c
                 l_cmd->cmd_res_len = atoi(l_str_ptr + strlen(l_cont_len_str));
                 if (l_cmd->cmd_res_len == 0) {
                     s_status = DAP_CLI_ERROR_FORMAT;
-                } else {
+                }
+                else {
                     s_status++;
+                    // resize buffer for received data
+                    if (l_cmd->cmd_res_len > l_cmd->cmd_res_len_max) {
+                        size_t l_len_max = l_cmd->cmd_res_len_max;
+                        l_cmd->cmd_res_len_max = l_cmd->cmd_res_len + 1;
+                        l_cmd->cmd_res = DAP_REALLOC(l_cmd->cmd_res, l_cmd->cmd_res_len_max);
+                        memset(l_cmd->cmd_res + l_len_max, 0, l_cmd->cmd_res_len_max - l_len_max);
+                    }
                 }
             } else {
                 break;
@@ -181,7 +189,8 @@ int dap_app_cli_post_command( dap_app_cli_connect_param_t *a_socket, dap_app_cli
         return -1;
     }
     s_status = 1;
-    a_cmd->cmd_res = DAP_NEW_Z_SIZE(char, DAP_CLI_HTTP_RESPONSE_SIZE_MAX);
+    a_cmd->cmd_res_len_max = DAP_CLI_HTTP_RESPONSE_SIZE_MAX;
+    a_cmd->cmd_res = DAP_NEW_Z_SIZE(char, a_cmd->cmd_res_len_max);
     a_cmd->cmd_res_cur = 0;
     dap_string_t *l_cmd_data = dap_string_new(a_cmd->cmd_name);
     if (a_cmd->cmd_param) {
diff --git a/modules/app-cli/include/dap_app_cli.h b/modules/app-cli/include/dap_app_cli.h
index 944bb9c96c..0acfe18fa6 100644
--- a/modules/app-cli/include/dap_app_cli.h
+++ b/modules/app-cli/include/dap_app_cli.h
@@ -34,6 +34,7 @@ typedef struct dap_app_cli_cmd_state {
     int ret_code;
     // for reply
     char *cmd_res;
+    size_t cmd_res_len_max;
     size_t cmd_res_len;
     size_t cmd_res_cur;
 } dap_app_cli_cmd_state_t;
-- 
GitLab