diff --git a/src/client/src/tscParseInsert.c b/src/client/src/tscParseInsert.c index cbc83c6e75ed5d8c71ec5fe85550a32df19e0651..c4749e461177e037db04f0c85ddede0010c6bdb6 100644 --- a/src/client/src/tscParseInsert.c +++ b/src/client/src/tscParseInsert.c @@ -795,7 +795,7 @@ static int32_t tscCheckIfCreateTable(char **sqlstr, SSqlObj *pSql) { STableMetaInfo *pSTableMeterMetaInfo = tscGetMetaInfo(pQueryInfo, STABLE_INDEX); tscSetTableFullName(pSTableMeterMetaInfo, &sToken, pSql); - strncpy(pTag->name, pSTableMeterMetaInfo->name, TSDB_TABLE_ID_LEN); + tstrncpy(pTag->name, pSTableMeterMetaInfo->name, sizeof(pTag->name)); code = tscGetTableMeta(pSql, pSTableMeterMetaInfo); if (code != TSDB_CODE_SUCCESS) { return code; @@ -989,7 +989,7 @@ static int32_t tscCheckIfCreateTable(char **sqlstr, SSqlObj *pSql) { int validateTableName(char *tblName, int len) { char buf[TSDB_TABLE_ID_LEN] = {0}; - strncpy(buf, tblName, len); + tstrncpy(buf, tblName, sizeof(buf)); SSQLToken token = {.n = len, .type = TK_ID, .z = buf}; tSQLGetToken(buf, &token.type); @@ -1512,7 +1512,7 @@ void tscProcessMultiVnodesInsertFromFile(SSqlObj *pSql) { } pCmd->count = 1; - strncpy(path, pDataBlock->filename, PATH_MAX); + tstrncpy(path, pDataBlock->filename, sizeof(path)); FILE *fp = fopen(path, "r"); if (fp == NULL) { @@ -1520,7 +1520,7 @@ void tscProcessMultiVnodesInsertFromFile(SSqlObj *pSql) { continue; } - strncpy(pTableMetaInfo->name, pDataBlock->tableId, TSDB_TABLE_ID_LEN); + tstrncpy(pTableMetaInfo->name, pDataBlock->tableId, sizeof(pTableMetaInfo->name)); memset(pDataBlock->pData, 0, pDataBlock->nAllocSize); int32_t ret = tscGetTableMeta(pSql, pTableMetaInfo); diff --git a/src/client/src/tscProfile.c b/src/client/src/tscProfile.c index 79d00bf5dc6e9075af64c12de776c8022841e180..55243c43827763c533e9318b767e6c59f1504a1b 100644 --- a/src/client/src/tscProfile.c +++ b/src/client/src/tscProfile.c @@ -239,8 +239,7 @@ int tscBuildQueryStreamDesc(void *pMsg, STscObj *pObj) { continue; } - strncpy(pQdesc->sql, pSql->sqlstr, TSDB_SHOW_SQL_LEN - 1); - pQdesc->sql[TSDB_SHOW_SQL_LEN - 1] = 0; + tstrncpy(pQdesc->sql, pSql->sqlstr, sizeof(pQdesc->sql)); pQdesc->stime = htobe64(pSql->stime); pQdesc->queryId = htonl(pSql->queryId); pQdesc->useconds = htobe64(pSql->res.useconds); @@ -256,8 +255,7 @@ int tscBuildQueryStreamDesc(void *pMsg, STscObj *pObj) { SSqlStream *pStream = pObj->streamList; while (pStream) { - strncpy(pSdesc->sql, pStream->pSql->sqlstr, TSDB_SHOW_SQL_LEN - 1); - pSdesc->sql[TSDB_SHOW_SQL_LEN - 1] = 0; + tstrncpy(pSdesc->sql, pStream->pSql->sqlstr, sizeof(pSdesc->sql)); pSdesc->streamId = htonl(pStream->streamId); pSdesc->num = htobe64(pStream->num); diff --git a/src/client/src/tscSQLParser.c b/src/client/src/tscSQLParser.c index 5167add6cbcbb0c512297542a54f45536e7a532b..ab2d7d7830ff02bf3ba9f0b840f5ef842a1c455f 100644 --- a/src/client/src/tscSQLParser.c +++ b/src/client/src/tscSQLParser.c @@ -1309,7 +1309,7 @@ static void addProjectQueryCol(SQueryInfo* pQueryInfo, int32_t startPos, SColumn SSchema* pSchema = tscGetTableColumnSchema(pTableMeta, pIndex->columnIndex); char* colName = (pItem->aliasName == NULL) ? pSchema->name : pItem->aliasName; - strncpy(pExpr->aliasName, colName, tListLen(pExpr->aliasName)); + tstrncpy(pExpr->aliasName, colName, sizeof(pExpr->aliasName)); SColumnList ids = {0}; ids.num = 1; @@ -1359,7 +1359,7 @@ static int32_t doAddProjectionExprAndResultFields(SQueryInfo* pQueryInfo, SColum for (int32_t j = 0; j < numOfTotalColumns; ++j) { SSqlExpr* pExpr = doAddProjectCol(pQueryInfo, startPos + j, j, pIndex->tableIndex); - strncpy(pExpr->aliasName, pSchema[j].name, tListLen(pExpr->aliasName)); + tstrncpy(pExpr->aliasName, pSchema[j].name, sizeof(pExpr->aliasName)); pIndex->columnIndex = j; SColumnList ids = {0}; @@ -1453,7 +1453,7 @@ static int32_t setExprInfoForFunctions(SQueryInfo* pQueryInfo, SSchema* pSchema, } SSqlExpr* pExpr = tscSqlExprAppend(pQueryInfo, functionID, pColIndex, type, bytes, bytes, false); - strncpy(pExpr->aliasName, columnName, tListLen(pExpr->aliasName)); + tstrncpy(pExpr->aliasName, columnName, sizeof(pExpr->aliasName)); // for all queries, the timestamp column needs to be loaded SColumnIndex index = {.tableIndex = pColIndex->tableIndex, .columnIndex = PRIMARYKEY_TIMESTAMP_COL_INDEX}; diff --git a/src/client/src/tscServer.c b/src/client/src/tscServer.c index e0febc0e95d708ac11f05a3c5ba809d9d6734685..8815e463ad9742c0aacd1d38f1de6e5ca56617eb 100644 --- a/src/client/src/tscServer.c +++ b/src/client/src/tscServer.c @@ -866,7 +866,7 @@ int32_t tscBuildCreateDbMsg(SSqlObj *pSql, SSqlInfo *pInfo) { assert(pCmd->numOfClause == 1); STableMetaInfo *pTableMetaInfo = tscGetTableMetaInfoFromCmd(pCmd, pCmd->clauseIndex, 0); - strncpy(pCreateDbMsg->db, pTableMetaInfo->name, tListLen(pCreateDbMsg->db)); + tstrncpy(pCreateDbMsg->db, pTableMetaInfo->name, sizeof(pCreateDbMsg->db)); return TSDB_CODE_SUCCESS; } @@ -2153,8 +2153,8 @@ int tscProcessConnectRsp(SSqlObj *pSql) { strcpy(pObj->acctId, pConnect->acctId); // copy acctId from response int32_t len = sprintf(temp, "%s%s%s", pObj->acctId, TS_PATH_DELIMITER, pObj->db); - assert(len <= tListLen(pObj->db)); - strncpy(pObj->db, temp, tListLen(pObj->db)); + assert(len <= sizeof(pObj->db)); + tstrncpy(pObj->db, temp, sizeof(pObj->db)); if (pConnect->ipList.numOfIps > 0) tscSetMgmtIpList(&pConnect->ipList); @@ -2338,7 +2338,7 @@ static int32_t getTableMetaFromMgmt(SSqlObj *pSql, STableMetaInfo *pTableMetaInf STableMetaInfo *pNewMeterMetaInfo = tscAddEmptyMetaInfo(pNewQueryInfo); assert(pNew->cmd.numOfClause == 1 && pNewQueryInfo->numOfTables == 1); - strncpy(pNewMeterMetaInfo->name, pTableMetaInfo->name, tListLen(pNewMeterMetaInfo->name)); + tstrncpy(pNewMeterMetaInfo->name, pTableMetaInfo->name, sizeof(pNewMeterMetaInfo->name)); memcpy(pNew->cmd.payload, pSql->cmd.payload, pSql->cmd.payloadLen); // tag information if table does not exists. pNew->cmd.payloadLen = pSql->cmd.payloadLen; tscTrace("%p new pSqlObj:%p to get tableMeta, auto create:%d", pSql, pNew, pNew->cmd.autoCreated); diff --git a/src/client/src/tscSql.c b/src/client/src/tscSql.c index 723f03e9649fd8ccdde5268c51dd67f5280c8a13..5f2a8598dbc02b22084d1cded10993fd29c763b7 100644 --- a/src/client/src/tscSql.c +++ b/src/client/src/tscSql.c @@ -86,7 +86,7 @@ SSqlObj *taosConnectImpl(const char *ip, const char *user, const char *pass, con pObj->signature = pObj; - strncpy(pObj->user, user, TSDB_USER_LEN); + tstrncpy(pObj->user, user, sizeof(pObj->user)); taosEncryptPass((uint8_t *)pass, strlen(pass), pObj->pass); if (db) { diff --git a/src/client/src/tscSystem.c b/src/client/src/tscSystem.c index fd7d31aa9f198aa14ccec0763d4fedd31e043c09..66fbf9fc9701d4bc33c0dd4a11b17faa53d361bc 100644 --- a/src/client/src/tscSystem.c +++ b/src/client/src/tscSystem.c @@ -180,7 +180,7 @@ static int taos_options_imp(TSDB_OPTION option, const char *pStr) { assert(cfg != NULL); if (cfg->cfgStatus <= TAOS_CFG_CSTATUS_OPTION) { - strncpy(configDir, pStr, TSDB_FILENAME_LEN); + tstrncpy(configDir, pStr, TSDB_FILENAME_LEN); cfg->cfgStatus = TAOS_CFG_CSTATUS_OPTION; tscPrint("set config file directory:%s", pStr); } else { @@ -234,7 +234,7 @@ static int taos_options_imp(TSDB_OPTION option, const char *pStr) { tscPrint("failed to set locale:%s, current locale:%s", pStr, tsLocale); } - strncpy(tsLocale, locale, tListLen(tsLocale)); + tstrncpy(tsLocale, locale, sizeof(tsLocale)); char *charset = strrchr(tsLocale, sep); if (charset != NULL) { @@ -249,7 +249,7 @@ static int taos_options_imp(TSDB_OPTION option, const char *pStr) { tscPrint("charset changed from %s to %s", tsCharset, charset); } - strncpy(tsCharset, charset, tListLen(tsCharset)); + tstrncpy(tsCharset, charset, sizeof(tsCharset)); cfg->cfgStatus = TAOS_CFG_CSTATUS_OPTION; } else { @@ -286,7 +286,7 @@ static int taos_options_imp(TSDB_OPTION option, const char *pStr) { tscPrint("charset changed from %s to %s", tsCharset, pStr); } - strncpy(tsCharset, pStr, tListLen(tsCharset)); + tstrncpy(tsCharset, pStr, sizeof(tsCharset)); cfg->cfgStatus = TAOS_CFG_CSTATUS_OPTION; } else { tscPrint("charset:%s not valid", pStr); @@ -324,7 +324,7 @@ static int taos_options_imp(TSDB_OPTION option, const char *pStr) { // return -1; // } - strncpy(tsSocketType, pStr, tListLen(tsSocketType)); + tstrncpy(tsSocketType, pStr, sizeof(tsSocketType)); cfg->cfgStatus = TAOS_CFG_CSTATUS_OPTION; tscPrint("socket type is set:%s", tsSocketType); } diff --git a/src/os/darwin/src/darwinPlatform.c b/src/os/darwin/src/darwinPlatform.c index 05290c43f4edb6b413266c73ae149da7a967a1b7..cae65a292729e01da7ced54e40af040b08cc1ac2 100644 --- a/src/os/darwin/src/darwinPlatform.c +++ b/src/os/darwin/src/darwinPlatform.c @@ -189,7 +189,7 @@ void taosGetSystemLocale() { if (cfg_locale && cfg_locale->cfgStatus < TAOS_CFG_CSTATUS_DEFAULT) { char *locale = setlocale(LC_CTYPE, "chs"); if (locale != NULL) { - strncpy(tsLocale, locale, sizeof(tsLocale) / sizeof(tsLocale[0])); + tstrncpy(tsLocale, locale, sizeof(tsLocale)); cfg_locale->cfgStatus = TAOS_CFG_CSTATUS_DEFAULT; uPrint("locale not configured, set to default:%s", tsLocale); } diff --git a/src/os/linux/src/linuxSysPara.c b/src/os/linux/src/linuxSysPara.c index b270eb14cf4c652a531cb0228c949868dc58f7a9..3ec7de3b635253d6936be9137c4c3ee6ff8b4d15 100644 --- a/src/os/linux/src/linuxSysPara.c +++ b/src/os/linux/src/linuxSysPara.c @@ -229,7 +229,7 @@ static void taosGetSystemLocale() { // get and set default locale uError("can't get locale from system, set it to en_US.UTF-8"); strcpy(tsLocale, "en_US.UTF-8"); } else { - tstrncpy(tsLocale, locale, tListLen(tsLocale)); + tstrncpy(tsLocale, locale, sizeof(tsLocale)); uError("locale not configured, set to system default:%s", tsLocale); } } @@ -242,7 +242,7 @@ static void taosGetSystemLocale() { // get and set default locale str++; char *revisedCharset = taosCharsetReplace(str); - strncpy(tsCharset, revisedCharset, tListLen(tsCharset)); + tstrncpy(tsCharset, revisedCharset, sizeof(tsCharset)); free(revisedCharset); uWarn("charset not configured, set to system default:%s", tsCharset); diff --git a/src/os/windows/src/twindows.c b/src/os/windows/src/twindows.c index 76b354f7844325f9f35655c640fe43672ec1f94e..82bcc69b11495db45fb1fbc9c943af09c75d0c2e 100644 --- a/src/os/windows/src/twindows.c +++ b/src/os/windows/src/twindows.c @@ -226,7 +226,7 @@ void taosGetSystemLocale() { if (cfg_locale && cfg_locale->cfgStatus < TAOS_CFG_CSTATUS_DEFAULT) { char *locale = setlocale(LC_CTYPE, "chs"); if (locale != NULL) { - strncpy(tsLocale, locale, sizeof(tsLocale) / sizeof(tsLocale[0])); + tstrncpy(tsLocale, locale, sizeof(tsLocale)); cfg_locale->cfgStatus = TAOS_CFG_CSTATUS_DEFAULT; uPrint("locale not configured, set to default:%s", tsLocale); } diff --git a/src/query/src/qExecutor.c b/src/query/src/qExecutor.c index 09fed075733b122db8b2fe6b7e48f3e1a41e8ec7..92c23f6b8377a34876f15bd7bf9de40878c2d933 100644 --- a/src/query/src/qExecutor.c +++ b/src/query/src/qExecutor.c @@ -6038,7 +6038,6 @@ static void buildTagQueryResult(SQInfo* pQInfo) { for(int32_t j = 0; j < pQuery->numOfOutput; ++j) { if (pExprInfo[j].base.colInfo.colId == TSDB_TBNAME_COLUMN_INDEX) { char* data = tsdbGetTableName(pQInfo->tsdb, &item->id); - char* dst = pQuery->sdata[j]->data + count * ((TSDB_TABLE_NAME_LEN - 1) + VARSTR_HEADER_SIZE); memcpy(dst, data, varDataTLen(data)); } else {// todo refactor diff --git a/src/query/src/qtsbuf.c b/src/query/src/qtsbuf.c index 555ccb7318bed924ededc0fd13cdf1a9c2345997..869299f30936bb7fd7e40a89b02c15d06f307b8d 100644 --- a/src/query/src/qtsbuf.c +++ b/src/query/src/qtsbuf.c @@ -51,7 +51,7 @@ STSBuf* tsBufCreateFromFile(const char* path, bool autoDelete) { return NULL; } - strncpy(pTSBuf->path, path, PATH_MAX); + tstrncpy(pTSBuf->path, path, sizeof(pTSBuf->path)); pTSBuf->f = fopen(pTSBuf->path, "r+"); if (pTSBuf->f == NULL) { diff --git a/src/rpc/src/rpcMain.c b/src/rpc/src/rpcMain.c index b0a5a0bfc0d2c53aec4187de265c6726494273a0..604406112717bef5b61671032cd59b8606c54e5d 100644 --- a/src/rpc/src/rpcMain.c +++ b/src/rpc/src/rpcMain.c @@ -458,7 +458,7 @@ int rpcGetConnInfo(void *thandle, SRpcConnInfo *pInfo) { pInfo->clientPort = pConn->peerPort; // pInfo->serverIp = pConn->destIp; - strncpy(pInfo->user, pConn->user, sizeof(pInfo->user)); + tstrncpy(pInfo->user, pConn->user, sizeof(pInfo->user)); return 0; } diff --git a/src/util/src/tsched.c b/src/util/src/tsched.c index 898ab7087617beb5ab2fdff99e7300952edcda82..6fc767fd3bd2e37287de1a41e5dafb43a14976af 100644 --- a/src/util/src/tsched.c +++ b/src/util/src/tsched.c @@ -15,6 +15,7 @@ #include "os.h" #include "taosdef.h" +#include "tutil.h" #include "tulog.h" #include "tsched.h" #include "ttimer.h" @@ -62,8 +63,7 @@ void *taosInitScheduler(int queueSize, int numOfThreads, const char *label) { } pSched->queueSize = queueSize; - strncpy(pSched->label, label, sizeof(pSched->label)); // fix buffer overflow - pSched->label[sizeof(pSched->label)-1] = '\0'; + tstrncpy(pSched->label, label, sizeof(pSched->label)); // fix buffer overflow pSched->fullSlot = 0; pSched->emptySlot = 0; diff --git a/src/util/src/ttimer.c b/src/util/src/ttimer.c index 68db574d82f3e668ced4b8895c3427640641079e..cdb2c6c0beb5ea0223fb077dde20c08a5966a294 100644 --- a/src/util/src/ttimer.c +++ b/src/util/src/ttimer.c @@ -552,8 +552,7 @@ void* taosTmrInit(int maxNumOfTmrs, int resolution, int longest, const char* lab return NULL; } - strncpy(ctrl->label, label, sizeof(ctrl->label)); - ctrl->label[sizeof(ctrl->label) - 1] = 0; + tstrncpy(ctrl->label, label, sizeof(ctrl->label)); tmrTrace("%s timer controller is initialized, number of timer controllers: %d.", label, numOfTmrCtrl); return ctrl; } diff --git a/tests/tsim/src/simParse.c b/tests/tsim/src/simParse.c index fddda679c681e12d8dbb8bcb82315f13b718dbbe..9fea1f115b2b12b12368391172f239a32efe5d5a 100644 --- a/tests/tsim/src/simParse.c +++ b/tests/tsim/src/simParse.c @@ -165,7 +165,7 @@ SScript *simBuildScriptObj(char *fileName) { script->type = SIM_SCRIPT_TYPE_MAIN; script->numOfLines = numOfLines; - strncpy(script->fileName, fileName, MAX_FILE_NAME_LEN); + tstrncpy(script->fileName, fileName, sizeof(script->fileName)); script->optionBuffer = malloc(optionOffset); memcpy(script->optionBuffer, optionBuffer, optionOffset);