From 6cb94f69f210482adb9891e696383e5f72ef2148 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Wed, 18 Sep 2024 15:22:09 +0800 Subject: [PATCH] enh: memory malloc return --- source/dnode/mgmt/mgmt_vnode/src/vmInt.c | 6 +++ source/dnode/vnode/src/meta/metaQuery.c | 49 +++++++----------------- source/dnode/vnode/src/meta/metaTable.c | 28 +++++++++++--- source/dnode/vnode/src/vnd/vnodeSvr.c | 4 ++ source/libs/tdb/src/db/tdbBtree.c | 16 ++++++-- source/libs/tdb/src/db/tdbPage.c | 4 ++ 6 files changed, 63 insertions(+), 44 deletions(-) diff --git a/source/dnode/mgmt/mgmt_vnode/src/vmInt.c b/source/dnode/mgmt/mgmt_vnode/src/vmInt.c index 488a4d3b99..dd921c615b 100644 --- a/source/dnode/mgmt/mgmt_vnode/src/vmInt.c +++ b/source/dnode/mgmt/mgmt_vnode/src/vmInt.c @@ -360,6 +360,12 @@ static int32_t vmOpenVnodes(SVnodeMgmt *pMgmt) { int32_t vnodesPerThread = numOfVnodes / threadNum + 1; SVnodeThread *threads = taosMemoryCalloc(threadNum, sizeof(SVnodeThread)); + if (threads == NULL) { + dError("failed to allocate memory for threads since %s", terrstr()); + taosMemoryFree(pCfgs); + return terrno; + } + for (int32_t t = 0; t < threadNum; ++t) { threads[t].threadIndex = t; threads[t].pMgmt = pMgmt; diff --git a/source/dnode/vnode/src/meta/metaQuery.c b/source/dnode/vnode/src/meta/metaQuery.c index acee24e494..c44a1d2342 100644 --- a/source/dnode/vnode/src/meta/metaQuery.c +++ b/source/dnode/vnode/src/meta/metaQuery.c @@ -98,7 +98,7 @@ int metaReaderGetTableEntryByUidCache(SMetaReader *pReader, tb_uid_t uid) { SMeta *pMeta = pReader->pMeta; SMetaInfo info; - int32_t code = metaGetInfo(pMeta, uid, &info, pReader); + int32_t code = metaGetInfo(pMeta, uid, &info, pReader); if (TSDB_CODE_SUCCESS != code) { return terrno = (TSDB_CODE_NOT_FOUND == code ? TSDB_CODE_PAR_TABLE_NOT_EXIST : code); } @@ -616,17 +616,17 @@ STSchema *metaGetTbTSchema(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock) { return pTSchema; } -int32_t metaGetTbTSchemaNotNull(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, STSchema** ppTSchema) { +int32_t metaGetTbTSchemaNotNull(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, STSchema **ppTSchema) { *ppTSchema = metaGetTbTSchema(pMeta, uid, sver, lock); - if(*ppTSchema == NULL) { + if (*ppTSchema == NULL) { return terrno; } return TSDB_CODE_SUCCESS; } -int32_t metaGetTbTSchemaMaybeNull(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, STSchema** ppTSchema) { +int32_t metaGetTbTSchemaMaybeNull(SMeta *pMeta, tb_uid_t uid, int32_t sver, int lock, STSchema **ppTSchema) { *ppTSchema = metaGetTbTSchema(pMeta, uid, sver, lock); - if(*ppTSchema == NULL && terrno == TSDB_CODE_OUT_OF_MEMORY) { + if (*ppTSchema == NULL && terrno == TSDB_CODE_OUT_OF_MEMORY) { return terrno; } return TSDB_CODE_SUCCESS; @@ -1036,36 +1036,6 @@ const void *metaGetTableTagVal(const void *pTag, int16_t type, STagVal *val) { return NULL; } -#ifdef TAG_FILTER_DEBUG - if (IS_VAR_DATA_TYPE(val->type)) { - char *buf = taosMemoryCalloc(val->nData + 1, 1); - memcpy(buf, val->pData, val->nData); - metaDebug("metaTag table val varchar index:%d cid:%d type:%d value:%s", 1, val->cid, val->type, buf); - taosMemoryFree(buf); - } else { - double dval = 0; - GET_TYPED_DATA(dval, double, val->type, &val->i64); - metaDebug("metaTag table val number index:%d cid:%d type:%d value:%f", 1, val->cid, val->type, dval); - } - - SArray *pTagVals = NULL; - tTagToValArray((STag *)pTag, &pTagVals); - for (int i = 0; i < taosArrayGetSize(pTagVals); i++) { - STagVal *pTagVal = (STagVal *)taosArrayGet(pTagVals, i); - - if (IS_VAR_DATA_TYPE(pTagVal->type)) { - char *buf = taosMemoryCalloc(pTagVal->nData + 1, 1); - memcpy(buf, pTagVal->pData, pTagVal->nData); - metaDebug("metaTag table varchar index:%d cid:%d type:%d value:%s", i, pTagVal->cid, pTagVal->type, buf); - taosMemoryFree(buf); - } else { - double dval = 0; - GET_TYPED_DATA(dval, double, pTagVal->type, &pTagVal->i64); - metaDebug("metaTag table number index:%d cid:%d type:%d value:%f", i, pTagVal->cid, pTagVal->type, dval); - } - } -#endif - return val; } @@ -1088,6 +1058,9 @@ int32_t metaFilterCreateTime(void *pVnode, SMetaFltParam *arg, SArray *pUids) { SIdxCursor *pCursor = NULL; pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor)); + if (pCursor == NULL) { + return terrno; + } pCursor->pMeta = pMeta; pCursor->suid = param->suid; pCursor->cid = param->cid; @@ -1160,6 +1133,9 @@ int32_t metaFilterTableName(void *pVnode, SMetaFltParam *arg, SArray *pUids) { SIdxCursor *pCursor = NULL; pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor)); + if (pCursor == NULL) { + return terrno; + } pCursor->pMeta = pMeta; pCursor->suid = param->suid; pCursor->cid = param->cid; @@ -1235,6 +1211,9 @@ int32_t metaFilterTtl(void *pVnode, SMetaFltParam *arg, SArray *pUids) { SIdxCursor *pCursor = NULL; pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor)); + if (pCursor == NULL) { + return terrno; + } pCursor->pMeta = pMeta; pCursor->suid = param->suid; pCursor->cid = param->cid; diff --git a/source/dnode/vnode/src/meta/metaTable.c b/source/dnode/vnode/src/meta/metaTable.c index b568b03c2c..d70c48af64 100644 --- a/source/dnode/vnode/src/meta/metaTable.c +++ b/source/dnode/vnode/src/meta/metaTable.c @@ -89,12 +89,15 @@ static void metaGetEntryInfo(const SMetaEntry *pEntry, SMetaInfo *pInfo) { static int metaUpdateMetaRsp(tb_uid_t uid, char *tbName, SSchemaWrapper *pSchema, STableMetaRsp *pMetaRsp) { pMetaRsp->pSchemas = taosMemoryMalloc(pSchema->nCols * sizeof(SSchema)); - if (NULL == pMetaRsp->pSchemas) { - return terrno = TSDB_CODE_OUT_OF_MEMORY; + return terrno; } pMetaRsp->pSchemaExt = taosMemoryMalloc(pSchema->nCols * sizeof(SSchemaExt)); + if (pMetaRsp->pSchemaExt == NULL) { + taosMemoryFree(pMetaRsp->pSchemas); + return terrno; + } tstrncpy(pMetaRsp->tbName, tbName, TSDB_TABLE_NAME_LEN); pMetaRsp->numOfColumns = pSchema->nCols; @@ -1558,7 +1561,11 @@ static int metaAlterTableColumn(SMeta *pMeta, int64_t version, SVAlterTbReq *pAl // get table entry SDecoder dc = {0}; - entry.pBuf = taosMemoryMalloc(nData); + if ((entry.pBuf = taosMemoryMalloc(nData)) == NULL) { + (void)tdbTbcClose(pUidIdxc); + (void)tdbTbcClose(pTbDbc); + return terrno; + } memcpy(entry.pBuf, pData, nData); tDecoderInit(&dc, entry.pBuf, nData); ret = metaDecodeEntry(&dc, &entry); @@ -1625,6 +1632,9 @@ static int metaAlterTableColumn(SMeta *pMeta, int64_t version, SVAlterTbReq *pAl pSchema->version++; pSchema->nCols++; pNewSchema = taosMemoryMalloc(sizeof(SSchema) * pSchema->nCols); + if (pNewSchema == NULL) { + goto _err; + } memcpy(pNewSchema, pSchema->pSchema, sizeof(SSchema) * (pSchema->nCols - 1)); pSchema->pSchema = pNewSchema; pSchema->pSchema[entry.ntbEntry.schemaRow.nCols - 1].bytes = pAlterTbReq->bytes; @@ -1832,7 +1842,11 @@ static int metaUpdateTableTagVal(SMeta *pMeta, int64_t version, SVAlterTbReq *pA (void)tdbTbcGet(pTbDbc, NULL, NULL, &pData, &nData); - ctbEntry.pBuf = taosMemoryMalloc(nData); + if ((ctbEntry.pBuf = taosMemoryMalloc(nData)) == NULL) { + (void)tdbTbcClose(pUidIdxc); + (void)tdbTbcClose(pTbDbc); + return terrno; + } memcpy(ctbEntry.pBuf, pData, nData); tDecoderInit(&dc1, ctbEntry.pBuf, nData); (void)metaDecodeEntry(&dc1, &ctbEntry); @@ -2019,7 +2033,11 @@ static int metaUpdateTableOptions(SMeta *pMeta, int64_t version, SVAlterTbReq *p // get table entry SDecoder dc = {0}; - entry.pBuf = taosMemoryMalloc(nData); + if ((entry.pBuf = taosMemoryMalloc(nData)) == NULL) { + (void)tdbTbcClose(pUidIdxc); + (void)tdbTbcClose(pTbDbc); + return terrno; + } memcpy(entry.pBuf, pData, nData); tDecoderInit(&dc, entry.pBuf, nData); ret = metaDecodeEntry(&dc, &entry); diff --git a/source/dnode/vnode/src/vnd/vnodeSvr.c b/source/dnode/vnode/src/vnd/vnodeSvr.c index 2604e2262f..957b328807 100644 --- a/source/dnode/vnode/src/vnd/vnodeSvr.c +++ b/source/dnode/vnode/src/vnd/vnodeSvr.c @@ -1412,6 +1412,10 @@ static int32_t vnodeProcessDropTbReq(SVnode *pVnode, int64_t ver, void *pReq, in if (tsEnableAuditCreateTable) { char *str = taosMemoryCalloc(1, TSDB_TABLE_FNAME_LEN); + if (str == NULL) { + pRsp->code = terrno; + goto _exit; + } strcpy(str, pDropTbReq->name); if (taosArrayPush(tbNames, &str) == NULL) { terrno = TSDB_CODE_OUT_OF_MEMORY; diff --git a/source/libs/tdb/src/db/tdbBtree.c b/source/libs/tdb/src/db/tdbBtree.c index ff40616d70..ea18d5f67b 100644 --- a/source/libs/tdb/src/db/tdbBtree.c +++ b/source/libs/tdb/src/db/tdbBtree.c @@ -597,7 +597,9 @@ static int tdbBtreeBalanceNonRoot(SBTree *pBt, SPage *pParent, int idx, TXN *pTx if (sIdx + i < TDB_PAGE_TOTAL_CELLS(pParent)) { pCell = tdbPageGetCell(pParent, sIdx + i); szDivCell[i] = tdbBtreeCellSize(pParent, pCell, 0, NULL, NULL); - pDivCell[i] = tdbOsMalloc(szDivCell[i]); + if ((pDivCell[i] = tdbOsMalloc(szDivCell[i])) == NULL) { + return terrno; + } memcpy(pDivCell[i], pCell, szDivCell[i]); } @@ -832,8 +834,11 @@ static int tdbBtreeBalanceNonRoot(SBTree *pBt, SPage *pParent, int idx, TXN *pTx // TODO: pCell here may be inserted as an overflow cell, handle it SCell *pNewCell = tdbOsMalloc(cd.kLen + 9); - int szNewCell; - SPgno pgno; + if (pNewCell == NULL) { + return terrno; + } + int szNewCell; + SPgno pgno; pgno = TDB_PAGE_PGNO(pNews[iNew]); (void)tdbBtreeEncodeCell(pParent, cd.pKey, cd.kLen, (void *)&pgno, sizeof(SPgno), pNewCell, &szNewCell, pTxn, pBt); @@ -2201,7 +2206,10 @@ int tdbBtcDelete(SBTC *pBtc) { } // update the cell with new key - pCell = tdbOsMalloc(nKey + 9); + if ((pCell = tdbOsMalloc(nKey + 9)) == NULL) { + tdbError("tdb/btc-delete: malloc failed."); + return terrno; + } (void)tdbBtreeEncodeCell(pPage, pKey, nKey, &pgno, sizeof(pgno), pCell, &szCell, pBtc->pTxn, pBtc->pBt); ret = tdbPageUpdateCell(pPage, idx, pCell, szCell, pBtc->pTxn, pBtc->pBt); diff --git a/source/libs/tdb/src/db/tdbPage.c b/source/libs/tdb/src/db/tdbPage.c index eab8f6ef19..7a76c003b6 100644 --- a/source/libs/tdb/src/db/tdbPage.c +++ b/source/libs/tdb/src/db/tdbPage.c @@ -184,6 +184,10 @@ int tdbPageInsertCell(SPage *pPage, int idx, SCell *pCell, int szCell, u8 asOvfl // TODO: here has memory leak pNewCell = (SCell *)tdbOsMalloc(szCell); + if (pNewCell == NULL) { + tdbError("tdb/page-insert-cell: malloc failed."); + return terrno; + } memcpy(pNewCell, pCell, szCell); tdbTrace("tdbPage/insert/new ovfl cell: %p/%p", pNewCell, pPage);