From 1317dbc53b68c15d5270297acb7194de5561e027 Mon Sep 17 00:00:00 2001 From: Shuduo Sang Date: Fri, 1 May 2020 16:33:06 +0800 Subject: [PATCH] fix part of issues reported by TScanCode. [TD-217] --- src/client/src/tscSecondaryMerge.c | 8 +++++++- src/client/src/tscServer.c | 8 ++++++-- src/client/src/tscStream.c | 4 ++-- src/client/src/tscUtil.c | 4 +++- src/dnode/src/dnodeMain.c | 1 + src/kit/shell/src/shellImport.c | 1 + src/kit/shell/src/shellLinux.c | 15 +++++++++++++-- src/kit/taosdemo/taosdemo.c | 3 +++ src/kit/taosdump/taosdump.c | 6 ++---- src/mnode/src/mgmtShell.c | 4 +++- src/mnode/src/mgmtVgroup.c | 6 +++++- src/query/src/qparserImpl.c | 4 ++-- src/query/src/qtsbuf.c | 10 +++++++--- src/query/src/queryUtil.c | 5 ++++- src/tsdb/src/tsdbRead.c | 2 +- 15 files changed, 60 insertions(+), 21 deletions(-) diff --git a/src/client/src/tscSecondaryMerge.c b/src/client/src/tscSecondaryMerge.c index 84f14abf4c..dbf17b56c5 100644 --- a/src/client/src/tscSecondaryMerge.c +++ b/src/client/src/tscSecondaryMerge.c @@ -140,7 +140,13 @@ void tscCreateLocalReducer(tExtMemBuffer **pMemBuffer, int32_t numOfBuffer, tOrd // offset of cmd in SSqlObj structure char *pSqlObjAddr = (char *)pCmd - offsetof(SSqlObj, cmd); - if (pMemBuffer == NULL || pDesc->pColumnModel == NULL) { + if (pMemBuffer == NULL) { + tscError("%p pMemBuffer", pMemBuffer); + pRes->code = TSDB_CODE_APP_ERROR; + return; + } + + if (pDesc->pColumnModel == NULL) { tscLocalReducerEnvDestroy(pMemBuffer, pDesc, finalmodel, numOfBuffer); tscError("%p no local buffer or intermediate result format model", pSqlObjAddr); diff --git a/src/client/src/tscServer.c b/src/client/src/tscServer.c index 9b44bea82d..7874d0e816 100644 --- a/src/client/src/tscServer.c +++ b/src/client/src/tscServer.c @@ -228,7 +228,11 @@ int tscSendMsgToServer(SSqlObj *pSql) { void tscProcessMsgFromServer(SRpcMsg *rpcMsg) { SSqlObj *pSql = (SSqlObj *)rpcMsg->handle; - if (pSql == NULL || pSql->signature != pSql) { + if (pSql == NULL) { + tscError("%p sql is already released", pSql->signature); + return; + } + if (pSql->signature != pSql) { tscError("%p sql is already released, signature:%p", pSql, pSql->signature); return; } @@ -310,7 +314,7 @@ void tscProcessMsgFromServer(SRpcMsg *rpcMsg) { pRes->rspType = rpcMsg->msgType; pRes->rspLen = rpcMsg->contLen; - if (pRes->rspLen > 0) { + if (pRes->rspLen > 0 && rpcMsg->pCont) { char *tmp = (char *)realloc(pRes->pRsp, pRes->rspLen); if (tmp == NULL) { pRes->code = TSDB_CODE_CLI_OUT_OF_MEMORY; diff --git a/src/client/src/tscStream.c b/src/client/src/tscStream.c index d690681729..5f5af09cf8 100644 --- a/src/client/src/tscStream.c +++ b/src/client/src/tscStream.c @@ -172,17 +172,17 @@ static void tscSetTimestampForRes(SSqlStream *pStream, SSqlObj *pSql) { static void tscProcessStreamRetrieveResult(void *param, TAOS_RES *res, int numOfRows) { SSqlStream * pStream = (SSqlStream *)param; SSqlObj * pSql = (SSqlObj *)res; - STableMetaInfo *pTableMetaInfo = tscGetTableMetaInfoFromCmd(&pSql->cmd, 0, 0); if (pSql == NULL || numOfRows < 0) { int64_t retryDelayTime = tscGetRetryDelayTime(pStream->slidingTime, pStream->precision); tscError("%p stream:%p, retrieve data failed, code:%d, retry in %" PRId64 "ms", pSql, pStream, numOfRows, retryDelayTime); - tscClearTableMetaInfo(pTableMetaInfo, true); tscSetRetryTimer(pStream, pStream->pSql, retryDelayTime); return; } + STableMetaInfo *pTableMetaInfo = tscGetTableMetaInfoFromCmd(&pSql->cmd, 0, 0); + if (numOfRows > 0) { // when reaching here the first execution of stream computing is successful. pStream->numOfRes += numOfRows; SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(&pSql->cmd, 0); diff --git a/src/client/src/tscUtil.c b/src/client/src/tscUtil.c index c0cfa4d3af..506fa1a605 100644 --- a/src/client/src/tscUtil.c +++ b/src/client/src/tscUtil.c @@ -757,7 +757,9 @@ void tscCloseTscObj(STscObj* pObj) { taosTmrStopA(&(pObj->pTimer)); tscFreeSqlObj(pSql); - sem_destroy(&pSql->rspSem); + if (pSql) { + sem_destroy(&pSql->rspSem); + } rpcClose(pObj->pMgmtConn); pthread_mutex_destroy(&pObj->mutex); diff --git a/src/dnode/src/dnodeMain.c b/src/dnode/src/dnodeMain.c index 6aa42f05c7..940b884927 100644 --- a/src/dnode/src/dnodeMain.c +++ b/src/dnode/src/dnodeMain.c @@ -213,6 +213,7 @@ static void dnodeCheckDataDirOpenned(char *dir) { int32_t ret = flock(fd, LOCK_EX | LOCK_NB); if (ret != 0) { dError("failed to lock file:%s ret:%d, database may be running, quit", filepath, ret); + close(fd); exit(0); } } diff --git a/src/kit/shell/src/shellImport.c b/src/kit/shell/src/shellImport.c index 256b251075..e5c50bb74e 100644 --- a/src/kit/shell/src/shellImport.c +++ b/src/kit/shell/src/shellImport.c @@ -142,6 +142,7 @@ static void shellSourceFile(TAOS *con, char *fptr) { if (wordexp(fptr, &full_path, 0) != 0) { fprintf(stderr, "ERROR: illegal file name\n"); + free(cmd); return; } diff --git a/src/kit/shell/src/shellLinux.c b/src/kit/shell/src/shellLinux.c index d3453cda36..22ffa78c81 100644 --- a/src/kit/shell/src/shellLinux.c +++ b/src/kit/shell/src/shellLinux.c @@ -62,7 +62,13 @@ static error_t parse_opt(int key, char *arg, struct argp_state *state) { if (arg) arguments->password = arg; break; case 'P': - tsMnodeShellPort = atoi(arg); + if (arg) { + tsMnodeShellPort = atoi(arg); + } else { + fprintf(stderr, "Invalid port\n"); + return -1; + } + break; case 't': arguments->timezone = arg; @@ -101,7 +107,12 @@ static error_t parse_opt(int key, char *arg, struct argp_state *state) { wordfree(&full_path); break; case 'T': - arguments->threadNum = atoi(arg); + if (arg) { + arguments->threadNum = atoi(arg); + } else { + fprintf(stderr, "Invalid number of threads\n"); + return -1; + } break; case 'd': arguments->database = arg; diff --git a/src/kit/taosdemo/taosdemo.c b/src/kit/taosdemo/taosdemo.c index 24855ab8b5..937c8d177d 100644 --- a/src/kit/taosdemo/taosdemo.c +++ b/src/kit/taosdemo/taosdemo.c @@ -340,6 +340,9 @@ int main(int argc, char *argv[]) { int count_data_type = 0; char dataString[512]; bool do_aggreFunc = true; + + memset(dataString, 0, 512); + if (strcasecmp(data_type[0], "BINARY") == 0 || strcasecmp(data_type[0], "BOOL") == 0) { do_aggreFunc = false; } diff --git a/src/kit/taosdump/taosdump.c b/src/kit/taosdump/taosdump.c index 2e64c9bccc..ed98a9b92c 100644 --- a/src/kit/taosdump/taosdump.c +++ b/src/kit/taosdump/taosdump.c @@ -383,14 +383,13 @@ int taosGetTableRecordInfo(char *table, STableRecordInfo *pTableRecordInfo) { TAOS_FIELD *fields = taos_fetch_fields(result); - while ((row = taos_fetch_row(result)) != NULL) { + if ((row = taos_fetch_row(result)) != NULL) { isSet = true; pTableRecordInfo->isMetric = false; strncpy(pTableRecordInfo->tableRecord.name, (char *)row[TSDB_SHOW_TABLES_NAME_INDEX], fields[TSDB_SHOW_TABLES_NAME_INDEX].bytes); strncpy(pTableRecordInfo->tableRecord.metric, (char *)row[TSDB_SHOW_TABLES_METRIC_INDEX], fields[TSDB_SHOW_TABLES_METRIC_INDEX].bytes); - break; } taos_free_result(result); @@ -410,11 +409,10 @@ int taosGetTableRecordInfo(char *table, STableRecordInfo *pTableRecordInfo) { return -1; } - while ((row = taos_fetch_row(result)) != NULL) { + if ((row = taos_fetch_row(result)) != NULL) { isSet = true; pTableRecordInfo->isMetric = true; strcpy(pTableRecordInfo->tableRecord.metric, table); - break; } taos_free_result(result); diff --git a/src/mnode/src/mgmtShell.c b/src/mnode/src/mgmtShell.c index 193521b026..05cacbb95c 100644 --- a/src/mnode/src/mgmtShell.c +++ b/src/mnode/src/mgmtShell.c @@ -149,7 +149,9 @@ void mgmtDealyedAddToShellQueue(SQueuedMsg *queuedMsg) { } static void mgmtProcessMsgFromShell(SRpcMsg *rpcMsg) { - if (rpcMsg == NULL || rpcMsg->pCont == NULL) { + assert(rpcMsg); + + if (rpcMsg->pCont == NULL) { mgmtSendSimpleResp(rpcMsg->handle, TSDB_CODE_INVALID_MSG_LEN); return; } diff --git a/src/mnode/src/mgmtVgroup.c b/src/mnode/src/mgmtVgroup.c index 839dce5c38..b3260850ad 100644 --- a/src/mnode/src/mgmtVgroup.c +++ b/src/mnode/src/mgmtVgroup.c @@ -158,7 +158,11 @@ static int32_t mgmtVgroupActionUpdate(SSdbOper *pOper) { } mgmtDecVgroupRef(pVgroup); - mTrace("vgId:%d, is updated, tables:%d numOfVnode:%d", pVgroup->vgId, pDb->cfg.maxTables, pVgroup->numOfVnodes); + + mTrace("vgId:%d, is updated, numOfVnode:%d", pVgroup->vgId, pVgroup->numOfVnodes); + if (pDb) { + mTrace("tables:%d", pDb->cfg.maxTables); + } return TSDB_CODE_SUCCESS; } diff --git a/src/query/src/qparserImpl.c b/src/query/src/qparserImpl.c index 075dbc9d14..7b3a76c9d2 100644 --- a/src/query/src/qparserImpl.c +++ b/src/query/src/qparserImpl.c @@ -675,7 +675,7 @@ void SQLInfoDestroy(SSqlInfo *pInfo) { free(pInfo->pDCLInfo->a); } - if (pInfo->type == TSDB_SQL_CREATE_DB) { + if (pInfo->pDCLInfo != NULL && pInfo->type == TSDB_SQL_CREATE_DB) { tVariantListDestroy(pInfo->pDCLInfo->dbOpt.keep); } @@ -899,4 +899,4 @@ void setDefaultCreateDbOption(SCreateDBInfo *pDBInfo) { pDBInfo->keep = NULL; memset(&pDBInfo->precision, 0, sizeof(SSQLToken)); -} \ No newline at end of file +} diff --git a/src/query/src/qtsbuf.c b/src/query/src/qtsbuf.c index 062a8038b2..1d5c4f2d9d 100644 --- a/src/query/src/qtsbuf.c +++ b/src/query/src/qtsbuf.c @@ -636,12 +636,16 @@ void tsBufResetPos(STSBuf* pTSBuf) { STSElem tsBufGetElem(STSBuf* pTSBuf) { STSElem elem1 = {.vnode = -1}; - STSCursor* pCur = &pTSBuf->cur; - if (pTSBuf == NULL || pCur->vnodeIndex < 0) { + if (pTSBuf == NULL) { return elem1; } + STSCursor* pCur = &pTSBuf->cur; + if (pCur != NULL && pCur->vnodeIndex < 0) { + return elem1; + } + STSBlock* pBlock = &pTSBuf->block; elem1.vnode = pTSBuf->pData[pCur->vnodeIndex].info.vnode; @@ -920,4 +924,4 @@ static STSBuf* allocResForTSBuf(STSBuf* pTSBuf) { pTSBuf->fileSize += getDataStartOffset(); return pTSBuf; -} \ No newline at end of file +} diff --git a/src/query/src/queryUtil.c b/src/query/src/queryUtil.c index b4d8911284..9da02f9f0f 100644 --- a/src/query/src/queryUtil.c +++ b/src/query/src/queryUtil.c @@ -62,7 +62,10 @@ void destroyTimeWindowRes(SWindowResult *pWindowRes, int32_t nOutputCols) { } void cleanupTimeWindowInfo(SWindowResInfo *pWindowResInfo, int32_t numOfCols) { - if (pWindowResInfo == NULL || pWindowResInfo->capacity == 0) { + if (pWindowResInfo == NULL) { + return; + } + if (pWindowResInfo->capacity == 0) { assert(pWindowResInfo->hashList == NULL && pWindowResInfo->pResult == NULL); return; } diff --git a/src/tsdb/src/tsdbRead.c b/src/tsdb/src/tsdbRead.c index bc9220dbc7..46480b2b9d 100644 --- a/src/tsdb/src/tsdbRead.c +++ b/src/tsdb/src/tsdbRead.c @@ -216,7 +216,7 @@ static bool hasMoreDataInCache(STsdbQueryHandle* pHandle) { return false; } - if (pCheckInfo->iter == NULL) { + if (pCheckInfo->iter == NULL && pTable->mem) { pCheckInfo->iter = tSkipListCreateIterFromVal(pTable->mem->pData, (const char*) &pCheckInfo->lastKey, TSDB_DATA_TYPE_TIMESTAMP, pHandle->order); -- GitLab