diff --git a/include/common/ttime.h b/include/common/ttime.h index d430f7bd2a..65bb763b1f 100644 --- a/include/common/ttime.h +++ b/include/common/ttime.h @@ -64,7 +64,7 @@ static FORCE_INLINE int64_t taosGetTimestampToday(int32_t precision) { : 1000000000; time_t t = taosTime(NULL); struct tm tm; - (void) taosLocalTime(&t, &tm, NULL); + (void) taosLocalTime(&t, &tm, NULL, 0); tm.tm_hour = 0; tm.tm_min = 0; tm.tm_sec = 0; diff --git a/include/os/osDef.h b/include/os/osDef.h index ff30265afa..be37c50264 100644 --- a/include/os/osDef.h +++ b/include/os/osDef.h @@ -247,9 +247,12 @@ void syslog(int unused, const char *format, ...); #define TD_DIRSEP_CHAR '/' #endif +#define TD_FQDN_LEN 128 #define TD_LOCALE_LEN 64 #define TD_CHARSET_LEN 64 #define TD_TIMEZONE_LEN 96 +#define TD_TIME_STR_LEN 128 +#define TD_IP_LEN 64 #ifdef __cplusplus } diff --git a/include/os/osString.h b/include/os/osString.h index b184e7efdb..af94646dac 100644 --- a/include/os/osString.h +++ b/include/os/osString.h @@ -87,7 +87,7 @@ bool taosMbsToUcs4(const char *mbs, size_t mbs_len, TdUcs4 *ucs4, int32_t ucs int32_t tasoUcs4Compare(TdUcs4 *f1_ucs4, TdUcs4 *f2_ucs4, int32_t bytes); int32_t tasoUcs4Copy(TdUcs4 *target_ucs4, TdUcs4 *source_ucs4, int32_t len_ucs4); bool taosValidateEncodec(const char *encodec); -int32_t taosHexEncode(const unsigned char *src, char *dst, int32_t len); +int32_t taosHexEncode(const unsigned char *src, char *dst, int32_t len, int32_t bufSize); int32_t taosHexDecode(const char *src, char *dst, int32_t len); int32_t taosWcharWidth(TdWchar wchar); diff --git a/include/os/osTime.h b/include/os/osTime.h index c367416175..5d74146e9c 100644 --- a/include/os/osTime.h +++ b/include/os/osTime.h @@ -91,7 +91,7 @@ static FORCE_INLINE int64_t taosGetMonoTimestampMs() { } char *taosStrpTime(const char *buf, const char *fmt, struct tm *tm); -struct tm *taosLocalTime(const time_t *timep, struct tm *result, char *buf); +struct tm *taosLocalTime(const time_t *timep, struct tm *result, char *buf, int32_t bufSize); struct tm *taosLocalTimeNolock(struct tm *result, const time_t *timep, int dst); time_t taosTime(time_t *t); time_t taosMktime(struct tm *timep); diff --git a/include/util/tdef.h b/include/util/tdef.h index 768ff82ade..b4cb1bdd1c 100644 --- a/include/util/tdef.h +++ b/include/util/tdef.h @@ -284,7 +284,7 @@ typedef enum ELogicConditionType { #define TSDB_CLUSTER_ID_LEN 40 #define TSDB_MACHINE_ID_LEN 24 -#define TSDB_FQDN_LEN 128 +#define TSDB_FQDN_LEN TD_FQDN_LEN #define TSDB_EP_LEN (TSDB_FQDN_LEN + 6) #define TSDB_IPv4ADDR_LEN 16 #define TSDB_FILENAME_LEN 128 diff --git a/source/common/src/tdatablock.c b/source/common/src/tdatablock.c index 7a67522231..d727be30ce 100644 --- a/source/common/src/tdatablock.c +++ b/source/common/src/tdatablock.c @@ -2446,7 +2446,7 @@ _error: return NULL; } -static char* formatTimestamp(char* buf, int64_t val, int precision) { +static char* formatTimestamp(char* buf, int32_t bufSize, int64_t val, int precision) { time_t tt; int32_t ms = 0; if (precision == TSDB_TIME_PRECISION_NANO) { @@ -2479,11 +2479,11 @@ static char* formatTimestamp(char* buf, int64_t val, int precision) { } } struct tm ptm = {0}; - if (taosLocalTime(&tt, &ptm, buf) == NULL) { + if (taosLocalTime(&tt, &ptm, buf, bufSize) == NULL) { return buf; } - size_t pos = strftime(buf, 35, "%Y-%m-%d %H:%M:%S", &ptm); + size_t pos = strftime(buf, bufSize, "%Y-%m-%d %H:%M:%S", &ptm); if (precision == TSDB_TIME_PRECISION_NANO) { sprintf(buf + pos, ".%09d", ms); } else if (precision == TSDB_TIME_PRECISION_MICRO) { @@ -2500,7 +2500,7 @@ int32_t dumpBlockData(SSDataBlock* pDataBlock, const char* flag, char** pDataBuf int32_t size = 2048 * 1024; int32_t code = 0; char* dumpBuf = NULL; - char pBuf[128] = {0}; + char pBuf[TD_TIME_STR_LEN] = {0}; int32_t rows = pDataBlock->info.rows; int32_t len = 0; @@ -2543,7 +2543,7 @@ int32_t dumpBlockData(SSDataBlock* pDataBlock, const char* flag, char** pDataBuf switch (pColInfoData->info.type) { case TSDB_DATA_TYPE_TIMESTAMP: memset(pBuf, 0, sizeof(pBuf)); - (void)formatTimestamp(pBuf, *(uint64_t*)var, pColInfoData->info.precision); + (void)formatTimestamp(pBuf, sizeof(pBuf), *(uint64_t*)var, pColInfoData->info.precision); len += snprintf(dumpBuf + len, size - len, " %25s |", pBuf); if (len >= size - 1) goto _exit; break; diff --git a/source/common/src/tname.c b/source/common/src/tname.c index a9dda87591..b012e8fe9e 100644 --- a/source/common/src/tname.c +++ b/source/common/src/tname.c @@ -33,7 +33,7 @@ int64_t taosGetIntervalStartTimestamp(int64_t startTime, int64_t slidingTime, in } struct tm tm; time_t t = (time_t)start; - taosLocalTime(&t, &tm); + taosLocalTime(&t, &tm, NULL, 0); tm.tm_sec = 0; tm.tm_min = 0; tm.tm_hour = 0; diff --git a/source/common/src/ttime.c b/source/common/src/ttime.c index 98e46ab672..b4dec2fcd1 100644 --- a/source/common/src/ttime.c +++ b/source/common/src/ttime.c @@ -693,7 +693,7 @@ int64_t taosTimeAdd(int64_t t, int64_t duration, char unit, int32_t precision) { struct tm tm; time_t tt = (time_t)(t / TSDB_TICK_PER_SECOND(precision)); - struct tm* ptm = taosLocalTime(&tt, &tm, NULL); + struct tm* ptm = taosLocalTime(&tt, &tm, NULL, 0); int32_t mon = tm.tm_year * 12 + tm.tm_mon + (int32_t)numOfMonth; tm.tm_year = mon / 12; tm.tm_mon = mon % 12; @@ -754,11 +754,11 @@ int32_t taosTimeCountIntervalForFill(int64_t skey, int64_t ekey, int64_t interva struct tm tm; time_t t = (time_t)skey; - struct tm* ptm = taosLocalTime(&t, &tm, NULL); + struct tm* ptm = taosLocalTime(&t, &tm, NULL, 0); int32_t smon = tm.tm_year * 12 + tm.tm_mon; t = (time_t)ekey; - ptm = taosLocalTime(&t, &tm, NULL); + ptm = taosLocalTime(&t, &tm, NULL, 0); int32_t emon = tm.tm_year * 12 + tm.tm_mon; if (unit == 'y') { @@ -782,7 +782,7 @@ int64_t taosTimeTruncate(int64_t ts, const SInterval* pInterval) { start /= (int64_t)(TSDB_TICK_PER_SECOND(precision)); struct tm tm; time_t tt = (time_t)start; - struct tm* ptm = taosLocalTime(&tt, &tm, NULL); + struct tm* ptm = taosLocalTime(&tt, &tm, NULL, 0); tm.tm_sec = 0; tm.tm_min = 0; tm.tm_hour = 0; @@ -911,13 +911,13 @@ int64_t taosTimeGetIntervalEnd(int64_t intervalStart, const SInterval* pInterval // 2020-07-03 17:48:42 // and the parameter can also be a variable. const char* fmtts(int64_t ts) { - static char buf[96] = {0}; + static char buf[TD_TIME_STR_LEN] = {0}; size_t pos = 0; struct tm tm; if (ts > -62135625943 && ts < 32503651200) { time_t t = (time_t)ts; - if (taosLocalTime(&t, &tm, buf) == NULL) { + if (taosLocalTime(&t, &tm, buf, sizeof(buf)) == NULL) { return buf; } pos += strftime(buf + pos, sizeof(buf), "s=%Y-%m-%d %H:%M:%S", &tm); @@ -925,7 +925,7 @@ const char* fmtts(int64_t ts) { if (ts > -62135625943000 && ts < 32503651200000) { time_t t = (time_t)(ts / 1000); - if (taosLocalTime(&t, &tm, buf) == NULL) { + if (taosLocalTime(&t, &tm, buf, sizeof(buf)) == NULL) { return buf; } if (pos > 0) { @@ -939,7 +939,7 @@ const char* fmtts(int64_t ts) { { time_t t = (time_t)(ts / 1000000); - if (taosLocalTime(&t, &tm, buf) == NULL) { + if (taosLocalTime(&t, &tm, buf, sizeof(buf)) == NULL) { return buf; } if (pos > 0) { @@ -993,7 +993,7 @@ int32_t taosFormatUtcTime(char* buf, int32_t bufLen, int64_t t, int32_t precisio TAOS_RETURN(TSDB_CODE_INVALID_PARA); } - if (NULL == taosLocalTime(", &ptm, buf)) { + if (NULL == taosLocalTime(", &ptm, buf, bufLen)) { TAOS_RETURN(TAOS_SYSTEM_ERROR(errno)); } int32_t length = (int32_t)strftime(ts, 40, "%Y-%m-%dT%H:%M:%S", &ptm); @@ -1007,7 +1007,7 @@ int32_t taosFormatUtcTime(char* buf, int32_t bufLen, int64_t t, int32_t precisio int32_t taosTs2Tm(int64_t ts, int32_t precision, struct STm* tm) { tm->fsec = ts % TICK_PER_SECOND[precision] * (TICK_PER_SECOND[TSDB_TIME_PRECISION_NANO] / TICK_PER_SECOND[precision]); time_t t = ts / TICK_PER_SECOND[precision]; - if (NULL == taosLocalTime(&t, &tm->tm, NULL)) { + if (NULL == taosLocalTime(&t, &tm->tm, NULL, 0)) { TAOS_RETURN(TAOS_SYSTEM_ERROR(errno)); } return TSDB_CODE_SUCCESS; diff --git a/source/libs/catalog/src/ctgAsync.c b/source/libs/catalog/src/ctgAsync.c index 525573ee01..c1dcdf2741 100644 --- a/source/libs/catalog/src/ctgAsync.c +++ b/source/libs/catalog/src/ctgAsync.c @@ -2916,7 +2916,7 @@ int32_t ctgHandleGetTbTSMARsp(SCtgTaskReq* tReq, int32_t reqType, const SDataBuf if (META_TYPE_BOTH_TABLE == pOut->metaType) { // rewrite tsma fetch table with it's super table name - (void)sprintf(pFetch->tsmaSourceTbName.tname, "%s", pOut->tbName); + (void)snprintf(pFetch->tsmaSourceTbName.tname, sizeof(pFetch->tsmaSourceTbName.tname), "%s", pOut->tbName); } CTG_ERR_JRET(ctgGetTbTSMAFromMnode(pCtg, pConn, &pFetch->tsmaSourceTbName, NULL, tReq, TDMT_MND_GET_TABLE_TSMA)); diff --git a/source/libs/catalog/src/ctgDbg.c b/source/libs/catalog/src/ctgDbg.c index 0ac606d4d6..f3a0b04457 100644 --- a/source/libs/catalog/src/ctgDbg.c +++ b/source/libs/catalog/src/ctgDbg.c @@ -176,22 +176,22 @@ int32_t ctgdLaunchAsyncCall(SCatalog *pCtg, SRequestConnInfo *pConn, uint64_t re taosArrayPush(req.pTableMeta, &name); taosArrayPush(req.pTableHash, &name); - strcpy(dbFName, "1.db1"); + tstrncpy(dbFName, "1.db1", sizeof(dbFName)); taosArrayPush(req.pDbVgroup, dbFName); taosArrayPush(req.pDbCfg, dbFName); taosArrayPush(req.pDbInfo, dbFName); - strcpy(dbFName, "1.db2"); + tstrncpy(dbFName, "1.db2", sizeof(dbFName)); taosArrayPush(req.pDbVgroup, dbFName); taosArrayPush(req.pDbCfg, dbFName); taosArrayPush(req.pDbInfo, dbFName); - strcpy(funcName, "udf1"); + tstrncpy(funcName, "udf1", sizeof(funcName)); taosArrayPush(req.pUdf, funcName); - strcpy(funcName, "udf2"); + tstrncpy(funcName, "udf2", sizeof(funcName)); taosArrayPush(req.pUdf, funcName); - strcpy(user.user, "root"); - strcpy(user.dbFName, "1.db1"); + tstrncpy(user.user, "root", sizeof(user.user)); + tstrncpy(user.dbFName, "1.db1", sizeof(user.dbFName)); user.type = AUTH_TYPE_READ; taosArrayPush(req.pUser, &user); user.type = AUTH_TYPE_WRITE; @@ -199,8 +199,8 @@ int32_t ctgdLaunchAsyncCall(SCatalog *pCtg, SRequestConnInfo *pConn, uint64_t re user.type = AUTH_TYPE_OTHER; taosArrayPush(req.pUser, &user); - strcpy(user.user, "user1"); - strcpy(user.dbFName, "1.db2"); + tstrncpy(user.user, "user1", sizeof(user.user)); + tstrncpy(user.dbFName, "1.db2", sizeof(user.dbFName)); user.type = AUTH_TYPE_READ; taosArrayPush(req.pUser, &user); user.type = AUTH_TYPE_WRITE; @@ -335,7 +335,7 @@ int32_t ctgdHandleDbgCommand(char *command) { CTG_RET(TSDB_CODE_INVALID_PARA); } - bool enable = atoi(param); + bool enable = taosStr2Int32(param, NULL, 10); int32_t code = ctgdEnableDebug(option, enable); diff --git a/source/libs/catalog/src/ctgRemote.c b/source/libs/catalog/src/ctgRemote.c index ed9dc81dd7..46a615aeed 100644 --- a/source/libs/catalog/src/ctgRemote.c +++ b/source/libs/catalog/src/ctgRemote.c @@ -1303,7 +1303,7 @@ int32_t ctgGetTbMetaFromMnodeImpl(SCatalog* pCtg, SRequestConnInfo* pConn, const int32_t msgLen = 0; int32_t reqType = TDMT_MND_TABLE_META; char tbFName[TSDB_TABLE_FNAME_LEN]; - (void)sprintf(tbFName, "%s.%s", dbFName, tbName); + (void)snprintf(tbFName, sizeof(tbFName), "%s.%s", dbFName, tbName); void* (*mallocFp)(int64_t) = pTask ? (MallocType)taosMemoryMalloc : (MallocType)rpcMallocCont; ctgDebug("try to get table meta from mnode, tbFName:%s", tbFName); @@ -1369,7 +1369,7 @@ int32_t ctgGetTbMetaFromVnode(SCatalog* pCtg, SRequestConnInfo* pConn, const SNa (void)tNameGetFullDbName(pTableName, dbFName); int32_t reqType = (pTask && pTask->type == CTG_TASK_GET_TB_NAME ? TDMT_VND_TABLE_NAME : TDMT_VND_TABLE_META); char tbFName[TSDB_TABLE_FNAME_LEN]; - (void)sprintf(tbFName, "%s.%s", dbFName, pTableName->tname); + (void)snprintf(tbFName, sizeof(tbFName), "%s.%s", dbFName, pTableName->tname); void* (*mallocFp)(int64_t) = pTask ? (MallocType)taosMemoryMalloc : (MallocType)rpcMallocCont; SEp* pEp = &vgroupInfo->epSet.eps[vgroupInfo->epSet.inUse]; diff --git a/source/libs/catalog/src/ctgUtil.c b/source/libs/catalog/src/ctgUtil.c index e7759bcc7d..f8591b2121 100644 --- a/source/libs/catalog/src/ctgUtil.c +++ b/source/libs/catalog/src/ctgUtil.c @@ -1386,7 +1386,7 @@ int32_t ctgGetVgInfosFromHashValue(SCatalog* pCtg, SEpSet* pMgmgEpSet, SCtgTaskR } char tbFullName[TSDB_TABLE_FNAME_LEN]; - (void)sprintf(tbFullName, "%s.", dbFName); + (void)snprintf(tbFullName, sizeof(tbFullName), "%s.", dbFName); int32_t offset = strlen(tbFullName); SName* pName = NULL; int32_t tbNameLen = 0; @@ -2070,7 +2070,7 @@ int32_t ctgChkSetTbAuthRes(SCatalog* pCtg, SCtgAuthReq* req, SCtgAuthRsp* res) { continue; } - (void)sprintf(tbFName, "%s.%s", dbFName, stbName); + (void)snprintf(tbFName, sizeof(tbFName), "%s.%s", dbFName, stbName); continue; } diff --git a/source/libs/executor/src/groupcacheoperator.c b/source/libs/executor/src/groupcacheoperator.c index 13aff27d68..648a2ea6d2 100644 --- a/source/libs/executor/src/groupcacheoperator.c +++ b/source/libs/executor/src/groupcacheoperator.c @@ -78,14 +78,15 @@ static void logGroupCacheExecInfo(SGroupCacheOperatorInfo* pGrpCacheOperator) { if (pGrpCacheOperator->downstreamNum <= 0 || NULL == pGrpCacheOperator->execInfo.pDownstreamBlkNum) { return; } - - char* buf = taosMemoryMalloc(pGrpCacheOperator->downstreamNum * 32 + 100); + + int32_t bufSize = pGrpCacheOperator->downstreamNum * 32 + 100; + char* buf = taosMemoryMalloc(bufSize); if (NULL == buf) { return; } - int32_t offset = sprintf(buf, "groupCache exec info, downstreamBlkNum:"); + int32_t offset = snprintf(buf, bufSize, "groupCache exec info, downstreamBlkNum:"); for (int32_t i = 0; i < pGrpCacheOperator->downstreamNum; ++i) { - offset += sprintf(buf + offset, " %" PRId64 , pGrpCacheOperator->execInfo.pDownstreamBlkNum[i]); + offset += snprintf(buf + offset, bufSize, " %" PRId64 , pGrpCacheOperator->execInfo.pDownstreamBlkNum[i]); } qDebug("%s", buf); taosMemoryFree(buf); @@ -234,7 +235,7 @@ static int32_t acquireFdFromFileCtx(SGcFileCacheCtx* pFileCtx, int32_t fileId, S SGroupCacheFileInfo* pTmp = taosHashGet(pFileCtx->pCacheFile, &fileId, sizeof(fileId)); if (NULL == pTmp) { - (void)sprintf(&pFileCtx->baseFilename[pFileCtx->baseNameLen], "_%d", fileId); + (void)snprintf(&pFileCtx->baseFilename[pFileCtx->baseNameLen], sizeof(pFileCtx->baseFilename) - pFileCtx->baseNameLen, "_%d", fileId); SGroupCacheFileInfo newFile = {0}; if (taosHashPut(pFileCtx->pCacheFile, &fileId, sizeof(fileId), &newFile, sizeof(newFile))) { @@ -439,7 +440,7 @@ static FORCE_INLINE void chkRemoveVgroupCurrFile(SGcFileCacheCtx* pFileCtx, int3 #if 0 /* debug only */ - sprintf(&pFileCtx->baseFilename[pFileCtx->baseNameLen], "_%d", pFileCtx->fileId); + snprintf(&pFileCtx->baseFilename[pFileCtx->baseNameLen], sizeof(pFileCtx->baseFilename) - pFileCtx->baseNameLen, "_%d", pFileCtx->fileId); taosRemoveFile(pFileCtx->baseFilename); /* debug only */ #endif @@ -813,7 +814,7 @@ static int32_t addFileRefTableNum(SGcFileCacheCtx* pFileCtx, int32_t fileId, int SGroupCacheFileInfo* pTmp = taosHashGet(pFileCtx->pCacheFile, &fileId, sizeof(fileId)); if (NULL == pTmp) { - (void)sprintf(&pFileCtx->baseFilename[pFileCtx->baseNameLen], "_%u", fileId); + (void)snprintf(&pFileCtx->baseFilename[pFileCtx->baseNameLen], sizeof(pFileCtx->baseFilename) - pFileCtx->baseNameLen, "_%u", fileId); SGroupCacheFileInfo newFile = {0}; newFile.groupNum = 1; @@ -1377,7 +1378,7 @@ static void freeRemoveGroupCacheData(void* p) { #if 0 /* debug only */ - sprintf(&pFileCtx->baseFilename[pFileCtx->baseNameLen], "_%d", pGroup->fileId); + snprintf(&pFileCtx->baseFilename[pFileCtx->baseNameLen], sizeof(pFileCtx->baseFilename) - pFileCtx->baseNameLen, "_%d", pGroup->fileId); taosRemoveFile(pFileCtx->baseFilename); /* debug only */ #endif diff --git a/source/libs/function/src/builtins.c b/source/libs/function/src/builtins.c index 9364c313ca..1fd99125a0 100644 --- a/source/libs/function/src/builtins.c +++ b/source/libs/function/src/builtins.c @@ -209,10 +209,10 @@ static int32_t countTrailingSpaces(const SValueNode* pVal, bool isLtrim) { } static int32_t addTimezoneParam(SNodeList* pList) { - char buf[6] = {0}; + char buf[TD_TIME_STR_LEN] = {0}; time_t t = taosTime(NULL); struct tm tmInfo; - if (taosLocalTime(&t, &tmInfo, buf) != NULL) { + if (taosLocalTime(&t, &tmInfo, buf, sizeof(buf)) != NULL) { (void)strftime(buf, sizeof(buf), "%z", &tmInfo); } int32_t len = (int32_t)strlen(buf); diff --git a/source/libs/nodes/src/nodesCodeFuncs.c b/source/libs/nodes/src/nodesCodeFuncs.c index 830284cdcc..0f67493094 100644 --- a/source/libs/nodes/src/nodesCodeFuncs.c +++ b/source/libs/nodes/src/nodesCodeFuncs.c @@ -4058,9 +4058,10 @@ static int32_t datumToJson(const void* pObj, SJson* pJson) { break; case TSDB_DATA_TYPE_NCHAR: { // cJSON only support utf-8 encoding. Convert memory content to hex string. - char* buf = taosMemoryCalloc(varDataLen(pNode->datum.p) * 2 + 1, sizeof(char)); + int32_t bufSize = varDataLen(pNode->datum.p) * 2 + 1; + char* buf = taosMemoryCalloc(bufSize, sizeof(char)); if (!buf) return terrno; - code = taosHexEncode(varDataVal(pNode->datum.p), buf, varDataLen(pNode->datum.p)); + code = taosHexEncode(varDataVal(pNode->datum.p), buf, varDataLen(pNode->datum.p), bufSize); if (code != TSDB_CODE_SUCCESS) { taosMemoryFree(buf); return TSDB_CODE_TSC_INVALID_VALUE; @@ -4076,9 +4077,10 @@ static int32_t datumToJson(const void* pObj, SJson* pJson) { break; case TSDB_DATA_TYPE_JSON: { int32_t len = getJsonValueLen(pNode->datum.p); - char* buf = taosMemoryCalloc(len * 2 + 1, sizeof(char)); + int32_t bufSize = len * 2 + 1; + char* buf = taosMemoryCalloc(bufSize, sizeof(char)); if (!buf) return terrno; - code = taosHexEncode(pNode->datum.p, buf, len); + code = taosHexEncode(pNode->datum.p, buf, len, bufSize); if (code != TSDB_CODE_SUCCESS) { taosMemoryFree(buf); return TSDB_CODE_TSC_INVALID_VALUE; diff --git a/source/libs/qworker/src/qwUtil.c b/source/libs/qworker/src/qwUtil.c index d6266afa02..bebb9b288a 100644 --- a/source/libs/qworker/src/qwUtil.c +++ b/source/libs/qworker/src/qwUtil.c @@ -539,7 +539,7 @@ int32_t qwSaveTbVersionInfo(qTaskInfo_t pTaskInfo, SQWTaskCtx *ctx) { } if (dbFName[0] && tbName[0]) { - (void)sprintf(tbInfo.tbFName, "%s.%s", dbFName, tbName); + (void)snprintf(tbInfo.tbFName, sizeof(tbInfo.tbFName), "%s.%s", dbFName, tbName); } else { tbInfo.tbFName[0] = 0; } diff --git a/source/libs/scalar/src/sclfunc.c b/source/libs/scalar/src/sclfunc.c index f408314fad..71e29a7aa1 100644 --- a/source/libs/scalar/src/sclfunc.c +++ b/source/libs/scalar/src/sclfunc.c @@ -2193,7 +2193,7 @@ int32_t toISO8601Function(SScalarParam *pInput, int32_t inputNum, SScalarParam * NUM_TO_STRING(type, input, sizeof(fraction), fraction); int32_t fractionLen; - char buf[64] = {0}; + char buf[TD_TIME_STR_LEN] = {0}; int64_t timeVal; char* format = NULL; int64_t quot = 0; @@ -2244,7 +2244,7 @@ int32_t toISO8601Function(SScalarParam *pInput, int32_t inputNum, SScalarParam * struct tm tmInfo; int32_t len = 0; - if (taosLocalTime((const time_t *)", &tmInfo, buf) == NULL) { + if (taosLocalTime((const time_t *)", &tmInfo, buf, sizeof(buf)) == NULL) { len = (int32_t)strlen(buf); goto _end; } diff --git a/source/libs/scheduler/src/schRemote.c b/source/libs/scheduler/src/schRemote.c index b3106d8c7e..a8e747ccd2 100644 --- a/source/libs/scheduler/src/schRemote.c +++ b/source/libs/scheduler/src/schRemote.c @@ -729,7 +729,7 @@ int32_t schMakeHbCallbackParam(SSchJob *pJob, SSchTask *pTask, void **pParam) { param->nodeEpId.nodeId = addr->nodeId; SEp* pEp = SCH_GET_CUR_EP(addr); - strcpy(param->nodeEpId.ep.fqdn, pEp->fqdn); + tstrncpy(param->nodeEpId.ep.fqdn, pEp->fqdn, sizeof(param->nodeEpId.ep.fqdn)); param->nodeEpId.ep.port = pEp->port; param->pTrans = pJob->pTrans; diff --git a/source/libs/scheduler/src/schTask.c b/source/libs/scheduler/src/schTask.c index 4c609fa5e2..e6b68051f9 100644 --- a/source/libs/scheduler/src/schTask.c +++ b/source/libs/scheduler/src/schTask.c @@ -288,7 +288,7 @@ int32_t schProcessOnTaskSuccess(SSchJob *pJob, SSchTask *pTask) { /* if (SCH_IS_DATA_SRC_TASK(task) && job->dataSrcEps.numOfEps < SCH_MAX_CANDIDATE_EP_NUM) { - strncpy(job->dataSrcEps.fqdn[job->dataSrcEps.numOfEps], task->execAddr.fqdn, sizeof(task->execAddr.fqdn)); + tstrncpy(job->dataSrcEps.fqdn[job->dataSrcEps.numOfEps], task->execAddr.fqdn, sizeof(task->execAddr.fqdn)); job->dataSrcEps.port[job->dataSrcEps.numOfEps] = task->execAddr.port; ++job->dataSrcEps.numOfEps; @@ -840,7 +840,7 @@ int32_t schSetTaskCandidateAddrs(SSchJob *pJob, SSchTask *pTask) { /* for (int32_t i = 0; i < job->dataSrcEps.numOfEps && addNum < SCH_MAX_CANDIDATE_EP_NUM; ++i) { - strncpy(epSet->fqdn[epSet->numOfEps], job->dataSrcEps.fqdn[i], sizeof(job->dataSrcEps.fqdn[i])); + tstrncpy(epSet->fqdn[epSet->numOfEps], job->dataSrcEps.fqdn[i], sizeof(job->dataSrcEps.fqdn[i])); epSet->port[epSet->numOfEps] = job->dataSrcEps.port[i]; ++epSet->numOfEps; diff --git a/source/libs/sync/src/syncUtil.c b/source/libs/sync/src/syncUtil.c index 6f7ea9375c..3cf62cb87c 100644 --- a/source/libs/sync/src/syncUtil.c +++ b/source/libs/sync/src/syncUtil.c @@ -60,7 +60,7 @@ bool syncUtilNodeInfo2RaftId(const SNodeInfo* pInfo, SyncGroupId vgId, SRaftId* return false; } - char ipbuf[128] = {0}; + char ipbuf[TD_IP_LEN] = {0}; tinet_ntoa(ipbuf, ipv4); raftId->addr = SYNC_ADDR(pInfo); raftId->vgId = vgId; diff --git a/source/libs/transport/src/thttp.c b/source/libs/transport/src/thttp.c index 7d7868f3cd..bdcbfeb1cd 100644 --- a/source/libs/transport/src/thttp.c +++ b/source/libs/transport/src/thttp.c @@ -223,7 +223,7 @@ static FORCE_INLINE int32_t taosBuildDstAddr(const char* server, uint16_t port, tError("http-report failed to resolving domain names %s, reason: %s", server, tstrerror(code)); return TSDB_CODE_RPC_FQDN_ERROR; } - char buf[256] = {0}; + char buf[TD_IP_LEN] = {0}; tinet_ntoa(buf, ip); int ret = uv_ip4_addr(buf, port, dest); if (ret != 0) { diff --git a/source/libs/transport/src/transCli.c b/source/libs/transport/src/transCli.c index c0453c7759..c3f9819119 100644 --- a/source/libs/transport/src/transCli.c +++ b/source/libs/transport/src/transCli.c @@ -1790,7 +1790,7 @@ static FORCE_INLINE int32_t cliUpdateFqdnCache(SHashObj* cache, char* fqdn) { size_t len = strlen(fqdn); uint32_t* v = taosHashGet(cache, fqdn, len); if (addr != *v) { - char old[64] = {0}, new[64] = {0}; + char old[TD_IP_LEN] = {0}, new[TD_IP_LEN] = {0}; tinet_ntoa(old, *v); tinet_ntoa(new, addr); tWarn("update ip of fqdn:%s, old: %s, new: %s", fqdn, old, new); diff --git a/source/os/src/osDir.c b/source/os/src/osDir.c index 04747cacd4..497769a71c 100644 --- a/source/os/src/osDir.c +++ b/source/os/src/osDir.c @@ -151,7 +151,7 @@ int32_t taosMulMkDir(const char *dirname) { } if (temp[1] == ':') pos += 3; #else - (void)strcpy(temp, dirname); + tstrncpy(temp, dirname, sizeof(temp)); #endif if (taosDirExist(temp)) return code; @@ -216,7 +216,7 @@ int32_t taosMulModeMkDir(const char *dirname, int mode, bool checkAccess) { } if (temp[1] == ':') pos += 3; #else - (void)strcpy(temp, dirname); + tstrncpy(temp, dirname, sizeof(temp)); #endif if (taosDirExist(temp)) { @@ -341,7 +341,7 @@ int32_t taosExpandDir(const char *dirname, char *outname, int32_t maxlen) { } if (full_path.we_wordv != NULL && full_path.we_wordv[0] != NULL) { - (void)strncpy(outname, full_path.we_wordv[0], maxlen); + tstrncpy(outname, full_path.we_wordv[0], maxlen); } wordfree(&full_path); @@ -358,9 +358,9 @@ int32_t taosRealPath(char *dirname, char *realPath, int32_t maxlen) { #endif if (strlen(tmp) < maxlen) { if (realPath == NULL) { - (void)strncpy(dirname, tmp, maxlen); + tstrncpy(dirname, tmp, maxlen); } else { - (void)strncpy(realPath, tmp, maxlen); + tstrncpy(realPath, tmp, maxlen); } return 0; } @@ -440,8 +440,7 @@ TdDirPtr taosOpenDir(const char *dirname) { return NULL; } - strcpy(szFind, dirname); - strcat(szFind, "\\*.*"); //利用通配符找这个目录下的所以文件,包括目录 + snprintf(szFind, sizeof(szFind), "%s%s", dirname, "\\*.*"); //利用通配符找这个目录下的所以文件,包括目录 pDir->hFind = FindFirstFile(szFind, &(pDir->dirEntry.findFileData)); if (INVALID_HANDLE_VALUE == pDir->hFind) { @@ -560,6 +559,6 @@ void taosGetCwd(char *buf, int32_t len) { char *unused __attribute__((unused)); unused = getcwd(buf, len - 1); #else - strncpy(buf, "not implemented on windows", len - 1); + tstrncpy(buf, "not implemented on windows", len); #endif } diff --git a/source/os/src/osEnv.c b/source/os/src/osEnv.c index f2c90e778d..14efa1b534 100644 --- a/source/os/src/osEnv.c +++ b/source/os/src/osEnv.c @@ -77,21 +77,21 @@ int32_t osDefaultInit() { tmpDir = getenv("temp"); } if (tmpDir != NULL) { - (void)strcpy(tsTempDir, tmpDir); + tstrncpy(tsTempDir, tmpDir, sizeof(tsTempDir)); } - (void)strcpy(tsOsName, "Windows"); + tstrncpy(tsOsName, "Windows", sizeof(tsOsName)); #elif defined(_TD_DARWIN_64) - (void)strcpy(tsOsName, "Darwin"); + tstrncpy(tsOsName, "Darwin", sizeof(tsOsName)); #else - (void)strcpy(tsOsName, "Linux"); + tstrncpy(tsOsName, "Linux", sizeof(tsOsName)); #endif if (configDir[0] == 0) { - (void)strcpy(configDir, TD_CFG_DIR_PATH); + tstrncpy(configDir, TD_CFG_DIR_PATH, sizeof(configDir)); } - (void)strcpy(tsDataDir, TD_DATA_DIR_PATH); - (void)strcpy(tsLogDir, TD_LOG_DIR_PATH); - if(strlen(tsTempDir) == 0){ - (void)strcpy(tsTempDir, TD_TMP_DIR_PATH); + tstrncpy(tsDataDir, TD_DATA_DIR_PATH, sizeof(tsDataDir)); + tstrncpy(tsLogDir, TD_LOG_DIR_PATH, sizeof(tsLogDir)); + if (strlen(tsTempDir) == 0){ + tstrncpy(tsTempDir, TD_TMP_DIR_PATH, sizeof(tsTempDir)); } return code; diff --git a/source/os/src/osFile.c b/source/os/src/osFile.c index ef8c1eb860..46cca99e5f 100644 --- a/source/os/src/osFile.c +++ b/source/os/src/osFile.c @@ -91,11 +91,7 @@ void taosGetTmpfilePath(const char *inputTmpDir, const char *fileNamePrefix, cha tmpPath[len++] = '\\'; } - strcpy(tmpPath + len, TD_TMP_FILE_PREFIX); - if (strlen(tmpPath) + strlen(fileNamePrefix) + strlen("-%d-%s") < PATH_MAX) { - strcat(tmpPath, fileNamePrefix); - strcat(tmpPath, "-%d-%s"); - } + snprintf(tmpPath + len, sizeof(tmpPath) - len, "%s%s%s", TD_TMP_FILE_PREFIX, fileNamePrefix, "-%d-%s"); char rand[8] = {0}; taosRandStr(rand, tListLen(rand) - 1); @@ -112,15 +108,11 @@ void taosGetTmpfilePath(const char *inputTmpDir, const char *fileNamePrefix, cha tmpPath[len++] = '/'; } - (void)strcpy(tmpPath + len, TD_TMP_FILE_PREFIX); - if (strlen(tmpPath) + strlen(fileNamePrefix) + strlen("-%d-%s") < PATH_MAX) { - (void)strcat(tmpPath, fileNamePrefix); - (void)strcat(tmpPath, "-%d-%s"); - } + snprintf(tmpPath + len, sizeof(tmpPath) - len, "%s%s%s", TD_TMP_FILE_PREFIX, fileNamePrefix, "-%d-%s"); char rand[32] = {0}; - (void)sprintf(rand, "%" PRIu64, atomic_add_fetch_64(&seqId, 1)); + (void)snprintf(rand, sizeof(rand), "%" PRIu64, atomic_add_fetch_64(&seqId, 1)); (void)snprintf(dstPath, PATH_MAX, tmpPath, getpid(), rand); diff --git a/source/os/src/osLocale.c b/source/os/src/osLocale.c index d7fbb05a5d..becf0d5a70 100644 --- a/source/os/src/osLocale.c +++ b/source/os/src/osLocale.c @@ -95,7 +95,7 @@ void taosGetSystemLocale(char *outLocale, char *outCharset) { if (locale != NULL) { tstrncpy(outLocale, locale, TD_LOCALE_LEN); } - strcpy(outCharset, "UTF-8"); + tstrncpy(outCharset, "UTF-8", TD_CHARSET_LEN); #elif defined(_TD_DARWIN_64) /* @@ -123,7 +123,7 @@ void taosGetSystemLocale(char *outLocale, char *outCharset) { locale = setlocale(LC_CTYPE, ""); if (locale == NULL) { // printf("can't get locale from system, set it to en_US.UTF-8 since error:%d:%s", errno, strerror(errno)); - strcpy(outLocale, "en_US.UTF-8"); + tstrncpy(outLocale, "en_US.UTF-8", TD_LOCALE_LEN); } else { tstrncpy(outLocale, locale, TD_LOCALE_LEN); // printf("locale not configured, set to system default:%s", outLocale); @@ -137,7 +137,7 @@ void taosGetSystemLocale(char *outLocale, char *outCharset) { char *revisedCharset = taosCharsetReplace(str); if (NULL == revisedCharset) { - (void)strcpy(outCharset, "UTF-8"); + tstrncpy(outCharset, "UTF-8", TD_CHARSET_LEN); } else { tstrncpy(outCharset, revisedCharset, TD_CHARSET_LEN); @@ -145,7 +145,7 @@ void taosGetSystemLocale(char *outLocale, char *outCharset) { } // printf("charset not configured, set to system default:%s", outCharset); } else { - strcpy(outCharset, "UTF-8"); + tstrncpy(outCharset, "UTF-8", TD_CHARSET_LEN); // printf("can't get locale and charset from system, set it to UTF-8"); } @@ -173,7 +173,7 @@ void taosGetSystemLocale(char *outLocale, char *outCharset) { locale = setlocale(LC_CTYPE, ""); if (locale == NULL) { // printf("can't get locale from system, set it to en_US.UTF-8 since error:%d:%s", errno, strerror(errno)); - (void)strcpy(outLocale, "en_US.UTF-8"); + tstrncpy(outLocale, "en_US.UTF-8", TD_LOCALE_LEN); } else { tstrncpy(outLocale, locale, TD_LOCALE_LEN); //printf("locale not configured, set to system default:%s\n", outLocale); @@ -186,15 +186,15 @@ void taosGetSystemLocale(char *outLocale, char *outCharset) { char *revisedCharset = taosCharsetReplace(str); if (NULL == revisedCharset) { - (void)strcpy(outCharset, "UTF-8"); + tstrncpy(outCharset, "UTF-8", TD_CHARSET_LEN); } else { - tstrncpy(outCharset, revisedCharset, TD_LOCALE_LEN); + tstrncpy(outCharset, revisedCharset, TD_CHARSET_LEN); taosMemoryFree(revisedCharset); } // printf("charset not configured, set to system default:%s", outCharset); } else { - (void)strcpy(outCharset, "UTF-8"); + tstrncpy(outCharset, "UTF-8", TD_CHARSET_LEN); // printf("can't get locale and charset from system, set it to UTF-8"); } diff --git a/source/os/src/osSocket.c b/source/os/src/osSocket.c index 09a5579e13..d17cc9b239 100644 --- a/source/os/src/osSocket.c +++ b/source/os/src/osSocket.c @@ -332,8 +332,8 @@ int32_t taosGetFqdn(char *fqdn) { // thus, we choose AF_INET (ipv4 for the moment) to make getaddrinfo return // immediately // hints.ai_family = AF_INET; - strcpy(fqdn, hostname); - strcpy(fqdn + strlen(hostname), ".local"); + tstrncpy(fqdn, hostname, TD_FQDN_LEN); + tstrncpy(fqdn + strlen(hostname), ".local", TD_FQDN_LEN - strlen(hostname)); #else // linux #endif // linux @@ -361,7 +361,7 @@ int32_t taosGetFqdn(char *fqdn) { break; } - (void)strcpy(fqdn, result->ai_canonname); + tstrncpy(fqdn, result->ai_canonname, TD_FQDN_LEN); freeaddrinfo(result); @@ -375,7 +375,7 @@ int32_t taosGetFqdn(char *fqdn) { // fprintf(stderr, "failed to get fqdn, code:%d, hostname:%s, reason:%s\n", ret, hostname, gai_strerror(ret)); return TAOS_SYSTEM_WINSOCKET_ERROR(WSAGetLastError()); } - strcpy(fqdn, result->ai_canonname); + tstrncpy(fqdn, result->ai_canonname, TD_FQDN_LEN); freeaddrinfo(result); #endif @@ -384,7 +384,7 @@ int32_t taosGetFqdn(char *fqdn) { } void tinet_ntoa(char *ipstr, uint32_t ip) { - (void)sprintf(ipstr, "%d.%d.%d.%d", ip & 0xFF, (ip >> 8) & 0xFF, (ip >> 16) & 0xFF, ip >> 24); + (void)snprintf(ipstr, TD_IP_LEN, "%d.%d.%d.%d", ip & 0xFF, (ip >> 8) & 0xFF, (ip >> 16) & 0xFF, ip >> 24); } int32_t taosIgnSIGPIPE() { diff --git a/source/os/src/osString.c b/source/os/src/osString.c index d265ed510a..15f12e9db8 100644 --- a/source/os/src/osString.c +++ b/source/os/src/osString.c @@ -71,7 +71,7 @@ char *taosStrndup(const char *s, int size) { if (l > size) l = size; s2 = malloc(l + 1); if (s2) { - strncpy(s2, s, l); + tstrncpy(s2, s, l + 1); s2[l] = '\0'; } else { terrno = TSDB_CODE_OUT_OF_MEMORY; @@ -442,14 +442,14 @@ int32_t taosUcs4len(TdUcs4 *ucs4) { } // dst buffer size should be at least 2*len + 1 -int32_t taosHexEncode(const unsigned char *src, char *dst, int32_t len) { +int32_t taosHexEncode(const unsigned char *src, char *dst, int32_t len, int32_t bufSize) { if (!dst) { terrno = TSDB_CODE_INVALID_PARA; return terrno; } for (int32_t i = 0; i < len; ++i) { - (void)sprintf(dst + i * 2, "%02x", src[i]); + (void)snprintf(dst + i * 2, bufSize - i * 2, "%02x", src[i]); } return 0; diff --git a/source/os/src/osSysinfo.c b/source/os/src/osSysinfo.c index cb6d3a7736..67be264435 100644 --- a/source/os/src/osSysinfo.c +++ b/source/os/src/osSysinfo.c @@ -389,10 +389,10 @@ int32_t taosGetOsReleaseName(char *releaseName, char* sName, char* ver, int32_t } if (major >= 20) { major -= 9; // macOS 11 and newer - sprintf(releaseName, "macOS %u.%u", major, minor); + snprintf(releaseName, maxLen, "macOS %u.%u", major, minor); } else { major -= 4; // macOS 10.1.1 and newer - sprintf(releaseName, "macOS 10.%d.%d", major, minor); + snprintf(releaseName, maxLen, "macOS 10.%d.%d", major, minor); } return 0; @@ -474,7 +474,7 @@ int32_t taosGetCpuInfo(char *cpuModel, int32_t maxLen, float *numOfCores) { if (taosGetsCmd(pCmd, sizeof(buf) - 1, buf) > 0) { code = 0; done |= 2; - *numOfCores = atof(buf); + *numOfCores = taosStr2Float(buf, NULL); } taosCloseCmd(&pCmd); @@ -498,7 +498,7 @@ int32_t taosGetCpuInfo(char *cpuModel, int32_t maxLen, float *numOfCores) { done |= 1; } else if (((done & 2) == 0) && strncmp(line, "cpu cores", 9) == 0) { const char *v = strchr(line, ':') + 2; - *numOfCores = atof(v); + *numOfCores = taosStr2Float(v, NULL); done |= 2; } if (strncmp(line, "processor", 9) == 0) coreCount += 1; @@ -1095,7 +1095,7 @@ char *taosGetCmdlineByPID(int pid) { return cmdline; #else static char cmdline[1024]; - (void)sprintf(cmdline, "/proc/%d/cmdline", pid); + (void)snprintf(cmdline, sizeof(cmdline), "/proc/%d/cmdline", pid); // int fd = open(cmdline, O_RDONLY); TdFilePtr pFile = taosOpenFile(cmdline, TD_FILE_READ); diff --git a/source/os/src/osTime.c b/source/os/src/osTime.c index c650ac989d..d4d9936154 100644 --- a/source/os/src/osTime.c +++ b/source/os/src/osTime.c @@ -474,22 +474,15 @@ time_t taosMktime(struct tm *timep) { #endif } -struct tm *taosLocalTime(const time_t *timep, struct tm *result, char *buf) { +struct tm *taosLocalTime(const time_t *timep, struct tm *result, char *buf, int32_t bufSize) { struct tm *res = NULL; - if (timep == NULL) { + if (timep == NULL || result == NULL) { return NULL; } - if (result == NULL) { - res = localtime(timep); - if (res == NULL && buf != NULL) { - (void)sprintf(buf, "NaN"); - } - return res; - } #ifdef WINDOWS if (*timep < -2208988800LL) { if (buf != NULL) { - sprintf(buf, "NaN"); + snprintf(buf, bufSize, "NaN"); } return NULL; } else if (*timep < 0) { @@ -501,7 +494,7 @@ struct tm *taosLocalTime(const time_t *timep, struct tm *result, char *buf) { time_t tt = 0; if (localtime_s(&tm1, &tt) != 0) { if (buf != NULL) { - sprintf(buf, "NaN"); + snprintf(buf, bufSize, "NaN"); } return NULL; } @@ -532,7 +525,7 @@ struct tm *taosLocalTime(const time_t *timep, struct tm *result, char *buf) { } else { if (localtime_s(result, timep) != 0) { if (buf != NULL) { - sprintf(buf, "NaN"); + snprintf(buf, bufSize, "NaN"); } return NULL; } @@ -540,7 +533,7 @@ struct tm *taosLocalTime(const time_t *timep, struct tm *result, char *buf) { #else res = localtime_r(timep, result); if (res == NULL && buf != NULL) { - (void)sprintf(buf, "NaN"); + (void)snprintf(buf, bufSize, "NaN"); } #endif return result; @@ -559,7 +552,7 @@ static int isLeapYear(time_t year) { struct tm *taosLocalTimeNolock(struct tm *result, const time_t *timep, int dst) { if (result == NULL) { - return localtime(timep); + return NULL; } #ifdef WINDOWS if (*timep < 0) { diff --git a/source/os/src/osTimezone.c b/source/os/src/osTimezone.c index 6e7c22c7f1..5eded97cde 100644 --- a/source/os/src/osTimezone.c +++ b/source/os/src/osTimezone.c @@ -783,15 +783,15 @@ int32_t taosSetSystemTimezone(const char *inTimezoneStr, char *outTimezoneStr, i memset(winStr, 0, sizeof(winStr)); for (size_t i = 0; i < 554; i++) { if (strcmp(tz_win[i][0], buf) == 0) { - char keyPath[100]; + char keyPath[256]; char keyValue[100]; DWORD keyValueSize = sizeof(keyValue); - sprintf(keyPath, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones\\%s", tz_win[i][1]); + snprintf(keyPath, sizeof(keyPath), "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones\\%s", tz_win[i][1]); RegGetValue(HKEY_LOCAL_MACHINE, keyPath, "Display", RRF_RT_ANY, NULL, (PVOID)&keyValue, &keyValueSize); if (keyValueSize > 0) { keyValue[4] = (keyValue[4] == '+' ? '-' : '+'); keyValue[10] = 0; - sprintf(winStr, "TZ=%s:00", &(keyValue[1])); + snprintf(winStr, sizeof(winStr), "TZ=%s:00", &(keyValue[1])); *tsTimezone = -taosStr2Int32(&keyValue[4], NULL, 10); } break; @@ -805,7 +805,7 @@ int32_t taosSetSystemTimezone(const char *inTimezoneStr, char *outTimezoneStr, i char *ppp = strchr(inTimezoneStr, ','); int indexStr; if (pp == NULL || ppp == NULL) { - indexStr = sprintf(winStr, "TZ=UTC"); + indexStr = snprintf(winStr, sizeof(winStr), "TZ=UTC"); } else { memcpy(winStr, "TZ=", 3); pp++; @@ -814,7 +814,7 @@ int32_t taosSetSystemTimezone(const char *inTimezoneStr, char *outTimezoneStr, i } char to[5]; parseTimeStr(p, to); - sprintf(&winStr[indexStr], "%c%c%c:%c%c:00", (to[0] == '+' ? '+' : '-'), to[1], to[2], to[3], to[4]); + snprintf(&winStr[indexStr], sizeof(winStr) - indexStr, "%c%c%c:%c%c:00", (to[0] == '+' ? '+' : '-'), to[1], to[2], to[3], to[4]); *tsTimezone = -taosStr2Int32(p, NULL, 10); } else { *tsTimezone = 0; @@ -823,7 +823,7 @@ int32_t taosSetSystemTimezone(const char *inTimezoneStr, char *outTimezoneStr, i _putenv(winStr); _tzset(); if (outTimezoneStr != inTimezoneStr) { - strcpy(outTimezoneStr, inTimezoneStr); + tstrncpy(outTimezoneStr, inTimezoneStr, TD_TIMEZONE_LEN); } *outDaylight = 0; @@ -839,7 +839,7 @@ int32_t taosSetSystemTimezone(const char *inTimezoneStr, char *outTimezoneStr, i *tsTimezone = tz; tz += isdst_now; - sprintf(outTimezoneStr, "%s (%s, %s%02d00)", buf, tzname[isdst_now], tz >= 0 ? "+" : "-", abs(tz)); + snprintf(outTimezoneStr, TD_TIMEZONE_LEN, "%s (%s, %s%02d00)", buf, tzname[isdst_now], tz >= 0 ? "+" : "-", abs(tz)); *outDaylight = isdst_now; #else @@ -853,7 +853,7 @@ int32_t taosSetSystemTimezone(const char *inTimezoneStr, char *outTimezoneStr, i int32_t tz = (int32_t)((-timezone * MILLISECOND_PER_SECOND) / MILLISECOND_PER_HOUR); *tsTimezone = tz; tz += isdst_now; - (void)sprintf(outTimezoneStr, "%s (%s, %s%02d00)", buf, tzname[isdst_now], tz >= 0 ? "+" : "-", abs(tz)); + (void)snprintf(outTimezoneStr, TD_TIMEZONE_LEN, "%s (%s, %s%02d00)", buf, tzname[isdst_now], tz >= 0 ? "+" : "-", abs(tz)); *outDaylight = isdst_now; #endif @@ -872,21 +872,21 @@ int32_t taosGetSystemTimezone(char *outTimezoneStr, enum TdTimezone *tsTimezone) if (result != ERROR_SUCCESS) { return TAOS_SYSTEM_WINAPI_ERROR(result); } - strcpy(outTimezoneStr, "not configured"); + tstrncpy(outTimezoneStr, "not configured", TD_TIMEZONE_LEN); *tsTimezone = 0; if (bufferSize > 0) { for (size_t i = 0; i < 139; i++) { if (strcmp(win_tz[i][0], value) == 0) { - strcpy(outTimezoneStr, win_tz[i][1]); + tstrncpy(outTimezoneStr, win_tz[i][1], TD_TIMEZONE_LEN); bufferSize = sizeof(value); - sprintf(keyPath, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones\\%s", value); + snprintf(keyPath, sizeof(keyPath), "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones\\%s", value); result = RegGetValue(HKEY_LOCAL_MACHINE, keyPath, "Display", RRF_RT_ANY, NULL, (PVOID)&value, &bufferSize); if (result != ERROR_SUCCESS) { return TAOS_SYSTEM_WINAPI_ERROR(result); } if (bufferSize > 0) { // value[4] = (value[4] == '+' ? '-' : '+'); - sprintf(outTimezoneStr, "%s (UTC, %c%c%c%c%c)", outTimezoneStr, value[4], value[5], value[6], value[8], + snprintf(outTimezoneStr, TD_TIMEZONE_LEN, "%s (UTC, %c%c%c%c%c)", outTimezoneStr, value[4], value[5], value[6], value[8], value[9]); *tsTimezone = taosStr2Int32(&value[4], NULL, 10); } @@ -926,7 +926,7 @@ int32_t taosGetSystemTimezone(char *outTimezoneStr, enum TdTimezone *tsTimezone) */ time_t tx1 = taosGetTimestampSec(); struct tm tm1; - if (taosLocalTime(&tx1, &tm1, NULL) == NULL) { + if (taosLocalTime(&tx1, &tm1, NULL, 0) == NULL) { return TSDB_CODE_TIME_ERROR; } daylight = tm1.tm_isdst; @@ -956,7 +956,7 @@ int32_t taosGetSystemTimezone(char *outTimezoneStr, enum TdTimezone *tsTimezone) */ time_t tx1 = taosGetTimestampSec(); struct tm tm1; - if(taosLocalTime(&tx1, &tm1, NULL) == NULL) { + if(taosLocalTime(&tx1, &tm1, NULL, 0) == NULL) { return TSDB_CODE_TIME_ERROR; } /* load time zone string from /etc/timezone */ @@ -1037,7 +1037,7 @@ int32_t taosGetSystemTimezone(char *outTimezoneStr, enum TdTimezone *tsTimezone) */ time_t tx1 = taosGetTimestampSec(); struct tm tm1; - if(taosLocalTime(&tx1, &tm1, NULL) == NULL) { + if(taosLocalTime(&tx1, &tm1, NULL, 0) == NULL) { return TSDB_CODE_TIME_ERROR; } isdst_now = tm1.tm_isdst; diff --git a/source/os/test/osTests.cpp b/source/os/test/osTests.cpp index 9786886c56..2c20348608 100644 --- a/source/os/test/osTests.cpp +++ b/source/os/test/osTests.cpp @@ -37,7 +37,7 @@ #include TEST(osTest, osFQDNSuccess) { - char fqdn[1024]; + char fqdn[TD_FQDN_LEN]; char ipString[INET_ADDRSTRLEN]; int code = taosGetFqdn(fqdn); uint32_t ipv4 = 0; diff --git a/source/os/test/osTimeTests.cpp b/source/os/test/osTimeTests.cpp index 90c089e310..42837cc537 100644 --- a/source/os/test/osTimeTests.cpp +++ b/source/os/test/osTimeTests.cpp @@ -31,20 +31,20 @@ TEST(osTimeTests, taosLocalTimeNolock) { time_t currentTime; - // Test when result is NULL - struct tm* result = taosLocalTimeNolock(NULL, ¤tTime, 0); // Test when result is not NULL struct tm expectedTime; - result = taosLocalTimeNolock(&expectedTime, ¤tTime, 1); - EXPECT_EQ(expectedTime.tm_year, result->tm_year); - EXPECT_EQ(expectedTime.tm_mon, result->tm_mon); - EXPECT_EQ(expectedTime.tm_mday, result->tm_mday); - EXPECT_EQ(expectedTime.tm_hour, result->tm_hour); - EXPECT_EQ(expectedTime.tm_min, result->tm_min); - EXPECT_EQ(expectedTime.tm_sec, result->tm_sec); - EXPECT_EQ(expectedTime.tm_wday, result->tm_wday); - EXPECT_EQ(expectedTime.tm_yday, result->tm_yday); - EXPECT_EQ(expectedTime.tm_isdst, result->tm_isdst); + struct tm* result = taosLocalTimeNolock(&expectedTime, ¤tTime, 1); + if (result) { + EXPECT_EQ(expectedTime.tm_year, result->tm_year); + EXPECT_EQ(expectedTime.tm_mon, result->tm_mon); + EXPECT_EQ(expectedTime.tm_mday, result->tm_mday); + EXPECT_EQ(expectedTime.tm_hour, result->tm_hour); + EXPECT_EQ(expectedTime.tm_min, result->tm_min); + EXPECT_EQ(expectedTime.tm_sec, result->tm_sec); + EXPECT_EQ(expectedTime.tm_wday, result->tm_wday); + EXPECT_EQ(expectedTime.tm_yday, result->tm_yday); + EXPECT_EQ(expectedTime.tm_isdst, result->tm_isdst); + } } @@ -52,7 +52,7 @@ TEST(osTimeTests, taosLocalTime) { // Test 1: Test when both timep and result are not NULL time_t timep = 1617531000; // 2021-04-04 18:10:00 struct tm result; - struct tm* local_time = taosLocalTime(&timep, &result, NULL); + struct tm* local_time = taosLocalTime(&timep, &result, NULL, 0); ASSERT_NE(local_time, nullptr); ASSERT_EQ(local_time->tm_year, 121); ASSERT_EQ(local_time->tm_mon, 3); @@ -62,20 +62,13 @@ TEST(osTimeTests, taosLocalTime) { ASSERT_EQ(local_time->tm_sec, 00); // Test 2: Test when timep is NULL - local_time = taosLocalTime(NULL, &result, NULL); + local_time = taosLocalTime(NULL, &result, NULL, 0); ASSERT_EQ(local_time, nullptr); - // Test 3: Test when result is NULL - local_time = taosLocalTime(&timep, NULL, NULL); - ASSERT_NE(local_time, nullptr); - ASSERT_EQ(local_time->tm_year, 121); - ASSERT_EQ(local_time->tm_mon, 3); - ASSERT_EQ(local_time->tm_mday, 4); - // Test 4: Test when timep is negative on Windows #ifdef WINDOWS time_t pos_timep = 1609459200; // 2021-01-01 08:00:00 - local_time = taosLocalTime(&pos_timep, &result, NULL); + local_time = taosLocalTime(&pos_timep, &result, NULL, 0); ASSERT_NE(local_time, nullptr); ASSERT_EQ(local_time->tm_year, 121); ASSERT_EQ(local_time->tm_mon, 0); @@ -85,7 +78,7 @@ TEST(osTimeTests, taosLocalTime) { ASSERT_EQ(local_time->tm_sec, 0); time_t neg_timep = -1617531000; // 1918-09-29 21:50:00 - local_time = taosLocalTime(&neg_timep, &result, NULL); + local_time = taosLocalTime(&neg_timep, &result, NULL, 0); ASSERT_NE(local_time, nullptr); ASSERT_EQ(local_time->tm_year, 18); ASSERT_EQ(local_time->tm_mon, 8); @@ -95,7 +88,7 @@ TEST(osTimeTests, taosLocalTime) { ASSERT_EQ(local_time->tm_sec, 0); time_t neg_timep2 = -315619200; // 1960-01-01 08:00:00 - local_time = taosLocalTime(&neg_timep2, &result, NULL); + local_time = taosLocalTime(&neg_timep2, &result, NULL, 0); ASSERT_NE(local_time, nullptr); ASSERT_EQ(local_time->tm_year, 60); ASSERT_EQ(local_time->tm_mon, 0); @@ -105,7 +98,7 @@ TEST(osTimeTests, taosLocalTime) { ASSERT_EQ(local_time->tm_sec, 0); time_t zero_timep = 0; // 1970-01-01 08:00:00 - local_time = taosLocalTime(&zero_timep, &result, NULL); + local_time = taosLocalTime(&zero_timep, &result, NULL, 0); ASSERT_NE(local_time, nullptr); ASSERT_EQ(local_time->tm_year, 70); ASSERT_EQ(local_time->tm_mon, 0); @@ -115,7 +108,7 @@ TEST(osTimeTests, taosLocalTime) { ASSERT_EQ(local_time->tm_sec, 0); time_t neg_timep3 = -78115158887; - local_time = taosLocalTime(&neg_timep3, &result, NULL); + local_time = taosLocalTime(&neg_timep3, &result, NULL, 0); ASSERT_EQ(local_time, nullptr); #endif } \ No newline at end of file diff --git a/source/util/src/tlog.c b/source/util/src/tlog.c index 2a7ca2c7dd..45c8a2f6c2 100644 --- a/source/util/src/tlog.c +++ b/source/util/src/tlog.c @@ -151,18 +151,18 @@ static int32_t taosStartLog() { return 0; } -static void getDay(char* buf){ +static void getDay(char* buf, int32_t bufSize){ time_t t = taosTime(NULL); struct tm tmInfo; - if (taosLocalTime(&t, &tmInfo, buf) != NULL) { - TAOS_UNUSED(strftime(buf, LOG_FILE_DAY_LEN, "%Y-%m-%d", &tmInfo)); + if (taosLocalTime(&t, &tmInfo, buf, bufSize) != NULL) { + TAOS_UNUSED(strftime(buf, bufSize, "%Y-%m-%d", &tmInfo)); } } static int64_t getTimestampToday() { time_t t = taosTime(NULL); struct tm tm; - if (taosLocalTime(&t, &tm, NULL) == NULL) { + if (taosLocalTime(&t, &tm, NULL, 0) == NULL) { return 0; } tm.tm_hour = 0; @@ -198,10 +198,10 @@ int32_t taosInitSlowLog() { getFullPathName(tsLogObj.slowLogName, logFileName); - char name[PATH_MAX + LOG_FILE_DAY_LEN] = {0}; - char day[LOG_FILE_DAY_LEN] = {0}; - getDay(day); - (void)snprintf(name, PATH_MAX + LOG_FILE_DAY_LEN, "%s.%s", tsLogObj.slowLogName, day); + char name[PATH_MAX + TD_TIME_STR_LEN] = {0}; + char day[TD_TIME_STR_LEN] = {0}; + getDay(day, sizeof(day)); + (void)snprintf(name, PATH_MAX + TD_TIME_STR_LEN, "%s.%s", tsLogObj.slowLogName, day); tsLogObj.timestampToday = getTimestampToday(); tsLogObj.slowHandle = taosLogBuffNew(LOG_SLOW_BUF_SIZE); @@ -430,11 +430,11 @@ static void taosOpenNewSlowLogFile() { taosWriteLog(tsLogObj.slowHandle); atomic_store_32(&tsLogObj.slowHandle->lock, 0); - char day[LOG_FILE_DAY_LEN] = {0}; - getDay(day); + char day[TD_TIME_STR_LEN] = {0}; + getDay(day, sizeof(day)); TdFilePtr pFile = NULL; - char name[PATH_MAX + LOG_FILE_DAY_LEN] = {0}; - (void)snprintf(name, PATH_MAX + LOG_FILE_DAY_LEN, "%s.%s", tsLogObj.slowLogName, day); + char name[PATH_MAX + TD_TIME_STR_LEN] = {0}; + (void)snprintf(name, PATH_MAX + TD_TIME_STR_LEN, "%s.%s", tsLogObj.slowLogName, day); pFile = taosOpenFile(name, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_APPEND); if (pFile == NULL) { uError("open new log file fail! reason:%s, reuse lastlog", strerror(errno)); @@ -631,7 +631,7 @@ static inline int32_t taosBuildLogHead(char *buffer, const char *flags) { TAOS_UNUSED(taosGetTimeOfDay(&timeSecs)); time_t curTime = timeSecs.tv_sec; - ptm = taosLocalTime(&curTime, &Tm, NULL); + ptm = taosLocalTime(&curTime, &Tm, NULL, 0); return sprintf(buffer, "%02d/%02d %02d:%02d:%02d.%06d %08" PRId64 " %s %s", ptm->tm_mon + 1, ptm->tm_mday, ptm->tm_hour, ptm->tm_min, ptm->tm_sec, (int32_t)timeSecs.tv_usec, taosGetSelfPthreadId(), diff --git a/tools/shell/src/shellEngine.c b/tools/shell/src/shellEngine.c index 84da746afd..5a3d06306c 100644 --- a/tools/shell/src/shellEngine.c +++ b/tools/shell/src/shellEngine.c @@ -44,7 +44,7 @@ static int32_t shellRunSingleCommand(char *command); static void shellRecordCommandToHistory(char *command); static int32_t shellRunCommand(char *command, bool recordHistory); static void shellRunSingleCommandImp(char *command); -static char *shellFormatTimestamp(char *buf, int64_t val, int32_t precision); +static char *shellFormatTimestamp(char *buf, int32_t bufSize, int64_t val, int32_t precision); static int64_t shellDumpResultToFile(const char *fname, TAOS_RES *tres); static void shellPrintNChar(const char *str, int32_t length, int32_t width); static void shellPrintGeometry(const unsigned char *str, int32_t length, int32_t width); @@ -304,7 +304,7 @@ void shellRunSingleCommandImp(char *command) { printf("\r\n"); } -char *shellFormatTimestamp(char *buf, int64_t val, int32_t precision) { +char *shellFormatTimestamp(char *buf, int32_t bufSize, int64_t val, int32_t precision) { if (shell.args.is_raw_time) { sprintf(buf, "%" PRId64, val); return buf; @@ -335,7 +335,7 @@ char *shellFormatTimestamp(char *buf, int64_t val, int32_t precision) { } struct tm ptm = {0}; - if (taosLocalTime(&tt, &ptm, buf) == NULL) { + if (taosLocalTime(&tt, &ptm, buf, bufSize) == NULL) { return buf; } size_t pos = strftime(buf, 35, "%Y-%m-%d %H:%M:%S", &ptm); @@ -465,7 +465,7 @@ void shellDumpFieldToFile(TdFilePtr pFile, const char *val, TAOS_FIELD *field, i break; } case TSDB_DATA_TYPE_TIMESTAMP: - shellFormatTimestamp(buf, *(int64_t *)val, precision); + shellFormatTimestamp(buf, sizeof(buf), *(int64_t *)val, precision); taosFprintfFile(pFile, "%s%s%s", quotationStr, buf, quotationStr); break; default: @@ -710,7 +710,7 @@ void shellPrintField(const char *val, TAOS_FIELD *field, int32_t width, int32_t shellPrintGeometry(val, length, width); break; case TSDB_DATA_TYPE_TIMESTAMP: - shellFormatTimestamp(buf, *(int64_t *)val, precision); + shellFormatTimestamp(buf, sizeof(buf), *(int64_t *)val, precision); printf("%s", buf); break; default: diff --git a/utils/test/c/tmqDemo.c b/utils/test/c/tmqDemo.c index 64f536433e..40ed98132c 100644 --- a/utils/test/c/tmqDemo.c +++ b/utils/test/c/tmqDemo.c @@ -597,7 +597,7 @@ void printParaIntoFile() { time_t tTime = taosGetTimestampSec(); struct tm tm; - taosLocalTime(&tTime, &tm, NULL); + taosLocalTime(&tTime, &tm, NULL, 0); taosFprintfFile(pFile, "###################################################################\n"); taosFprintfFile(pFile, "# configDir: %s\n", configDir); diff --git a/utils/test/c/tmqSim.c b/utils/test/c/tmqSim.c index 006bd516d0..e2a09c4259 100644 --- a/utils/test/c/tmqSim.c +++ b/utils/test/c/tmqSim.c @@ -166,7 +166,7 @@ static void printHelp() { char* getCurrentTimeString(char* timeString) { time_t tTime = taosGetTimestampSec(); struct tm tm; - taosLocalTime(&tTime, &tm, NULL); + taosLocalTime(&tTime, &tm, NULL, 0); sprintf(timeString, "%d-%02d-%02d %02d:%02d:%02d", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec); @@ -441,7 +441,7 @@ int32_t saveConsumeContentToTbl(SThreadInfo* pInfo, char* buf) { return 0; } -static char* shellFormatTimestamp(char* buf, int64_t val, int32_t precision) { +static char* shellFormatTimestamp(char* buf, int32_t bufSize, int64_t val, int32_t precision) { // if (shell.args.is_raw_time) { // sprintf(buf, "%" PRId64, val); // return buf; @@ -472,7 +472,7 @@ static char* shellFormatTimestamp(char* buf, int64_t val, int32_t precision) { } struct tm ptm; - if (taosLocalTime(&tt, &ptm, buf) == NULL) { + if (taosLocalTime(&tt, &ptm, buf, bufSize) == NULL) { return buf; } size_t pos = strftime(buf, 35, "%Y-%m-%d %H:%M:%S", &ptm); @@ -559,7 +559,7 @@ static void shellDumpFieldToFile(TdFilePtr pFile, const char* val, TAOS_FIELD* f taosFprintfFile(pFile, "%s%s%s", quotationStr, buf, quotationStr); } break; case TSDB_DATA_TYPE_TIMESTAMP: - shellFormatTimestamp(buf, *(int64_t*)val, precision); + shellFormatTimestamp(buf, sizeof(buf), *(int64_t*)val, precision); taosFprintfFile(pFile, "%s%s%s", quotationStr, buf, quotationStr); break; default: diff --git a/utils/tsim/src/simExe.c b/utils/tsim/src/simExe.c index bea839057e..c7cec64dec 100644 --- a/utils/tsim/src/simExe.c +++ b/utils/tsim/src/simExe.c @@ -797,7 +797,7 @@ bool simExecuteNativeSqlCommand(SScript *script, char *rest, bool isSlow) { tt = (*(int64_t *)row[i]) / 1000000000; } - if (taosLocalTime(&tt, &tp, timeStr) == NULL) { + if (taosLocalTime(&tt, &tp, timeStr, sizeof(timeStr)) == NULL) { break; } strftime(timeStr, 64, "%y-%m-%d %H:%M:%S", &tp);