From f843a22bb81ef9ca5f2e5250f1cefd5d59e5c210 Mon Sep 17 00:00:00 2001 From: hjxilinx Date: Fri, 10 Apr 2020 15:05:33 +0800 Subject: [PATCH] [td-98]fix uninitialised value access warning by valgrind --- src/util/src/tglobalcfg.c | 69 +++++++++++++++++++++------------------ 1 file changed, 37 insertions(+), 32 deletions(-) diff --git a/src/util/src/tglobalcfg.c b/src/util/src/tglobalcfg.c index 8a0d66068e..69178e35e9 100644 --- a/src/util/src/tglobalcfg.c +++ b/src/util/src/tglobalcfg.c @@ -14,13 +14,12 @@ */ #include "os.h" -#include +#include "taosdef.h" +#include "taoserror.h" #include "tglobalcfg.h" #include "tkey.h" #include "tlog.h" -#include "taosdef.h" -#include "taoserror.h" #include "tsocket.h" #include "tsystem.h" #include "tutil.h" @@ -833,9 +832,8 @@ void tsReadGlobalLogConfig() { FILE * fp; char * line, *option, *value; - size_t len; int olen, vlen; - char fileName[128]; + char fileName[PATH_MAX] = {0}; mdebugFlag = 135; sdbDebugFlag = 135; @@ -851,27 +849,30 @@ void tsReadGlobalLogConfig() { wordfree(&full_path); tsReadLogOption("logDir", logDir); + sprintf(fileName, "%s/taos.cfg", configDir); fp = fopen(fileName, "r"); if (fp == NULL) { - printf("\noption file:%s not found, all options are set to system default\n", fileName); + printf("\nconfig file:%s not found, all variables are set to default\n", fileName); return; } - - line = NULL; + + size_t len = 1024; + line = calloc(1, len); + while (!feof(fp)) { - tfree(line); - line = option = value = NULL; - len = olen = vlen = 0; + memset(line, 0, len); + + option = value = NULL; + olen = vlen = 0; getline(&line, &len, fp); - if (line == NULL) break; - paGetToken(line, &option, &olen); + paGetToken(line, len, &option, &olen); if (olen == 0) continue; option[olen] = 0; - paGetToken(option + olen + 1, &value, &vlen); + paGetToken(option + olen + 1, len, &value, &vlen); if (vlen == 0) continue; value[vlen] = 0; @@ -885,44 +886,45 @@ void tsReadGlobalLogConfig() { bool tsReadGlobalConfig() { tsInitGlobalConfig(); - FILE * fp; char * line, *option, *value, *value1; - size_t len; int olen, vlen, vlen1; - char fileName[128]; + char fileName[PATH_MAX] = {0}; sprintf(fileName, "%s/taos.cfg", configDir); - fp = fopen(fileName, "r"); - if (fp == NULL) { - } else { - line = NULL; + FILE* fp = fopen(fileName, "r"); + + size_t len = 1024; + line = calloc(1, len); + + if (fp != NULL) { while (!feof(fp)) { - tfree(line); - line = option = value = NULL; - len = olen = vlen = 0; + memset(line, 0, len); + + option = value = NULL; + olen = vlen = 0; getline(&line, &len, fp); - if (line == NULL) break; - paGetToken(line, &option, &olen); + paGetToken(line, len, &option, &olen); if (olen == 0) continue; option[olen] = 0; - paGetToken(option + olen + 1, &value, &vlen); + paGetToken(option + olen + 1, len, &value, &vlen); if (vlen == 0) continue; value[vlen] = 0; // For dataDir, the format is: // dataDir /mnt/disk1 0 - paGetToken(value + vlen + 1, &value1, &vlen1); - + paGetToken(value + vlen + 1, len, &value1, &vlen1); + tsReadConfigOption(option, value); } - tfree(line); fclose(fp); } + tfree(line); + if (tsReadStorageConfig) { tsReadStorageConfig(); } @@ -976,6 +978,7 @@ bool tsReadGlobalConfig() { strcpy(tsLocalIp, tsPrivateIp); } + // todo refactor tsVersion = 0; for (int i = 0; i < 10; i++) { if (version[i] >= '0' && version[i] <= '9') { @@ -984,6 +987,7 @@ bool tsReadGlobalConfig() { break; } } + tsVersion = 10 * tsVersion; return true; @@ -994,10 +998,11 @@ int tsCfgDynamicOptions(char *msg) { int olen, vlen, code = 0; int vint = 0; - paGetToken(msg, &option, &olen); + size_t len = 120; + paGetToken(msg, len, &option, &olen); if (olen == 0) return TSDB_CODE_INVALID_MSG_CONTENT; - paGetToken(option + olen + 1, &value, &vlen); + paGetToken(option + olen + 1, len, &value, &vlen); if (vlen == 0) vint = 135; else { -- GitLab