From 91927e84a5ae62a8dff4d9dba902fca930dd67b1 Mon Sep 17 00:00:00 2001 From: wangjiaming0909 <604227650@qq.com> Date: Wed, 18 Sep 2024 14:27:51 +0800 Subject: [PATCH] ret check for memory allocation --- include/libs/nodes/nodes.h | 1 + source/dnode/mnode/impl/src/mndSma.c | 58 +++++++++++++------------ source/libs/nodes/src/nodesCloneFuncs.c | 9 ++++ source/libs/nodes/src/nodesCodeFuncs.c | 2 + source/libs/nodes/src/nodesUtilFuncs.c | 2 +- source/libs/parser/inc/parInsertUtil.h | 2 +- source/libs/parser/src/parAstCreater.c | 2 + source/libs/parser/src/parInsertSml.c | 12 ++++- source/libs/parser/src/parInsertSql.c | 9 ++-- source/libs/parser/src/parInsertStmt.c | 4 +- source/libs/parser/src/parInsertUtil.c | 13 ++++-- source/libs/parser/src/parTranslater.c | 39 ++++++++++++++++- source/libs/parser/src/parUtil.c | 35 ++++++++++++--- 13 files changed, 139 insertions(+), 49 deletions(-) diff --git a/include/libs/nodes/nodes.h b/include/libs/nodes/nodes.h index 24a062bfa3..72dd3ef3e0 100644 --- a/include/libs/nodes/nodes.h +++ b/include/libs/nodes/nodes.h @@ -173,6 +173,7 @@ int32_t nodesNodeToSQL(SNode* pNode, char* buf, int32_t bufSize, int32_t* len); char* nodesGetNameFromColumnNode(SNode* pNode); int32_t nodesGetOutputNumFromSlotList(SNodeList* pSlots); void nodesSortList(SNodeList** pList, int32_t (*)(SNode* pNode1, SNode* pNode2)); +void destroyFuncParam(void* pFuncStruct); #ifdef __cplusplus } diff --git a/source/dnode/mnode/impl/src/mndSma.c b/source/dnode/mnode/impl/src/mndSma.c index 19c9339af4..1a76ab2a8b 100644 --- a/source/dnode/mnode/impl/src/mndSma.c +++ b/source/dnode/mnode/impl/src/mndSma.c @@ -651,6 +651,9 @@ static int32_t mndCreateSma(SMnode *pMnode, SRpcMsg *pReq, SMCreateSmaReq *pCrea streamObj.targetDbUid = pDb->uid; streamObj.version = 1; streamObj.sql = taosStrdup(pCreate->sql); + if (!streamObj.sql) { + return terrno; + } streamObj.smaId = smaObj.uid; streamObj.conf.watermark = pCreate->watermark; streamObj.deleteMark = pCreate->deleteMark; @@ -658,6 +661,10 @@ static int32_t mndCreateSma(SMnode *pMnode, SRpcMsg *pReq, SMCreateSmaReq *pCrea streamObj.conf.trigger = STREAM_TRIGGER_WINDOW_CLOSE; streamObj.conf.triggerParam = pCreate->maxDelay; streamObj.ast = taosStrdup(smaObj.ast); + if (!streamObj.ast) { + taosMemoryFree(streamObj.sql); + return terrno; + } streamObj.indexForMultiAggBalance = -1; // check the maxDelay @@ -1509,6 +1516,9 @@ static int32_t mndRetrieveIdx(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBloc if (pShow->pIter == NULL) { pShow->pIter = taosMemoryCalloc(1, sizeof(SSmaAndTagIter)); } + if (!pShow->pIter) { + return terrno; + } int32_t read = mndRetrieveSma(pReq, pShow, pBlock, rows); if (read < rows) { read += mndRetrieveTagIdx(pReq, pShow, pBlock, rows - read); @@ -1555,27 +1565,6 @@ static void initSMAObj(SCreateTSMACxt* pCxt) { pCxt->pSma->ast = pCxt->pCreateSmaReq->ast; } -static void initStreamObj(SStreamObj *pStream, const char *streamName, const SMCreateSmaReq *pCreateReq, - const SDbObj *pDb, SSmaObj *pSma) { - tstrncpy(pStream->name, streamName, TSDB_STREAM_FNAME_LEN); - tstrncpy(pStream->sourceDb, pDb->name, TSDB_DB_FNAME_LEN); - tstrncpy(pStream->targetDb, pDb->name, TSDB_DB_FNAME_LEN); - pStream->createTime = taosGetTimestampMs(); - pStream->updateTime = pStream->createTime; - pStream->uid = mndGenerateUid(streamName, strlen(streamName)); - pStream->sourceDbUid = pDb->uid; - pStream->targetDbUid = pDb->uid; - pStream->version = 1; - pStream->sql = taosStrdup(pCreateReq->sql); - pStream->smaId = pSma->uid; - pStream->conf.watermark = 0; - pStream->deleteMark = 0; - pStream->conf.fillHistory = STREAM_FILL_HISTORY_ON; - pStream->conf.trigger = STREAM_TRIGGER_WINDOW_CLOSE; - pStream->conf.triggerParam = 10000; - pStream->ast = taosStrdup(pSma->ast); -} - static int32_t mndCreateTSMABuildCreateStreamReq(SCreateTSMACxt *pCxt) { tstrncpy(pCxt->pCreateStreamReq->name, pCxt->streamName, TSDB_STREAM_FNAME_LEN); tstrncpy(pCxt->pCreateStreamReq->sourceDB, pCxt->pDb->name, TSDB_DB_FNAME_LEN); @@ -1653,11 +1642,15 @@ static int32_t mndCreateTSMABuildCreateStreamReq(SCreateTSMACxt *pCxt) { return code; } -static void mndCreateTSMABuildDropStreamReq(SCreateTSMACxt* pCxt) { +static int32_t mndCreateTSMABuildDropStreamReq(SCreateTSMACxt* pCxt) { tstrncpy(pCxt->pDropStreamReq->name, pCxt->streamName, TSDB_STREAM_FNAME_LEN); pCxt->pDropStreamReq->igNotExists = false; pCxt->pDropStreamReq->sql = taosStrdup(pCxt->pDropSmaReq->name); + if (!pCxt->pDropStreamReq->sql) { + return terrno; + } pCxt->pDropStreamReq->sqlLen = strlen(pCxt->pDropStreamReq->sql); + return TSDB_CODE_SUCCESS; } static int32_t mndSetUpdateDbTsmaVersionPrepareLogs(SMnode *pMnode, STrans *pTrans, SDbObj *pOld, SDbObj *pNew) { @@ -1815,7 +1808,10 @@ static int32_t mndCreateTSMA(SCreateTSMACxt *pCxt) { if (TSDB_CODE_SUCCESS != code) { goto _OVER; } - mndCreateTSMABuildDropStreamReq(pCxt); + code = mndCreateTSMABuildDropStreamReq(pCxt); + if (TSDB_CODE_SUCCESS != code) { + goto _OVER; + } if (TSDB_CODE_SUCCESS != (code = mndCreateTSMATxnPrepare(pCxt))) { goto _OVER; @@ -1985,7 +1981,10 @@ static int32_t mndDropTSMA(SCreateTSMACxt* pCxt) { } SMDropStreamReq dropStreamReq = {0}; pCxt->pDropStreamReq = &dropStreamReq; - mndCreateTSMABuildDropStreamReq(pCxt); + code = mndCreateTSMABuildDropStreamReq(pCxt); + if (TSDB_CODE_SUCCESS != code) { + goto _OVER; + } mndTransSetDbName(pTrans, pCxt->pDb->name, NULL); if (mndTransCheckConflict(pCxt->pMnode, pTrans) != 0) goto _OVER; mndTransSetSerial(pTrans); @@ -2141,12 +2140,15 @@ static int32_t mndRetrieveTSMA(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlo SMnode * pMnode = pReq->info.node; int32_t code = 0; SColumnInfoData *pColInfo; - if (pShow->db[0]) { - pDb = mndAcquireDb(pMnode, pShow->db); - } if (pShow->pIter == NULL) { pShow->pIter = taosMemoryCalloc(1, sizeof(SSmaAndTagIter)); } + if (!pShow->pIter) { + return terrno; + } + if (pShow->db[0]) { + pDb = mndAcquireDb(pMnode, pShow->db); + } SSmaAndTagIter *pIter = pShow->pIter; while (numOfRows < rows) { pIter->pSmaIter = sdbFetch(pMnode->pSdb, SDB_SMA, pIter->pSmaIter, (void **)&pSma); @@ -2273,7 +2275,7 @@ static int32_t mndRetrieveTSMA(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlo mndReleaseDb(pMnode, pSrcDb); if (TSDB_CODE_SUCCESS != code) { sdbCancelFetch(pMnode->pSdb, pIter->pSmaIter); - numOfRows = -1; + numOfRows = code; break; } } diff --git a/source/libs/nodes/src/nodesCloneFuncs.c b/source/libs/nodes/src/nodesCloneFuncs.c index b76e4448d3..d9b72b36b7 100644 --- a/source/libs/nodes/src/nodesCloneFuncs.c +++ b/source/libs/nodes/src/nodesCloneFuncs.c @@ -268,6 +268,7 @@ static SArray* functParamClone(const SArray* pSrc) { if (NULL == pDst) { return NULL; } + int32_t code = 0; for (int i = 0; i < TARRAY_SIZE(pSrc); ++i) { SFunctParam* pFunctParam = taosArrayGet(pSrc, i); SFunctParam* pNewFunctParam = (SFunctParam*)taosArrayPush(pDst, pFunctParam); @@ -277,8 +278,16 @@ static SArray* functParamClone(const SArray* pSrc) { } pNewFunctParam->type = pFunctParam->type; pNewFunctParam->pCol = taosMemoryCalloc(1, sizeof(SColumn)); + if (!pNewFunctParam->pCol) { + code = terrno; + break; + } memcpy(pNewFunctParam->pCol, pFunctParam->pCol, sizeof(SColumn)); } + if (TSDB_CODE_SUCCESS != code) { + taosArrayDestroyEx(pDst, destroyFuncParam); + return NULL; + } return pDst; } diff --git a/source/libs/nodes/src/nodesCodeFuncs.c b/source/libs/nodes/src/nodesCodeFuncs.c index 74879a53af..99d7c4e265 100644 --- a/source/libs/nodes/src/nodesCodeFuncs.c +++ b/source/libs/nodes/src/nodesCodeFuncs.c @@ -3954,6 +3954,7 @@ static int32_t datumToJson(const void* pObj, SJson* pJson) { 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)); + if (!buf) return terrno; code = taosHexEncode(varDataVal(pNode->datum.p), buf, varDataLen(pNode->datum.p)); if (code != TSDB_CODE_SUCCESS) { taosMemoryFree(buf); @@ -3971,6 +3972,7 @@ static int32_t datumToJson(const void* pObj, SJson* pJson) { case TSDB_DATA_TYPE_JSON: { int32_t len = getJsonValueLen(pNode->datum.p); char* buf = taosMemoryCalloc(len * 2 + 1, sizeof(char)); + if (!buf) return terrno; code = taosHexEncode(pNode->datum.p, buf, len); if (code != TSDB_CODE_SUCCESS) { taosMemoryFree(buf); diff --git a/source/libs/nodes/src/nodesUtilFuncs.c b/source/libs/nodes/src/nodesUtilFuncs.c index 9f831c05e9..22acf4df45 100644 --- a/source/libs/nodes/src/nodesUtilFuncs.c +++ b/source/libs/nodes/src/nodesUtilFuncs.c @@ -824,7 +824,7 @@ static void destroyTableCfg(STableCfg* pCfg) { static void destroySmaIndex(void* pIndex) { taosMemoryFree(((STableIndexInfo*)pIndex)->expr); } -static void destroyFuncParam(void* pValue) { taosMemoryFree(((SFunctParam*)pValue)->pCol); } +void destroyFuncParam(void* pValue) { taosMemoryFree(((SFunctParam*)pValue)->pCol); } static void destroyHintValue(EHintOption option, void* value) { switch (option) { diff --git a/source/libs/parser/inc/parInsertUtil.h b/source/libs/parser/inc/parInsertUtil.h index df9f698dc1..c5cce55d9a 100644 --- a/source/libs/parser/inc/parInsertUtil.h +++ b/source/libs/parser/inc/parInsertUtil.h @@ -42,7 +42,7 @@ typedef struct SVgroupDataCxt { int32_t insCreateSName(SName *pName, struct SToken *pTableName, int32_t acctId, const char *dbName, SMsgBuf *pMsgBuf); int16_t insFindCol(struct SToken *pColname, int16_t start, int16_t end, SSchema *pSchema); -void insBuildCreateTbReq(SVCreateTbReq *pTbReq, const char *tname, STag *pTag, int64_t suid, const char *sname, +int32_t insBuildCreateTbReq(SVCreateTbReq *pTbReq, const char *tname, STag *pTag, int64_t suid, const char *sname, SArray *tagName, uint8_t tagNum, int32_t ttl); int32_t insInitBoundColsInfo(int32_t numOfBound, SBoundColInfo *pInfo); void insResetBoundColsInfo(SBoundColInfo *pInfo); diff --git a/source/libs/parser/src/parAstCreater.c b/source/libs/parser/src/parAstCreater.c index fa6a3648e0..77d50ffd91 100644 --- a/source/libs/parser/src/parAstCreater.c +++ b/source/libs/parser/src/parAstCreater.c @@ -580,6 +580,7 @@ SNodeList* createHintNodeList(SAstCreateContext* pCxt, const SToken* pLiteral) { } SNodeList* pHintList = NULL; char* hint = strndup(pLiteral->z + 3, pLiteral->n - 5); + if (!hint) return NULL; int32_t i = 0; bool quit = false; bool inParamList = false; @@ -2775,6 +2776,7 @@ _err: static int32_t getIpV4RangeFromWhitelistItem(char* ipRange, SIpV4Range* pIpRange) { int32_t code = TSDB_CODE_SUCCESS; char* ipCopy = taosStrdup(ipRange); + if (!ipCopy) return terrno; char* slash = strchr(ipCopy, '/'); if (slash) { *slash = '\0'; diff --git a/source/libs/parser/src/parInsertSml.c b/source/libs/parser/src/parInsertSml.c index 929ec01f6a..6bfab0c9ce 100644 --- a/source/libs/parser/src/parInsertSml.c +++ b/source/libs/parser/src/parInsertSml.c @@ -274,6 +274,11 @@ int32_t smlBuildCol(STableDataCxt* pTableCxt, SSchema* schema, void* data, int32 } else if (kv->type == TSDB_DATA_TYPE_GEOMETRY || kv->type == TSDB_DATA_TYPE_VARBINARY) { pVal->value.nData = kv->length; pVal->value.pData = taosMemoryMalloc(kv->length); + if (!pVal->value.pData) { + ret = terrno; + uError("SML smlBuildCol malloc failed %s:%d, err: %s", __func__, __LINE__, tstrerror(ret)); + goto end; + } (void)memcpy(pVal->value.pData, (uint8_t*)kv->value, kv->length); } else { (void)memcpy(&pVal->value.val, &(kv->value), kv->length); @@ -318,8 +323,11 @@ int32_t smlBindData(SQuery* query, bool dataFormat, SArray* tags, SArray* colsSc ret = terrno; goto end; } - insBuildCreateTbReq(pCreateTblReq, tableName, pTag, pTableMeta->suid, NULL, tagName, pTableMeta->tableInfo.numOfTags, - ttl); + ret = insBuildCreateTbReq(pCreateTblReq, tableName, pTag, pTableMeta->suid, NULL, tagName, + pTableMeta->tableInfo.numOfTags, ttl); + if (TSDB_CODE_SUCCESS != ret) { + goto end; + } pCreateTblReq->ctb.stbName = taosMemoryCalloc(1, sTableNameLen + 1); if (pCreateTblReq->ctb.stbName == NULL){ diff --git a/source/libs/parser/src/parInsertSql.c b/source/libs/parser/src/parInsertSql.c index d15d305c95..a7e842f486 100644 --- a/source/libs/parser/src/parInsertSql.c +++ b/source/libs/parser/src/parInsertSql.c @@ -465,6 +465,7 @@ static int32_t parseVarbinary(SToken* pToken, uint8_t** pData, uint32_t* nData, *nData = size; } else { *pData = taosMemoryCalloc(1, pToken->n); + if (!pData) return terrno; int32_t len = trimString(pToken->z, pToken->n, *pData, pToken->n); *nData = len; @@ -639,6 +640,9 @@ static int32_t parseTagToken(const char** end, SToken* pToken, SSchema* pSchema, return generateSyntaxErrMsg(pMsgBuf, TSDB_CODE_PAR_VALUE_TOO_LONG, pSchema->name); } val->pData = taosStrdup(pToken->z); + if (!val->pData) { + return terrno; + } val->nData = pToken->n; break; } @@ -770,10 +774,9 @@ static int32_t buildCreateTbReq(SVnodeModifyOpStmt* pStmt, STag* pTag, SArray* p if (NULL == pStmt->pCreateTblReq) { return terrno; } - insBuildCreateTbReq(pStmt->pCreateTblReq, pStmt->targetTableName.tname, pTag, pStmt->pTableMeta->suid, + return insBuildCreateTbReq(pStmt->pCreateTblReq, pStmt->targetTableName.tname, pTag, pStmt->pTableMeta->suid, pStmt->usingTableName.tname, pTagName, pStmt->pTableMeta->tableInfo.numOfTags, TSDB_DEFAULT_TABLE_TTL); - return TSDB_CODE_SUCCESS; } int32_t checkAndTrimValue(SToken* pToken, char* tmpTokenBuf, SMsgBuf* pMsgBuf, int8_t type) { @@ -1919,7 +1922,7 @@ static int32_t processCtbAutoCreationAndCtbMeta(SInsertParseContext* pCxt, SVnod code = terrno; } if (code == TSDB_CODE_SUCCESS) { - insBuildCreateTbReq(pStbRowsCxt->pCreateCtbReq, pStbRowsCxt->ctbName.tname, pStbRowsCxt->pTag, + code = insBuildCreateTbReq(pStbRowsCxt->pCreateCtbReq, pStbRowsCxt->ctbName.tname, pStbRowsCxt->pTag, pStbRowsCxt->pStbMeta->uid, pStbRowsCxt->stbName.tname, pStbRowsCxt->aTagNames, getNumOfTags(pStbRowsCxt->pStbMeta), TSDB_DEFAULT_TABLE_TTL); pStbRowsCxt->pTag = NULL; diff --git a/source/libs/parser/src/parInsertStmt.c b/source/libs/parser/src/parInsertStmt.c index 7d68e80df9..ec4a6ee0e7 100644 --- a/source/libs/parser/src/parInsertStmt.c +++ b/source/libs/parser/src/parInsertStmt.c @@ -237,7 +237,7 @@ int32_t qBindStmtTagsValue(void* pBlock, void* boundTags, int64_t suid, const ch } } - insBuildCreateTbReq(pDataBlock->pData->pCreateTbReq, tName, pTag, suid, sTableName, tagName, + code = insBuildCreateTbReq(pDataBlock->pData->pCreateTbReq, tName, pTag, suid, sTableName, tagName, pDataBlock->pMeta->tableInfo.numOfTags, TSDB_DEFAULT_TABLE_TTL); pTag = NULL; @@ -585,7 +585,7 @@ int32_t qBindStmtTagsValue2(void* pBlock, void* boundTags, int64_t suid, const c } } - insBuildCreateTbReq(pDataBlock->pData->pCreateTbReq, tName, pTag, suid, sTableName, tagName, + code = insBuildCreateTbReq(pDataBlock->pData->pCreateTbReq, tName, pTag, suid, sTableName, tagName, pDataBlock->pMeta->tableInfo.numOfTags, TSDB_DEFAULT_TABLE_TTL); pTag = NULL; diff --git a/source/libs/parser/src/parInsertUtil.c b/source/libs/parser/src/parInsertUtil.c index e77678fb69..b3bfa3982c 100644 --- a/source/libs/parser/src/parInsertUtil.c +++ b/source/libs/parser/src/parInsertUtil.c @@ -146,19 +146,24 @@ int16_t insFindCol(SToken* pColname, int16_t start, int16_t end, SSchema* pSchem return -1; } -void insBuildCreateTbReq(SVCreateTbReq* pTbReq, const char* tname, STag* pTag, int64_t suid, const char* sname, +int32_t insBuildCreateTbReq(SVCreateTbReq* pTbReq, const char* tname, STag* pTag, int64_t suid, const char* sname, SArray* tagName, uint8_t tagNum, int32_t ttl) { pTbReq->type = TD_CHILD_TABLE; + pTbReq->ctb.pTag = (uint8_t*)pTag; pTbReq->name = taosStrdup(tname); + if (!pTbReq->name) return terrno; pTbReq->ctb.suid = suid; pTbReq->ctb.tagNum = tagNum; - if (sname) pTbReq->ctb.stbName = taosStrdup(sname); - pTbReq->ctb.pTag = (uint8_t*)pTag; + if (sname) { + pTbReq->ctb.stbName = taosStrdup(sname); + if (!pTbReq->ctb.stbName) return terrno; + } pTbReq->ctb.tagName = taosArrayDup(tagName, NULL); + if (!pTbReq->ctb.tagName) return terrno; pTbReq->ttl = ttl; pTbReq->commentLen = -1; - return; + return TSDB_CODE_SUCCESS; } static void initBoundCols(int32_t ncols, int16_t* pBoundCols) { diff --git a/source/libs/parser/src/parTranslater.c b/source/libs/parser/src/parTranslater.c index 22d0fe846e..473b4f4b1a 100755 --- a/source/libs/parser/src/parTranslater.c +++ b/source/libs/parser/src/parTranslater.c @@ -2733,6 +2733,9 @@ static int32_t rewriteServerStatusFunc(STranslateContext* pCxt, SNode** pNode) { return TSDB_CODE_RPC_NETWORK_UNAVAIL; } char* pStatus = taosStrdup((void*)"1"); + if (!pStatus) { + return terrno; + } int32_t code = rewriteFuncToValue(pCxt, &pStatus, pNode); if (TSDB_CODE_SUCCESS != code) taosMemoryFree(pStatus); return code; @@ -4122,12 +4125,18 @@ static EDealRes doTranslateTbName(SNode** pNode, void* pContext) { pVal->literal = taosStrdup(pCxt->pTbName); if (NULL == pVal->literal) { pCxt->errCode = TSDB_CODE_OUT_OF_MEMORY; + nodesDestroyNode((SNode*)pVal); return DEAL_RES_ERROR; } pVal->translate = true; pVal->node.resType.type = TSDB_DATA_TYPE_BINARY; pVal->node.resType.bytes = tbLen + VARSTR_HEADER_SIZE; pVal->datum.p = taosMemoryCalloc(1, tbLen + VARSTR_HEADER_SIZE + 1); + if (!pVal->datum.p) { + pCxt->errCode = terrno; + nodesDestroyNode((SNode*)pVal); + return DEAL_RES_ERROR; + } varDataSetLen(pVal->datum.p, tbLen); strncpy(varDataVal(pVal->datum.p), pVal->literal, tbLen); strcpy(pVal->node.userAlias, pFunc->node.userAlias); @@ -5832,6 +5841,11 @@ static int32_t createDefaultEveryNode(STranslateContext* pCxt, SNode** pOutput) pEvery->node.resType.bytes = tDataTypes[TSDB_DATA_TYPE_BIGINT].bytes; pEvery->flag |= VALUE_FLAG_IS_DURATION; pEvery->literal = taosStrdup("1s"); + if (!pEvery->literal) { + code = terrno; + nodesDestroyNode((SNode*)pEvery); + return code; + } *pOutput = (SNode*)pEvery; return TSDB_CODE_SUCCESS; @@ -6333,6 +6347,10 @@ static int32_t setEqualTbnameTableVgroups(STranslateContext* pCxt, SSelectStmt* nTbls = taosArrayGetSize(pInfo->aTbnames); SVgroupsInfo* vgsInfo = taosMemoryMalloc(sizeof(SVgroupsInfo) + nTbls * sizeof(SVgroupInfo)); + if (!vgsInfo) { + code = terrno; + break; + } findVgroupsFromEqualTbname(pCxt, pInfo->aTbnames, pInfo->pRealTable->table.dbName, numOfVgs, vgsInfo); if (vgsInfo->numOfVgroups != 0) { taosMemoryFree(pInfo->pRealTable->pVgroupList); @@ -6360,6 +6378,7 @@ static int32_t setEqualTbnameTableVgroups(STranslateContext* pCxt, SSelectStmt* break; } if (NULL == taosArrayPush(pTbNames, &pNewTbName)) { + taosMemoryFreeClear(pNewTbName); code = terrno; break; } @@ -6374,15 +6393,19 @@ static int32_t setEqualTbnameTableVgroups(STranslateContext* pCxt, SSelectStmt* findVgroupsFromEqualTbname(pCxt, pTbNames, pInfo->pRealTable->table.dbName, numOfVgs, vgsInfo); if (vgsInfo->numOfVgroups != 0) { if (NULL == taosArrayPush(pInfo->pRealTable->tsmaTargetTbVgInfo, &vgsInfo)) { + taosMemoryFreeClear(vgsInfo); code = terrno; } } else { - taosMemoryFree(vgsInfo); + taosMemoryFreeClear(vgsInfo); } } taosArrayDestroyP(pTbNames, taosMemoryFree); if (code) break; } + if (TSDB_CODE_SUCCESS != code) { + break; + } } } @@ -9216,6 +9239,9 @@ static int32_t translateCreateUser(STranslateContext* pCxt, SCreateUserStmt* pSt createReq.numIpRanges = pStmt->numIpRanges; if (pStmt->numIpRanges > 0) { createReq.pIpRanges = taosMemoryMalloc(createReq.numIpRanges * sizeof(SIpV4Range)); + if (!createReq.pIpRanges) { + return terrno; + } memcpy(createReq.pIpRanges, pStmt->pIpRanges, sizeof(SIpV4Range) * createReq.numIpRanges); } code = buildCmdMsg(pCxt, TDMT_MND_CREATE_USER, (FSerializeFunc)tSerializeSCreateUserReq, &createReq); @@ -9259,6 +9285,9 @@ static int32_t translateAlterUser(STranslateContext* pCxt, SAlterUserStmt* pStmt alterReq.numIpRanges = pStmt->numIpRanges; if (pStmt->numIpRanges > 0) { alterReq.pIpRanges = taosMemoryMalloc(alterReq.numIpRanges * sizeof(SIpV4Range)); + if (!alterReq.pIpRanges) { + return terrno; + } memcpy(alterReq.pIpRanges, pStmt->pIpRanges, sizeof(SIpV4Range) * alterReq.numIpRanges); } code = buildCmdMsg(pCxt, TDMT_MND_ALTER_USER, (FSerializeFunc)tSerializeSAlterUserReq, &alterReq); @@ -13282,6 +13311,10 @@ static int32_t buildNormalTableBatchReq(int32_t acctId, const SCreateTableStmt* SVCreateTbReq req = {0}; req.type = TD_NORMAL_TABLE; req.name = taosStrdup(pStmt->tableName); + if (!req.name) { + tdDestroySVCreateTbReq(&req); + return terrno; + } req.ttl = pStmt->pOptions->ttl; if (pStmt->pOptions->commentNull == false) { req.comment = taosStrdup(pStmt->pOptions->comment); @@ -14001,6 +14034,7 @@ static int32_t constructParseFileContext(SCreateSubTableFromFileClause* pStmt, S int32_t code = TSDB_CODE_SUCCESS; SParseFileContext* pParFileCxt = taosMemoryCalloc(1, sizeof(SParseFileContext)); + if (!pParFileCxt) return terrno; pParFileCxt->pStbMeta = pSuperTableMeta; pParFileCxt->tagNameFilled = false; pParFileCxt->pTag = NULL; @@ -14265,6 +14299,9 @@ int32_t continueCreateTbFromFile(SParseContext* pParseCxt, SQuery** pQuery) { SMsgBuf tmpBuf = {0}; tmpBuf.buf = taosMemoryMalloc(1024); + if (!tmpBuf.buf) { + return terrno; + } int32_t code = createSubTableFromFile(&tmpBuf, pParseCxt, pModifyStmt); if (TSDB_CODE_SUCCESS != code) goto _OUT; diff --git a/source/libs/parser/src/parUtil.c b/source/libs/parser/src/parUtil.c index 94bd53ea68..d17b8ac6c2 100644 --- a/source/libs/parser/src/parUtil.c +++ b/source/libs/parser/src/parUtil.c @@ -1294,35 +1294,56 @@ int32_t getTsmaFromCache(SParseMetaCache* pMetaCache, const SName* pTsmaName, ST STableCfg* tableCfgDup(STableCfg* pCfg) { STableCfg* pNew = taosMemoryMalloc(sizeof(*pNew)); - + if (!pNew) { + return NULL; + } memcpy(pNew, pCfg, sizeof(*pNew)); - if (NULL != pNew->pComment) { + pNew->pComment = NULL; + pNew->pFuncs = NULL; + pNew->pTags = NULL; + pNew->pSchemas = NULL; + pNew->pSchemaExt = NULL; + if (NULL != pCfg->pComment) { pNew->pComment = taosMemoryCalloc(pNew->commentLen + 1, 1); + if (!pNew->pComment) goto err; memcpy(pNew->pComment, pCfg->pComment, pNew->commentLen); } - if (NULL != pNew->pFuncs) { - pNew->pFuncs = taosArrayDup(pNew->pFuncs, NULL); + if (NULL != pCfg->pFuncs) { + pNew->pFuncs = taosArrayDup(pCfg->pFuncs, NULL); + if (!pNew->pFuncs) goto err; } - if (NULL != pNew->pTags) { - pNew->pTags = taosMemoryCalloc(pNew->tagsLen + 1, 1); + if (NULL != pCfg->pTags) { + pNew->pTags = taosMemoryCalloc(pCfg->tagsLen + 1, 1); + if (!pNew->pTags) goto err; memcpy(pNew->pTags, pCfg->pTags, pNew->tagsLen); } int32_t schemaSize = (pCfg->numOfColumns + pCfg->numOfTags) * sizeof(SSchema); SSchema* pSchema = taosMemoryMalloc(schemaSize); + if (!pSchema) goto err; memcpy(pSchema, pCfg->pSchemas, schemaSize); + pNew->pSchemas = pSchema; + SSchemaExt* pSchemaExt = NULL; if (useCompress(pCfg->tableType) && pCfg->pSchemaExt) { int32_t schemaExtSize = pCfg->numOfColumns * sizeof(SSchemaExt); pSchemaExt = taosMemoryMalloc(schemaExtSize); + if (!pSchemaExt) goto err; memcpy(pSchemaExt, pCfg->pSchemaExt, schemaExtSize); } - pNew->pSchemas = pSchema; pNew->pSchemaExt = pSchemaExt; return pNew; +err: + if (pNew->pComment) taosMemoryFreeClear(pNew->pComment); + if (pNew->pFuncs) taosArrayDestroy(pNew->pFuncs); + if (pNew->pTags) taosMemoryFreeClear(pNew->pTags); + if (pNew->pSchemas) taosMemoryFreeClear(pNew->pSchemas); + if (pNew->pSchemaExt) taosMemoryFreeClear(pNew->pSchemaExt); + taosMemoryFreeClear(pNew); + return NULL; } int32_t getTableCfgFromCache(SParseMetaCache* pMetaCache, const SName* pName, STableCfg** pOutput) {