From 89aa04debb96498cb96dac9ec1e2578052b30510 Mon Sep 17 00:00:00 2001 From: Hui Li Date: Thu, 18 Jun 2020 15:27:08 +0800 Subject: [PATCH] [modify for coverity scan] --- src/util/inc/tutil.h | 2 ++ src/util/src/tcache.c | 2 +- src/util/src/tconfig.c | 9 +++++++-- src/util/src/tdes.c | 2 +- src/util/src/tlog.c | 17 ++++++++++++----- src/util/src/tnote.c | 12 +++++++++--- src/util/src/tskiplist.c | 2 +- src/util/src/tutil.c | 31 +++++++++++++++++++++++++++---- 8 files changed, 60 insertions(+), 17 deletions(-) diff --git a/src/util/inc/tutil.h b/src/util/inc/tutil.h index d38f983718..ddb7c04094 100644 --- a/src/util/inc/tutil.h +++ b/src/util/inc/tutil.h @@ -116,6 +116,8 @@ extern "C" { #define POW2(x) ((x) * (x)) +int taosRand(void); + int32_t strdequote(char *src); size_t strtrim(char *src); diff --git a/src/util/src/tcache.c b/src/util/src/tcache.c index 7aaa5b91db..48603a014e 100644 --- a/src/util/src/tcache.c +++ b/src/util/src/tcache.c @@ -506,7 +506,7 @@ void taosAddToTrash(SCacheObj *pCacheObj, SCacheDataNode *pNode) { void taosRemoveFromTrashCan(SCacheObj *pCacheObj, STrashElem *pElem) { if (pElem->pData->signature != (uint64_t)pElem->pData) { - uError("key:sig:0x%x %p data has been released, ignore", pElem->pData->signature, pElem->pData); + uError("key:sig:0x%" PRIx64 " %p data has been released, ignore", pElem->pData->signature, pElem->pData); return; } diff --git a/src/util/src/tconfig.c b/src/util/src/tconfig.c index 2288035fc7..bcea8d1654 100644 --- a/src/util/src/tconfig.c +++ b/src/util/src/tconfig.c @@ -119,8 +119,13 @@ static void taosReadDirectoryConfig(SGlobalCfg *cfg, char *input_value) { struct stat dirstat; if (stat(option, &dirstat) < 0) { int code = mkdir(option, 0755); - uPrint("config option:%s, input value:%s, directory not exist, create with return code:%d", - cfg->option, input_value, code); + if (code < 0) { + uError("config option:%s, input value:%s, directory not exist, create fail with return code:%d", + cfg->option, input_value, code); + } else { + uPrint("config option:%s, input value:%s, directory not exist, create with return code:%d", + cfg->option, input_value, code); + } } cfg->cfgStatus = TAOS_CFG_CSTATUS_FILE; } else { diff --git a/src/util/src/tdes.c b/src/util/src/tdes.c index 3112fb4111..c76938d3aa 100644 --- a/src/util/src/tdes.c +++ b/src/util/src/tdes.c @@ -140,7 +140,7 @@ void print_char_as_binary(char input) { void generate_key(unsigned char* key) { int i; for (i = 0; i < 8; i++) { - key[i] = rand() % 255; + key[i] = taosRand() % 255; } } diff --git a/src/util/src/tlog.c b/src/util/src/tlog.c index d1fb287184..39ec89daf4 100644 --- a/src/util/src/tlog.c +++ b/src/util/src/tlog.c @@ -233,7 +233,9 @@ static void taosGetLogFileName(char *fn) { } } - strcpy(tsLogObj.logName, fn); + if (strlen(fn) < LOG_FILE_NAME_LEN) { + strcpy(tsLogObj.logName, fn); + } } static int32_t taosOpenLogFile(char *fn, int32_t maxLines, int32_t maxFileNum) { @@ -253,15 +255,20 @@ static int32_t taosOpenLogFile(char *fn, int32_t maxLines, int32_t maxFileNum) { tsLogObj.fileNum = maxFileNum; taosGetLogFileName(fn); - strcpy(name, fn); - strcat(name, ".0"); + if (strlen(fn) < LOG_FILE_NAME_LEN + 50 - 2) { + strcpy(name, fn); + strcat(name, ".0"); + } // if none of the log files exist, open 0, if both exists, open the old one if (stat(name, &logstat0) < 0) { tsLogObj.flag = 0; } else { - strcpy(name, fn); - strcat(name, ".1"); + if (strlen(fn) < LOG_FILE_NAME_LEN + 50 - 2) { + strcpy(name, fn); + strcat(name, ".1"); + } + if (stat(name, &logstat1) < 0) { tsLogObj.flag = 1; } else { diff --git a/src/util/src/tnote.c b/src/util/src/tnote.c index 5bb120d4c6..a8d9e8d416 100644 --- a/src/util/src/tnote.c +++ b/src/util/src/tnote.c @@ -169,7 +169,9 @@ void taosGetNoteName(char *fn, taosNoteInfo * pNote) } } - strcpy(pNote->taosNoteName, fn); + if (strlen(fn) < NOTE_FILE_NAME_LEN) { + strcpy(pNote->taosNoteName, fn); + } } int taosOpenNoteWithMaxLines(char *fn, int maxLines, int maxNoteNum, taosNoteInfo * pNote) @@ -182,14 +184,18 @@ int taosOpenNoteWithMaxLines(char *fn, int maxLines, int maxNoteNum, taosNoteInf pNote->taosNoteFileNum = maxNoteNum; taosGetNoteName(fn, pNote); + if (strlen(fn) > NOTE_FILE_NAME_LEN * 2 - 2) { + fprintf(stderr, "the len of file name overflow:%s\n", fn); + return -1; + } + strcpy(name, fn); strcat(name, ".0"); // if none of the note files exist, open 0, if both exists, open the old one if (stat(name, ¬estat0) < 0) { pNote->taosNoteFlag = 0; - } - else { + } else { strcpy(name, fn); strcat(name, ".1"); if (stat(name, ¬estat1) < 0) { diff --git a/src/util/src/tskiplist.c b/src/util/src/tskiplist.c index f1f3481865..4872c9298a 100644 --- a/src/util/src/tskiplist.c +++ b/src/util/src/tskiplist.c @@ -38,7 +38,7 @@ static FORCE_INLINE int32_t getSkipListNodeRandomHeight(SSkipList *pSkipList) { const uint32_t factor = 4; int32_t n = 1; - while ((rand() % factor) == 0 && n <= pSkipList->maxLevel) { + while ((taosRand() % factor) == 0 && n <= pSkipList->maxLevel) { n++; } diff --git a/src/util/src/tutil.c b/src/util/src/tutil.c index 3b92cf21ee..61082b85e3 100644 --- a/src/util/src/tutil.c +++ b/src/util/src/tutil.c @@ -27,6 +27,27 @@ #include "tulog.h" #include "taoserror.h" + +#ifdef WINDOWS +int taosRand(void) +{ + return rand(); +} +#else +int taosRand(void) +{ + int fd; + unsigned long seed; + + fd = open("/dev/urandom", 0); + if ((fd < 0) || (read(fd, &seed, sizeof(seed)) < 0)) seed = time(0); + if (fd >= 0) close(fd); + + srand(seed); + return rand(); +} +#endif + int32_t strdequote(char *z) { if (z == NULL) { return 0; @@ -434,8 +455,10 @@ void getTmpfilePath(const char *fileNamePrefix, char *dstPath) { strcpy(tmpPath, tmpDir); strcat(tmpPath, tdengineTmpFileNamePrefix); - strcat(tmpPath, fileNamePrefix); - strcat(tmpPath, "-%d-%s"); + if (strlen(tmpPath) + strlen(fileNamePrefix) + strlen("-%d-%s") < PATH_MAX) { + strcat(tmpPath, fileNamePrefix); + strcat(tmpPath, "-%d-%s"); + } char rand[8] = {0}; taosRandStr(rand, tListLen(rand) - 1); @@ -447,7 +470,7 @@ void taosRandStr(char* str, int32_t size) { int32_t len = 39; for(int32_t i = 0; i < size; ++i) { - str[i] = set[rand()%len]; + str[i] = set[taosRand()%len]; } } @@ -718,4 +741,4 @@ void taosRemoveDir(char *rootDir) { rmdir(rootDir); uPrint("dir:%s is removed", rootDir); -} \ No newline at end of file +} -- GitLab