From a1db09a360a3b7b84290e49cd61f6d0566c753ae Mon Sep 17 00:00:00 2001 From: Yihao Deng Date: Fri, 30 Aug 2024 07:13:56 +0000 Subject: [PATCH] fix mem leak possible --- source/libs/index/src/index.c | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/source/libs/index/src/index.c b/source/libs/index/src/index.c index 9df9c8f425..4fc836b738 100644 --- a/source/libs/index/src/index.c +++ b/source/libs/index/src/index.c @@ -661,12 +661,16 @@ int32_t idxFlushCacheToTFile(SIndex* sIdx, void* cache, bool quit) { } SArray* result = taosArrayInit(1024, sizeof(void*)); + if (result == NULL) { + TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, NULL, _exception); + } bool cn = cacheIter ? cacheIter->next(cacheIter) : false; bool tn = tfileIter ? tfileIter->next(tfileIter) : false; SIdxTRslt* tr = idxTRsltCreate(); if (tr == NULL) { + TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, NULL, _exception); } while (cn == true || tn == true) { IterateValue* cv = (cn == true) ? cacheIter->getValue(cacheIter) : NULL; @@ -682,33 +686,41 @@ int32_t idxFlushCacheToTFile(SIndex* sIdx, void* cache, bool quit) { } if (comp == 0) { code = idxMergeCacheAndTFile(result, cv, tv, tr); - if (code != 0) return code; + if (code != 0) { + TAOS_CHECK_GOTO(code, NULL, _exception); + } cn = cacheIter->next(cacheIter); tn = tfileIter->next(tfileIter); } else if (comp < 0) { code = idxMergeCacheAndTFile(result, cv, NULL, tr); - if (code != 0) return code; + if (code != 0) { + TAOS_CHECK_GOTO(code, NULL, _exception); + } cn = cacheIter->next(cacheIter); } else { code = idxMergeCacheAndTFile(result, NULL, tv, tr); - if (code != 0) return code; + if (code != 0) { + TAOS_CHECK_GOTO(code, NULL, _exception); + } tn = tfileIter->next(tfileIter); } } if ((code = idxMayMergeTempToFinalRslt(result, NULL, tr)) != 0) { idxTRsltDestroy(tr); - return code; + TAOS_CHECK_GOTO(code, NULL, _exception); } idxTRsltDestroy(tr); - int ret = idxGenTFile(sIdx, pCache, result); - if (ret != 0) { - indexError("failed to merge"); + code = idxGenTFile(sIdx, pCache, result); + if (code != 0) { + indexError("failed to merge since %s", tstrerror(code)); } else { int64_t cost = taosGetTimestampUs() - st; indexInfo("success to merge , time cost: %" PRId64 "ms", cost / 1000); } + +_exception: idxDestroyFinalRslt(result); idxCacheDestroyImm(pCache); @@ -725,7 +737,7 @@ int32_t idxFlushCacheToTFile(SIndex* sIdx, void* cache, bool quit) { } idxReleaseRef(sIdx->refId); - return ret; + return code; } void iterateValueDestroy(IterateValue* value, bool destroy) { if (destroy) {