From 88f0d2c83680ddfa765ab5a6f1bf9a8fb1e537a3 Mon Sep 17 00:00:00 2001 From: Bomin Zhang Date: Tue, 9 Jun 2020 10:05:41 +0800 Subject: [PATCH] TD-459: fix some strncpy --- src/client/src/tscParseInsert.c | 8 ++++---- src/client/src/tscProfile.c | 6 ++---- src/client/src/tscSQLParser.c | 6 +++--- src/client/src/tscServer.c | 8 ++++---- src/client/src/tscSql.c | 2 +- src/client/src/tscSystem.c | 10 +++++----- src/os/darwin/src/darwinPlatform.c | 2 +- src/os/linux/src/linuxSysPara.c | 4 ++-- src/os/windows/src/twindows.c | 2 +- src/query/src/qExecutor.c | 1 - src/query/src/qtsbuf.c | 2 +- src/rpc/src/rpcMain.c | 2 +- src/util/src/tsched.c | 4 ++-- src/util/src/ttimer.c | 3 +-- tests/tsim/src/simParse.c | 2 +- 15 files changed, 29 insertions(+), 33 deletions(-) diff --git a/src/client/src/tscParseInsert.c b/src/client/src/tscParseInsert.c index cbc83c6e75..c4749e4611 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 79d00bf5dc..55243c4382 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 5167add6cb..ab2d7d7830 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 e0febc0e95..8815e463ad 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 723f03e964..5f2a8598db 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 fd7d31aa9f..66fbf9fc97 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 05290c43f4..cae65a2927 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 b270eb14cf..3ec7de3b63 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 76b354f784..82bcc69b11 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 09fed07573..92c23f6b83 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 555ccb7318..869299f309 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 b0a5a0bfc0..6044061127 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 898ab70876..6fc767fd3b 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 68db574d82..cdb2c6c0be 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 fddda679c6..9fea1f115b 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); -- GitLab