diff --git a/src/util/inc/tutil.h b/src/util/inc/tutil.h index d38f983718cabf97f8954eb7787783055e411094..ddb7c04094bd0301b48f95db227b6528dd93254b 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 7aaa5b91db0f59cc7516b15051ba046153e0d135..48603a014e1479a5dfdd319f7b23f49669ee3f80 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 2288035fc7a67edcc11995b69253afbf6fe465c7..bcea8d16543cb26e6b2c951c83382794f7cf291d 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 3112fb411148925bf064b356df6e8dfc708ce86e..c76938d3aad47a416ee0a8e510d5ea3f7a6481c6 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 d1fb287184d85a4e6c7c68aa972e7ea39b75614c..39ec89daf4986632f50b4e11c6051b36e4366b7a 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 5bb120d4c6bfe49fe2ebd16949c5633b6d6d218d..a8d9e8d416ce9c9ee2653a665ecb080930e2c7ab 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 f1f34818655bd0ca70d62f1dfe81a5fb14238a68..4872c9298aa58ebad9c118b343573f724e8c4021 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 3b92cf21ee88003c48ee1e91587cac36ec17dcfd..61082b85e39764a148a81f53a45c607f04df3526 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 +}