From 6d717a2b571e1e0e8dcfb19d0417e4bdf50fd358 Mon Sep 17 00:00:00 2001 From: Bomin Zhang Date: Tue, 9 Jun 2020 15:48:49 +0800 Subject: [PATCH] TD-459: fix coverity issues 290707 288014 290195 290193 290186 288205 288005 286989 286970 286967 278634 268232 268153 268075 267950 267940 267724 267855 267732 --- src/client/src/tscSQLParser.c | 2 +- src/client/src/tscServer.c | 10 +++++----- src/client/src/tscUtil.c | 4 ++-- src/cq/src/cqMain.c | 8 ++++---- src/inc/tcq.h | 2 +- src/kit/taosdemo/taosdemo.c | 2 +- src/kit/taosdump/taosdump.c | 4 ++++ src/mnode/inc/mnodeDef.h | 4 ++-- src/mnode/src/mnodeTable.c | 4 ++-- src/os/linux/src/linuxSysPara.c | 2 +- src/plugins/http/src/httpAuth.c | 4 ++-- src/plugins/monitor/src/monitorMain.c | 2 +- src/query/src/qExecutor.c | 2 +- tests/tsim/src/simMain.c | 2 +- 14 files changed, 28 insertions(+), 24 deletions(-) diff --git a/src/client/src/tscSQLParser.c b/src/client/src/tscSQLParser.c index ab2d7d7830..372e81d737 100644 --- a/src/client/src/tscSQLParser.c +++ b/src/client/src/tscSQLParser.c @@ -5420,7 +5420,7 @@ int32_t doLocalQueryProcess(SQueryInfo* pQueryInfo, SQuerySQL* pQuerySql) { tDataTypeDesc[TSDB_DATA_TYPE_INT].nSize, tDataTypeDesc[TSDB_DATA_TYPE_INT].nSize, false); const char* name = (pExprList->a[0].aliasName != NULL)? pExprList->a[0].aliasName:functionsInfo[index].name; - strncpy(pExpr1->aliasName, name, tListLen(pExpr1->aliasName)); + tstrncpy(pExpr1->aliasName, name, tListLen(pExpr1->aliasName)); return TSDB_CODE_SUCCESS; } diff --git a/src/client/src/tscServer.c b/src/client/src/tscServer.c index 8815e463ad..f01ec1bece 100644 --- a/src/client/src/tscServer.c +++ b/src/client/src/tscServer.c @@ -1018,7 +1018,7 @@ int32_t tscBuildDropDnodeMsg(SSqlObj *pSql, SSqlInfo *pInfo) { SCMDropDnodeMsg *pDrop = (SCMDropDnodeMsg *)pCmd->payload; STableMetaInfo *pTableMetaInfo = tscGetTableMetaInfoFromCmd(pCmd, pCmd->clauseIndex, 0); - strcpy(pDrop->ep, pTableMetaInfo->name); + tstrncpy(pDrop->ep, pTableMetaInfo->name, sizeof(pDrop->ep)); pCmd->msgType = TSDB_MSG_TYPE_CM_DROP_DNODE; return TSDB_CODE_SUCCESS; @@ -1036,7 +1036,7 @@ int32_t tscBuildDropUserMsg(SSqlObj *pSql, SSqlInfo *pInfo) { SCMDropUserMsg *pDropMsg = (SCMDropUserMsg*)pCmd->payload; STableMetaInfo *pTableMetaInfo = tscGetTableMetaInfoFromCmd(pCmd, pCmd->clauseIndex, 0); - strcpy(pDropMsg->user, pTableMetaInfo->name); + tstrncpy(pDropMsg->user, pTableMetaInfo->name, sizeof(pDropMsg->user)); return TSDB_CODE_SUCCESS; } @@ -1091,9 +1091,9 @@ int32_t tscBuildShowMsg(SSqlObj *pSql, SSqlInfo *pInfo) { STableMetaInfo *pTableMetaInfo = tscGetTableMetaInfoFromCmd(pCmd, pCmd->clauseIndex, 0); size_t nameLen = strlen(pTableMetaInfo->name); if (nameLen > 0) { - strcpy(pShowMsg->db, pTableMetaInfo->name); // prefix is set here + tstrncpy(pShowMsg->db, pTableMetaInfo->name, sizeof(pShowMsg->db)); // prefix is set here } else { - strcpy(pShowMsg->db, pObj->db); + tstrncpy(pShowMsg->db, pObj->db, sizeof(pShowMsg->db)); } SShowInfo *pShowInfo = &pInfo->pDCLInfo->showOpt; @@ -1300,7 +1300,7 @@ int tscAlterDbMsg(SSqlObj *pSql, SSqlInfo *pInfo) { SCMAlterDbMsg *pAlterDbMsg = (SCMAlterDbMsg*)pCmd->payload; STableMetaInfo *pTableMetaInfo = tscGetTableMetaInfoFromCmd(pCmd, pCmd->clauseIndex, 0); - strcpy(pAlterDbMsg->db, pTableMetaInfo->name); + tstrncpy(pAlterDbMsg->db, pTableMetaInfo->name, sizeof(pAlterDbMsg->db)); return TSDB_CODE_SUCCESS; } diff --git a/src/client/src/tscUtil.c b/src/client/src/tscUtil.c index a810e5d4be..2701d2b572 100644 --- a/src/client/src/tscUtil.c +++ b/src/client/src/tscUtil.c @@ -593,7 +593,7 @@ int32_t tscCreateDataBlock(size_t initialSize, int32_t rowSize, int32_t startOff dataBuf->size = startOffset; dataBuf->tsSource = -1; - strncpy(dataBuf->tableId, name, TSDB_TABLE_ID_LEN); + tstrncpy(dataBuf->tableId, name, sizeof(dataBuf->tableId)); /* * The table meta may be released since the table meta cache are completed clean by other thread @@ -1666,7 +1666,7 @@ STableMetaInfo* tscAddTableMetaInfo(SQueryInfo* pQueryInfo, const char* name, ST assert(pTableMetaInfo != NULL); if (name != NULL) { - strncpy(pTableMetaInfo->name, name, TSDB_TABLE_ID_LEN); + tstrncpy(pTableMetaInfo->name, name, sizeof(pTableMetaInfo->name)); } pTableMetaInfo->pTableMeta = pTableMeta; diff --git a/src/cq/src/cqMain.c b/src/cq/src/cqMain.c index edb588b554..13f3c0a2b0 100644 --- a/src/cq/src/cqMain.c +++ b/src/cq/src/cqMain.c @@ -73,8 +73,8 @@ void *cqOpen(void *ahandle, const SCqCfg *pCfg) { return NULL; } - strcpy(pContext->user, pCfg->user); - strcpy(pContext->pass, pCfg->pass); + tstrncpy(pContext->user, pCfg->user, sizeof(pContext->user)); + tstrncpy(pContext->pass, pCfg->pass, sizeof(pContext->pass)); const char* db = pCfg->db; for (const char* p = db; *p != 0; p++) { if (*p == '.') { @@ -82,7 +82,7 @@ void *cqOpen(void *ahandle, const SCqCfg *pCfg) { break; } } - strcpy(pContext->db, db); + tstrncpy(pContext->db, db, sizeof(pContext->db)); pContext->vgId = pCfg->vgId; pContext->cqWrite = pCfg->cqWrite; pContext->ahandle = ahandle; @@ -215,7 +215,7 @@ void cqDrop(void *handle) { cTrace("vgId:%d, id:%d CQ:%s is dropped", pContext->vgId, pObj->tid, pObj->sqlStr); free(pObj); - pthread_mutex_lock(&pContext->mutex); + pthread_mutex_unlock(&pContext->mutex); } static void cqCreateStream(SCqContext *pContext, SCqObj *pObj) { diff --git a/src/inc/tcq.h b/src/inc/tcq.h index 9d987da468..32b75674c3 100644 --- a/src/inc/tcq.h +++ b/src/inc/tcq.h @@ -27,7 +27,7 @@ typedef struct { int vgId; char user[TSDB_USER_LEN]; char pass[TSDB_PASSWORD_LEN]; - char db[TSDB_DB_NAME_LEN + 1]; + char db[TSDB_DB_NAME_LEN]; FCqWrite cqWrite; } SCqCfg; diff --git a/src/kit/taosdemo/taosdemo.c b/src/kit/taosdemo/taosdemo.c index ca0af96145..8fb894a588 100644 --- a/src/kit/taosdemo/taosdemo.c +++ b/src/kit/taosdemo/taosdemo.c @@ -756,7 +756,7 @@ void *readMetric(void *sarg) { for (int j = 0; j < n; j++) { char condition[BUFFER_SIZE - 30] = "\0"; - char tempS[BUFFER_SIZE] = "\0"; + char tempS[64] = "\0"; int m = 10 < num_of_tables ? 10 : num_of_tables; diff --git a/src/kit/taosdump/taosdump.c b/src/kit/taosdump/taosdump.c index 417de629aa..6cb57ef0d5 100644 --- a/src/kit/taosdump/taosdump.c +++ b/src/kit/taosdump/taosdump.c @@ -643,6 +643,8 @@ int taosDumpDb(SDbInfo *dbInfo, SDumpArguments *arguments, FILE *fp) { lseek(fd, 0, SEEK_SET); while (read(fd, &tableRecord, sizeof(STableRecord)) > 0) { + tableRecord.name[sizeof(tableRecord.name) - 1] = 0; + tableRecord.metric[sizeof(tableRecord.metric) - 1] = 0; taosDumpTable(tableRecord.name, tableRecord.metric, arguments, fp); } @@ -902,6 +904,8 @@ int32_t taosDumpMetric(char *metric, SDumpArguments *arguments, FILE *fp) { lseek(fd, 0, SEEK_SET); while (read(fd, &tableRecord, sizeof(STableRecord)) > 0) { + tableRecord.name[sizeof(tableRecord.name) - 1] = 0; + tableRecord.metric[sizeof(tableRecord.metric) - 1] = 0; taosDumpTable(tableRecord.name, tableRecord.metric, arguments, fp); } diff --git a/src/mnode/inc/mnodeDef.h b/src/mnode/inc/mnodeDef.h index 35ae9aad33..14cd405763 100644 --- a/src/mnode/inc/mnodeDef.h +++ b/src/mnode/inc/mnodeDef.h @@ -32,8 +32,8 @@ struct SMnodeObj; typedef struct SDnodeObj { int32_t dnodeId; uint16_t dnodePort; - char dnodeFqdn[TSDB_FQDN_LEN + 1]; - char dnodeEp[TSDB_EP_LEN + 1]; + char dnodeFqdn[TSDB_FQDN_LEN]; + char dnodeEp[TSDB_EP_LEN]; int64_t createdTime; uint32_t lastAccess; int32_t openVnodes; diff --git a/src/mnode/src/mnodeTable.c b/src/mnode/src/mnodeTable.c index cde94c39b5..a2ddd9893f 100644 --- a/src/mnode/src/mnodeTable.c +++ b/src/mnode/src/mnodeTable.c @@ -1769,8 +1769,8 @@ static int32_t mnodeAutoCreateChildTable(SMnodeMsg *pMsg) { return TSDB_CODE_MND_OUT_OF_MEMORY; } - strncpy(pCreateMsg->tableId, pInfo->tableId, tListLen(pInfo->tableId)); - strcpy(pCreateMsg->db, pMsg->pDb->name); + tstrncpy(pCreateMsg->tableId, pInfo->tableId, sizeof(pInfo->tableId)); + tstrncpy(pCreateMsg->db, pMsg->pDb->name, sizeof(pCreateMsg->db)); pCreateMsg->igExists = 1; pCreateMsg->getMeta = 1; pCreateMsg->contLen = htonl(contLen); diff --git a/src/os/linux/src/linuxSysPara.c b/src/os/linux/src/linuxSysPara.c index 3ec7de3b63..8ae2630a15 100644 --- a/src/os/linux/src/linuxSysPara.c +++ b/src/os/linux/src/linuxSysPara.c @@ -160,7 +160,7 @@ static void taosGetSystemTimezone() { /* load time zone string from /etc/timezone */ FILE *f = fopen("/etc/timezone", "r"); - char buf[64] = {0}; + char buf[65] = {0}; if (f != NULL) { fread(buf, 64, 1, f); fclose(f); diff --git a/src/plugins/http/src/httpAuth.c b/src/plugins/http/src/httpAuth.c index 594f51e275..ccf39642c9 100644 --- a/src/plugins/http/src/httpAuth.c +++ b/src/plugins/http/src/httpAuth.c @@ -88,8 +88,8 @@ bool httpParseTaosdAuthToken(HttpContext *pContext, char *token, int len) { free(base64); return false; } else { - strncpy(pContext->user, descrypt, TSDB_USER_LEN); - strncpy(pContext->pass, descrypt + TSDB_USER_LEN, TSDB_PASSWORD_LEN); + tstrncpy(pContext->user, descrypt, sizeof(pContext->user)); + tstrncpy(pContext->pass, descrypt + TSDB_USER_LEN, TSDB_PASSWORD_LEN); httpTrace("context:%p, fd:%d, ip:%s, taosd token:%s parsed success, user:%s", pContext, pContext->fd, pContext->ipstr, token, pContext->user); diff --git a/src/plugins/monitor/src/monitorMain.c b/src/plugins/monitor/src/monitorMain.c index 01e276da64..ff9faa845c 100644 --- a/src/plugins/monitor/src/monitorMain.c +++ b/src/plugins/monitor/src/monitorMain.c @@ -156,7 +156,7 @@ static void dnodeBuildMonitorSql(char *sql, int32_t cmd) { ", io_read float, io_write float" ", req_http int, req_select int, req_insert int" ") tags (dnodeid int, fqdn binary(%d))", - tsMonitorDbName, TSDB_FQDN_LEN + 1); + tsMonitorDbName, TSDB_FQDN_LEN); } else if (cmd == MONITOR_CMD_CREATE_TB_DN) { snprintf(sql, SQL_LENGTH, "create table if not exists %s.dn%d using %s.dn tags(%d, '%s')", tsMonitorDbName, dnodeGetDnodeId(), tsMonitorDbName, dnodeGetDnodeId(), tsLocalEp); diff --git a/src/query/src/qExecutor.c b/src/query/src/qExecutor.c index 92c23f6b83..65a5ef45d3 100644 --- a/src/query/src/qExecutor.c +++ b/src/query/src/qExecutor.c @@ -5242,7 +5242,7 @@ static int32_t createFilterInfo(void *pQInfo, SQuery *pQuery) { if (pQuery->colList[i].numOfFilters > 0) { SSingleColumnFilterInfo *pFilterInfo = &pQuery->pFilterInfo[j]; - memcpy(&pFilterInfo->info, &pQuery->colList[i], sizeof(SColumnInfoData)); + memcpy(&pFilterInfo->info, &pQuery->colList[i], sizeof(SColumnInfo)); pFilterInfo->info = pQuery->colList[i]; pFilterInfo->numOfFilters = pQuery->colList[i].numOfFilters; diff --git a/tests/tsim/src/simMain.c b/tests/tsim/src/simMain.c index 2540851634..f016e36d41 100644 --- a/tests/tsim/src/simMain.c +++ b/tests/tsim/src/simMain.c @@ -29,7 +29,7 @@ int main(int argc, char *argv[]) { for (int i = 1; i < argc; ++i) { if (strcmp(argv[i], "-c") == 0 && i < argc - 1) { - strncpy(configDir, argv[++i], MAX_FILE_NAME_LEN); + tstrncpy(configDir, argv[++i], MAX_FILE_NAME_LEN); } else if (strcmp(argv[i], "-f") == 0 && i < argc - 1) { strcpy(scriptFile, argv[++i]); } else if (strcmp(argv[i], "-a") == 0) { -- GitLab