fix: use safe function

This commit is contained in:
dapan1121 2024-10-08 19:18:48 +08:00
parent fb5819c622
commit 0055d9544c
25 changed files with 91 additions and 111 deletions

View File

@ -247,9 +247,11 @@ 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_IP_LEN 64
#ifdef __cplusplus
}

View File

@ -86,7 +86,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);

View File

@ -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

View File

@ -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));

View File

@ -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);

View File

@ -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];

View File

@ -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;
}

View File

@ -79,13 +79,14 @@ static void logGroupCacheExecInfo(SGroupCacheOperatorInfo* pGrpCacheOperator) {
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

View File

@ -3953,9 +3953,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;
@ -3971,9 +3972,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;

View File

@ -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;
}

View File

@ -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;

View File

@ -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;

View File

@ -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) {

View File

@ -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);

View File

@ -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)) {
@ -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) {

View File

@ -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);
tstrncpy(tsDataDir, TD_DATA_DIR_PATH, sizeof(tsDataDir));
tstrncpy(tsLogDir, TD_LOG_DIR_PATH, sizeof(tsLogDir));
if (strlen(tsTempDir) == 0){
(void)strcpy(tsTempDir, TD_TMP_DIR_PATH);
tstrncpy(tsTempDir, TD_TMP_DIR_PATH, sizeof(tsTempDir));
}
return code;

View File

@ -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);

View File

@ -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");
(void)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");
}

View File

@ -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, TSDB_FQDN_LEN);
tstrncpy(fqdn + strlen(hostname), ".local", TSDB_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() {

View File

@ -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;

View File

@ -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);

View File

@ -479,17 +479,10 @@ struct tm *taosLocalTime(const time_t *timep, struct tm *result, char *buf) {
if (timep == 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, 4, "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, 4, "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, 4, "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, 4, "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) {

View File

@ -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);
}

View File

@ -37,7 +37,7 @@
#include <arpa/inet.h>
TEST(osTest, osFQDNSuccess) {
char fqdn[1024];
char fqdn[TSDB_FQDN_LEN];
char ipString[INET_ADDRSTRLEN];
int code = taosGetFqdn(fqdn);
uint32_t ipv4 = 0;

View File

@ -31,8 +31,6 @@
TEST(osTimeTests, taosLocalTimeNolock) {
time_t currentTime;
// Test when result is NULL
struct tm* result = taosLocalTimeNolock(NULL, &currentTime, 0);
// Test when result is not NULL
struct tm expectedTime;
result = taosLocalTimeNolock(&expectedTime, &currentTime, 1);
@ -65,13 +63,6 @@ TEST(osTimeTests, taosLocalTime) {
local_time = taosLocalTime(NULL, &result, NULL);
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