From 4b46c4c1fce121369c8cf6aa6f9f852672a23e99 Mon Sep 17 00:00:00 2001 From: Jinqing Kuang Date: Thu, 19 Sep 2024 09:06:01 +0800 Subject: [PATCH] enh(query)[TD-32130]. Handle return values of memory allocation functions --- include/util/tpagedbuf.h | 2 +- source/dnode/vnode/src/tsdb/tsdbCacheRead.c | 8 +++++ source/dnode/vnode/src/tsdb/tsdbRead2.c | 6 +++- source/dnode/vnode/src/tsdb/tsdbReadUtil.c | 9 ++++- source/dnode/vnode/src/tsdb/tsdbReadUtil.h | 2 +- source/libs/executor/src/tlinearhash.c | 38 ++++++++++++++++----- source/util/src/tpagedbuf.c | 9 ++++- 7 files changed, 60 insertions(+), 14 deletions(-) diff --git a/include/util/tpagedbuf.h b/include/util/tpagedbuf.h index 7f3bd29694..71cee62d2e 100644 --- a/include/util/tpagedbuf.h +++ b/include/util/tpagedbuf.h @@ -149,7 +149,7 @@ void setBufPageDirty(void* pPage, bool dirty); * Set the compress/ no-compress flag for paged buffer, when flushing data in disk. * @param pBuf */ -void setBufPageCompressOnDisk(SDiskbasedBuf* pBuf, bool comp); +int32_t setBufPageCompressOnDisk(SDiskbasedBuf* pBuf, bool comp); /** * Set the pageId page buffer is not need diff --git a/source/dnode/vnode/src/tsdb/tsdbCacheRead.c b/source/dnode/vnode/src/tsdb/tsdbCacheRead.c index 72f2052867..82baa761c5 100644 --- a/source/dnode/vnode/src/tsdb/tsdbCacheRead.c +++ b/source/dnode/vnode/src/tsdb/tsdbCacheRead.c @@ -309,6 +309,10 @@ int32_t tsdbCacherowsReaderOpen(void* pVnode, int32_t type, void* pTableIdList, p->rowKey.pks[0].type = pPkCol->type; if (IS_VAR_DATA_TYPE(pPkCol->type)) { p->rowKey.pks[0].pData = taosMemoryCalloc(1, pPkCol->bytes); + if (p->rowKey.pks[0].pData == NULL) { + taosMemoryFree(p); + return terrno; + } } p->pkColumn = *pPkCol; @@ -345,6 +349,10 @@ int32_t tsdbCacherowsReaderOpen(void* pVnode, int32_t type, void* pTableIdList, } p->idstr = taosStrdup(idstr); + if (idstr != NULL && p->idstr == NULL) { + tsdbCacherowsReaderClose(p); + return terrno; + } code = taosThreadMutexInit(&p->readerMutex, NULL); if (code) { tsdbCacherowsReaderClose(p); diff --git a/source/dnode/vnode/src/tsdb/tsdbRead2.c b/source/dnode/vnode/src/tsdb/tsdbRead2.c index 731b733b52..991c7863ac 100644 --- a/source/dnode/vnode/src/tsdb/tsdbRead2.c +++ b/source/dnode/vnode/src/tsdb/tsdbRead2.c @@ -828,7 +828,11 @@ static int32_t loadFileBlockBrinInfo(STsdbReader* pReader, SArray* pIndexList, S } SFileDataBlockInfo blockInfo = {.tbBlockIdx = TARRAY_SIZE(pScanInfo->pBlockList)}; - recordToBlockInfo(&blockInfo, pRecord); + code = recordToBlockInfo(&blockInfo, pRecord); + if (code != TSDB_CODE_SUCCESS) { + clearBrinBlockIter(&iter); + return code; + } void* p1 = taosArrayPush(pScanInfo->pBlockList, &blockInfo); if (p1 == NULL) { clearBrinBlockIter(&iter); diff --git a/source/dnode/vnode/src/tsdb/tsdbReadUtil.c b/source/dnode/vnode/src/tsdb/tsdbReadUtil.c index db987d07d6..24a0dca5c8 100644 --- a/source/dnode/vnode/src/tsdb/tsdbReadUtil.c +++ b/source/dnode/vnode/src/tsdb/tsdbReadUtil.c @@ -559,7 +559,7 @@ static int32_t fileDataBlockOrderCompar(const void* pLeft, const void* pRight, v return pLeftBlock->offset > pRightBlock->offset ? 1 : -1; } -void recordToBlockInfo(SFileDataBlockInfo* pBlockInfo, SBrinRecord* record) { +int32_t recordToBlockInfo(SFileDataBlockInfo* pBlockInfo, SBrinRecord* record) { pBlockInfo->uid = record->uid; pBlockInfo->firstKey = record->firstKey.key.ts; pBlockInfo->lastKey = record->lastKey.key.ts; @@ -580,17 +580,24 @@ void recordToBlockInfo(SFileDataBlockInfo* pBlockInfo, SBrinRecord* record) { pBlockInfo->lastPk.val = record->lastKey.key.pks[0].val; } else { char* p = taosMemoryCalloc(1, pFirstKey->pks[0].nData + VARSTR_HEADER_SIZE); + if (p == NULL) { + return terrno; + } memcpy(varDataVal(p), pFirstKey->pks[0].pData, pFirstKey->pks[0].nData); varDataSetLen(p, pFirstKey->pks[0].nData); pBlockInfo->firstPk.pData = (uint8_t*)p; int32_t keyLen = record->lastKey.key.pks[0].nData; p = taosMemoryCalloc(1, keyLen + VARSTR_HEADER_SIZE); + if (p == NULL) { + return terrno; + } memcpy(varDataVal(p), record->lastKey.key.pks[0].pData, keyLen); varDataSetLen(p, keyLen); pBlockInfo->lastPk.pData = (uint8_t*)p; } } + return TSDB_CODE_SUCCESS; } static void freePkItem(void* pItem) { diff --git a/source/dnode/vnode/src/tsdb/tsdbReadUtil.h b/source/dnode/vnode/src/tsdb/tsdbReadUtil.h index cc37f20cf6..7c7bee8260 100644 --- a/source/dnode/vnode/src/tsdb/tsdbReadUtil.h +++ b/source/dnode/vnode/src/tsdb/tsdbReadUtil.h @@ -344,7 +344,7 @@ int32_t loadSttTombDataForAll(STsdbReader* pReader, SSttFileReader* pSttFileRead int32_t getNumOfRowsInSttBlock(SSttFileReader* pSttFileReader, SSttBlockLoadInfo* pBlockLoadInfo, TStatisBlkArray* pStatisBlkArray, uint64_t suid, const uint64_t* pUidList, int32_t numOfTables, int32_t* pNumOfRows); -void recordToBlockInfo(SFileDataBlockInfo* pBlockInfo, SBrinRecord* record); +int32_t recordToBlockInfo(SFileDataBlockInfo* pBlockInfo, SBrinRecord* record); void destroyLDataIter(SLDataIter* pIter); int32_t adjustSttDataIters(SArray* pSttFileBlockIterArray, STFileSet* pFileSet); diff --git a/source/libs/executor/src/tlinearhash.c b/source/libs/executor/src/tlinearhash.c index 69ce50e150..9654f74ab1 100644 --- a/source/libs/executor/src/tlinearhash.c +++ b/source/libs/executor/src/tlinearhash.c @@ -238,11 +238,14 @@ static int32_t doAddNewBucket(SLHashObj* pHashObj) { } SLHashBucket* pBucket = taosMemoryCalloc(1, sizeof(SLHashBucket)); + if (pBucket == NULL) { + return terrno; + } pHashObj->pBucket[pHashObj->numOfBuckets] = pBucket; pBucket->pPageIdList = taosArrayInit(2, sizeof(int32_t)); if (pBucket->pPageIdList == NULL) { - return TSDB_CODE_OUT_OF_MEMORY; + return terrno; } int32_t pageId = -1; @@ -281,13 +284,14 @@ SLHashObj* tHashInit(int32_t inMemPages, int32_t pageSize, _hash_fn_t fn, int32_ int32_t code = createDiskbasedBuf(&pHashObj->pBuf, pageSize, inMemPages * pageSize, "", tsTempDir); if (code != 0) { - taosMemoryFree(pHashObj); - terrno = code; - return NULL; + goto _error; } // disable compress when flushing to disk - setBufPageCompressOnDisk(pHashObj->pBuf, false); + code = setBufPageCompressOnDisk(pHashObj->pBuf, false); + if (code != 0) { + goto _error; + } /** * The number of bits in the hash value, which is used to decide the exact bucket where the object should be located @@ -299,16 +303,32 @@ SLHashObj* tHashInit(int32_t inMemPages, int32_t pageSize, _hash_fn_t fn, int32_ pHashObj->numOfAlloc = 4; // initial allocated array list pHashObj->pBucket = taosMemoryCalloc(pHashObj->numOfAlloc, POINTER_BYTES); + if (pHashObj->pBucket == NULL) { + code = terrno; + goto _error; + } code = doAddNewBucket(pHashObj); if (code != TSDB_CODE_SUCCESS) { - destroyDiskbasedBuf(pHashObj->pBuf); - taosMemoryFreeClear(pHashObj); - terrno = code; - return NULL; + goto _error; } return pHashObj; + +_error: + if (pHashObj->pBuf) { + destroyDiskbasedBuf(pHashObj->pBuf); + } + if (pHashObj->pBucket) { + for (int32_t i = 0; i < pHashObj->numOfBuckets; ++i) { + taosArrayDestroy(pHashObj->pBucket[i]->pPageIdList); + taosMemoryFree(pHashObj->pBucket[i]); + } + taosMemoryFree(pHashObj->pBucket); + } + taosMemoryFree(pHashObj); + terrno = code; + return NULL; } void* tHashCleanup(SLHashObj* pHashObj) { diff --git a/source/util/src/tpagedbuf.c b/source/util/src/tpagedbuf.c index e8303b563e..2ac2c210f0 100644 --- a/source/util/src/tpagedbuf.c +++ b/source/util/src/tpagedbuf.c @@ -362,6 +362,9 @@ int32_t createDiskbasedBuf(SDiskbasedBuf** pBuf, int32_t pagesize, int32_t inMem pPBuf->allocateId = -1; pPBuf->pFile = NULL; pPBuf->id = taosStrdup(id); + if (id != NULL && pPBuf->id == NULL) { + goto _error; + } pPBuf->fileSize = 0; pPBuf->pFree = taosArrayInit(4, sizeof(SFreeListItem)); pPBuf->freePgList = tdListNew(POINTER_BYTES); @@ -688,11 +691,15 @@ void setBufPageDirty(void* pPage, bool dirty) { ppi->dirty = dirty; } -void setBufPageCompressOnDisk(SDiskbasedBuf* pBuf, bool comp) { +int32_t setBufPageCompressOnDisk(SDiskbasedBuf* pBuf, bool comp) { pBuf->comp = comp; if (comp && (pBuf->assistBuf == NULL)) { pBuf->assistBuf = taosMemoryMalloc(pBuf->pageSize + 2); // EXTRA BYTES + if (pBuf->assistBuf) { + return terrno; + } } + return TSDB_CODE_SUCCESS; } int32_t dBufSetBufPageRecycled(SDiskbasedBuf* pBuf, void* pPage) {