From 70946ea60f60814aee004d1f627d5879d86bdb13 Mon Sep 17 00:00:00 2001 From: knair410 Date: Thu, 14 May 2020 14:16:24 +0530 Subject: [PATCH] Valgrind and Coverity Fixes --- src/webcfg_blob.c | 57 +++++++++++++++++++++++++++++------------------ src/webcfg_blob.h | 2 +- 2 files changed, 36 insertions(+), 23 deletions(-) diff --git a/src/webcfg_blob.c b/src/webcfg_blob.c index 678c4cb2..8ec3090f 100644 --- a/src/webcfg_blob.c +++ b/src/webcfg_blob.c @@ -234,33 +234,46 @@ char * webcfg_appendeddoc(char * subdoc_name, uint32_t version, char * blob_data WebcfgInfo("The version is : %lu",(long)version); appenddata->subdoc_name = strdup(subdoc_name); appenddata->version = version; - appenddata->transaction_id = generateTransactionId(1001,3000); + appenddata->transaction_id = generateTransactionId(); + WebcfgInfo("Append Doc \n"); + appenddocPackSize = webcfg_pack_appenddoc(appenddata, &appenddocdata); + WebcfgInfo("appenddocPackSize is %zu\n", appenddocPackSize); + WebcfgInfo("data packed is %s\n", (char*)appenddocdata); + + WEBCFG_FREE(appenddata->subdoc_name); + WEBCFG_FREE(appenddata); + + WebcfgInfo("---------------------------------------------------------------\n"); + embeddeddocPackSize = appendWebcfgEncodedData(&embeddeddocdata, (void *)blob_data, blob_size, appenddocdata, appenddocPackSize); + WebcfgInfo("embeddeddocPackSize is %zu\n", embeddeddocPackSize); + WebcfgInfo("The embedded doc data is %s\n",(char*)embeddeddocdata); + + writeToFileData(APPEND_FILE, (char*)embeddeddocdata, embeddeddocPackSize); + finaldocdata = base64blobencoder((char *)embeddeddocdata, embeddeddocPackSize); + WebcfgInfo("The encoded append doc is %s\n",finaldocdata); } - - WebcfgInfo("Append Doc \n"); - appenddocPackSize = webcfg_pack_appenddoc(appenddata, &appenddocdata); - WebcfgInfo("appenddocPackSize is %zu\n", appenddocPackSize); - WebcfgInfo("data packed is %s\n", (char*)appenddocdata); - - free(appenddata->subdoc_name); - free(appenddata); - - WebcfgInfo("---------------------------------------------------------------\n"); - embeddeddocPackSize = appendWebcfgEncodedData(&embeddeddocdata, (void *)blob_data, blob_size, appenddocdata, appenddocPackSize); - WebcfgInfo("embeddeddocPackSize is %zu\n", embeddeddocPackSize); - WebcfgInfo("The embedded doc data is %s\n",(char*)embeddeddocdata); - - writeToFileData(APPEND_FILE, (char*)embeddeddocdata, embeddeddocPackSize); - finaldocdata = base64blobencoder((char *)embeddeddocdata, embeddeddocPackSize); - WebcfgInfo("The encoded append doc is %s\n",finaldocdata); + return finaldocdata; } -uint16_t generateTransactionId(int min, int max) +uint16_t generateTransactionId() { - srand(time(0)); - return (uint16_t)((rand() % - (max - min + 1)) + min); + FILE *fp; + uint16_t random_key,sz; + fp = fopen("/dev/urandom", "r"); + if (fp == NULL){ + return 0; + } + sz = fread(&random_key, sizeof(random_key), 1, fp); + if (!sz) + { + fclose(fp); + WebcfgError("fread failed.\n"); + return 0; + } + WebcfgInfo("generateTransactionId\n %d",random_key); + fclose(fp); + return(random_key); } int writeToFileData(char *db_file_path, char *data, size_t size) diff --git a/src/webcfg_blob.h b/src/webcfg_blob.h index 7d318b3f..d8fa6ca9 100644 --- a/src/webcfg_blob.h +++ b/src/webcfg_blob.h @@ -54,7 +54,7 @@ size_t appendWebcfgEncodedData( void **appendData, void *encodedBuffer, size_t e char * webcfg_appendeddoc(char * subdoc_name, uint32_t version, char * blob_data, size_t blob_size); -uint16_t generateTransactionId(int min, int max); +uint16_t generateTransactionId(); int writeToFileData(char *db_file_path, char *data, size_t size);