diff --git a/source/libs/index/src/index.c b/source/libs/index/src/index.c index 5a8ab52d25..85582452bb 100644 --- a/source/libs/index/src/index.c +++ b/source/libs/index/src/index.c @@ -212,6 +212,7 @@ void idxReleaseRef(int64_t ref) { int32_t indexPut(SIndex* index, SIndexMultiTerm* fVals, uint64_t uid) { // TODO(yihao): reduce the lock range + int32_t code = 0; (void)taosThreadMutexLock(&index->mtx); for (int i = 0; i < taosArrayGetSize(fVals); i++) { SIndexTerm* p = taosArrayGetP(fVals, i); @@ -223,11 +224,19 @@ int32_t indexPut(SIndex* index, SIndexMultiTerm* fVals, uint64_t uid) { IndexCache** cache = taosHashGet(index->colObj, buf, sz); if (cache == NULL) { IndexCache* pCache = idxCacheCreate(index, p->suid, p->colName, p->colType); - (void)taosHashPut(index->colObj, buf, sz, &pCache, sizeof(void*)); + code = taosHashPut(index->colObj, buf, sz, &pCache, sizeof(void*)); + if (code != 0) { + idxCacheDestroy(pCache); + break; + } } } (void)taosThreadMutexUnlock(&index->mtx); + if (code != 0) { + return code; + } + for (int i = 0; i < taosArrayGetSize(fVals); i++) { SIndexTerm* p = taosArrayGetP(fVals, i); diff --git a/source/libs/index/src/indexFst.c b/source/libs/index/src/indexFst.c index f98d5fd125..0e1cbb6221 100644 --- a/source/libs/index/src/indexFst.c +++ b/source/libs/index/src/indexFst.c @@ -1366,11 +1366,18 @@ FStmBuilder* stmBuilderCreate(Fst* fst, FAutoCtx* aut) { b->aut = aut; b->min = fstBoundStateCreate(Unbounded, NULL); b->max = fstBoundStateCreate(Unbounded, NULL); + + if (b->min == NULL || b->max == NULL) { + stmBuilderDestroy(b); + return NULL; + } + return b; } void stmBuilderDestroy(FStmBuilder* b) { - fstSliceDestroy(&b->min->data); - fstSliceDestroy(&b->max->data); + if (b->min) fstSliceDestroy(&b->min->data); + if (b->max) fstSliceDestroy(&b->max->data); + taosMemoryFreeClear(b->min); taosMemoryFreeClear(b->max); taosMemoryFree(b); diff --git a/source/libs/index/src/indexTfile.c b/source/libs/index/src/indexTfile.c index 598ecb5f1c..cdaa5bfb7c 100644 --- a/source/libs/index/src/indexTfile.c +++ b/source/libs/index/src/indexTfile.c @@ -97,6 +97,11 @@ TFileCache* tfileCacheCreate(SIndex* idx, const char* path) { } tcache->tableCache = taosHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK); + if (tcache->tableCache == NULL) { + indexError("failed to open table cache since%s", tstrerror(terrno)); + goto End; + } + tcache->capacity = 64; SArray* files = NULL; @@ -126,8 +131,11 @@ TFileCache* tfileCacheCreate(SIndex* idx, const char* path) { char buf[128] = {0}; int32_t sz = idxSerialCacheKey(&key, buf); - (void)taosHashPut(tcache->tableCache, buf, sz, &reader, sizeof(void*)); - tfileReaderRef(reader); + code = taosHashPut(tcache->tableCache, buf, sz, &reader, sizeof(void*)); + if (code != 0) { + tfileReaderRef(reader); + goto End; + } } taosArrayDestroyEx(files, tfileDestroyFileName); return tcache; @@ -173,10 +181,14 @@ int32_t tfileCachePut(TFileCache* tcache, ICacheKey* key, TFileReader* reader) { TFileReader** p = taosHashGet(tcache->tableCache, buf, sz); if (p != NULL && *p != NULL) { TFileReader* oldRdr = *p; - (void)taosHashRemove(tcache->tableCache, buf, sz); - indexInfo("found %s, should remove file %s", buf, oldRdr->ctx->file.buf); - oldRdr->remove = true; - tfileReaderUnRef(oldRdr); + if ((code = taosHashRemove(tcache->tableCache, buf, sz)) != 0) { + indexError("failed to remove old reader from cache since %s, suid:%" PRIu64 ", colName:%s", tstrerror(code), + oldRdr->header.suid, oldRdr->header.colName); + } else { + indexInfo("found %s, should remove file %s", buf, oldRdr->ctx->file.buf); + oldRdr->remove = true; + tfileReaderUnRef(oldRdr); + } } code = taosHashPut(tcache->tableCache, buf, sz, &reader, sizeof(void*)); @@ -267,8 +279,16 @@ static int32_t tfSearchPrefix(void* reader, SIndexTerm* tem, SIdxTRslt* tr) { uint64_t sz = tem->nColVal; SArray* offsets = taosArrayInit(16, sizeof(uint64_t)); + if (offsets == NULL) { + return TSDB_CODE_OUT_OF_MEMORY; + } + + FAutoCtx* ctx = automCtxCreate((void*)p, AUTOMATION_PREFIX); + if (ctx == NULL) { + taosArrayDestroy(offsets); + return TSDB_CODE_OUT_OF_MEMORY; + } - FAutoCtx* ctx = automCtxCreate((void*)p, AUTOMATION_PREFIX); FStmBuilder* sb = fstSearch(((TFileReader*)reader)->fst, ctx); FStmSt* st = stmBuilderIntoStm(sb); FStmStRslt* rt = NULL;