From c84659072fed677f188ced23ad2b47e3e2defc1a Mon Sep 17 00:00:00 2001 From: Sadhyama Vengilat Date: Tue, 25 Mar 2025 18:34:24 +0530 Subject: [PATCH] To fix webconfig crash while using testutility with empty multipart bin --- src/webcfg.c | 16 +++++++++++----- src/webcfg_multipart.c | 12 +++++++++--- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/webcfg.c b/src/webcfg.c index c651a8b0..5dc59c7c 100644 --- a/src/webcfg.c +++ b/src/webcfg.c @@ -834,11 +834,21 @@ int testUtility() if(readFromFile(TEST_FILE_LOCATION, &data, &test_dataSize) == 1) { + if(data != NULL && test_dataSize == 0) + { + WebcfgError("Test file is empty\n"); + return 0; + } set_g_testfile(1); WebcfgInfo("Using Test file \n"); char * data_body = malloc(sizeof(char) * test_dataSize+1); - memset(data_body, 0, sizeof(char) * test_dataSize+1); + if( NULL == data_body ) + { + WebcfgError("Memory allocation for data_body failed.\n"); + return 0; + } + memset(data_body, 0, sizeof(char) * (test_dataSize + 1)); data_body = memcpy(data_body, data, test_dataSize+1); data_body[test_dataSize] = '\0'; char *ptr_count = data_body; @@ -904,10 +914,6 @@ int testUtility() { WebcfgInfo("Test webConfigData applied successfully\n"); } - else - { - WebcfgError("Failed to apply Test root webConfigData received from server\n"); - } } else { diff --git a/src/webcfg_multipart.c b/src/webcfg_multipart.c index 9f842ff9..ad84b6d2 100644 --- a/src/webcfg_multipart.c +++ b/src/webcfg_multipart.c @@ -589,7 +589,7 @@ WEBCFG_STATUS parseMultipartDocument(void *config_data, char *ct , size_t data_s } else { - WebcfgDebug("processMsgpackSubdoc done,docs are sent for apply\n"); + WebcfgInfo("processMsgpackSubdoc done,docs are sent for apply\n"); } return WEBCFG_FAILURE; } @@ -1323,13 +1323,19 @@ int readFromFile(char *filename, char **data, int *len) ch_count = ftell(fp); fseek(fp, 0, SEEK_SET); *data = (char *) malloc(sizeof(char) * (ch_count + 1)); - sz = fread(*data, 1, ch_count-1,fp); + if( NULL == *data ) + { + WebcfgError("Memory allocation for data failed.\n"); + fclose(fp); + return 0; + } + sz = fread(*data, 1, ch_count,fp); if (!sz) { fclose(fp); WebcfgError("fread failed.\n"); WEBCFG_FREE(*data); - return WEBCFG_FAILURE; + return 0; } *len = ch_count; (*data)[ch_count] ='\0';