diff --git a/source/dnode/mgmt/exe/dmMain.c b/source/dnode/mgmt/exe/dmMain.c index 1fb5271a0e..377e4752f8 100644 --- a/source/dnode/mgmt/exe/dmMain.c +++ b/source/dnode/mgmt/exe/dmMain.c @@ -164,6 +164,9 @@ static int32_t dmParseArgs(int32_t argc, char const *argv[]) { if (argc < 2) return 0; global.envCmd = taosMemoryMalloc((argc - 1) * sizeof(char *)); + if (global.envCmd == NULL) { + return terrno; + } memset(global.envCmd, 0, (argc - 1) * sizeof(char *)); for (int32_t i = 1; i < argc; ++i) { if (strcmp(argv[i], "-c") == 0) { diff --git a/source/dnode/mgmt/node_util/src/dmEps.c b/source/dnode/mgmt/node_util/src/dmEps.c index f8f2b3dfca..0ae184ffd3 100644 --- a/source/dnode/mgmt/node_util/src/dmEps.c +++ b/source/dnode/mgmt/node_util/src/dmEps.c @@ -150,6 +150,10 @@ int32_t dmReadEps(SDnodeData *pData) { } char *tmp = taosMemoryMalloc(scopeLen + 1); + if (tmp == NULL) { + dError("failed to malloc memory for tsEncryptScope:%s", tsEncryptScope); + goto _OVER; + } memset(tmp, 0, scopeLen + 1); memcpy(tmp, tsEncryptScope, scopeLen); diff --git a/source/dnode/mgmt/node_util/src/dmFile.c b/source/dnode/mgmt/node_util/src/dmFile.c index 14154d1a23..1da13f72cd 100644 --- a/source/dnode/mgmt/node_util/src/dmFile.c +++ b/source/dnode/mgmt/node_util/src/dmFile.c @@ -342,6 +342,11 @@ static int32_t dmCompareEncryptKey(char *file, char *key, bool toLogFile) { int len = ENCRYPTED_LEN(size); result = taosMemoryMalloc(len); + if (result == NULL) { + code = terrno; + encryptError("failed to alloc memory file:%s since %s", file, tstrerror(code)); + goto _OVER; + } SCryptOpts opts = {0}; strncpy(opts.key, key, ENCRYPT_KEY_LEN); diff --git a/source/libs/index/src/indexCache.c b/source/libs/index/src/indexCache.c index 613c2430c9..4a03edaef4 100644 --- a/source/libs/index/src/indexCache.c +++ b/source/libs/index/src/indexCache.c @@ -80,6 +80,10 @@ static int32_t cacheSearchTerm(void* cache, SIndexTerm* term, SIdxTRslt* tr, STe IndexCache* pCache = mem->pCache; CacheTerm* pCt = taosMemoryCalloc(1, sizeof(CacheTerm)); + if (pCt == NULL) { + return terrno; + } + pCt->colVal = term->colVal; pCt->version = atomic_load_64(&pCache->version); @@ -290,6 +294,10 @@ static int32_t cacheSearchCompareFunc_JSON(void* cache, SIndexTerm* term, SIdxTR IndexCache* pCache = mem->pCache; CacheTerm* pCt = taosMemoryCalloc(1, sizeof(CacheTerm)); + if (pCt == NULL) { + return terrno; + } + pCt->colVal = term->colVal; pCt->version = atomic_load_64(&pCache->version); @@ -539,6 +547,10 @@ int idxCacheSchedToMerge(IndexCache* pCache, bool notify) { schedMsg.ahandle = pCache; if (notify) { schedMsg.thandle = taosMemoryMalloc(1); + if (schedMsg.thandle == NULL) { + indexError("fail to schedule merge task"); + return terrno; + } } schedMsg.msg = NULL; idxAcquireRef(pCache->index->refId); diff --git a/source/libs/index/src/indexComm.c b/source/libs/index/src/indexComm.c index fee0df5582..ce68615c0f 100644 --- a/source/libs/index/src/indexComm.c +++ b/source/libs/index/src/indexComm.c @@ -260,7 +260,10 @@ char* idxPackJsonData(SIndexTerm* itm) { int32_t sz = itm->nColName + itm->nColVal + sizeof(uint8_t) + sizeof(JSON_VALUE_DELIM) * 2 + 1; char* buf = (char*)taosMemoryCalloc(1, sz); - char* p = buf; + if (buf == NULL) { + return NULL; + } + char* p = buf; memcpy(p, itm->colName, itm->nColName); p += itm->nColName; @@ -288,7 +291,10 @@ char* idxPackJsonDataPrefix(SIndexTerm* itm, int32_t* skip) { int32_t sz = itm->nColName + itm->nColVal + sizeof(uint8_t) + sizeof(JSON_VALUE_DELIM) * 2 + 1; char* buf = (char*)taosMemoryCalloc(1, sz); - char* p = buf; + if (buf == NULL) { + return NULL; + } + char* p = buf; memcpy(p, itm->colName, itm->nColName); p += itm->nColName; @@ -315,7 +321,11 @@ char* idxPackJsonDataPrefixNoType(SIndexTerm* itm, int32_t* skip) { int32_t sz = itm->nColName + itm->nColVal + sizeof(uint8_t) + sizeof(JSON_VALUE_DELIM) * 2 + 1; char* buf = (char*)taosMemoryCalloc(1, sz); - char* p = buf; + if (buf == NULL) { + return NULL; + } + + char* p = buf; memcpy(p, itm->colName, itm->nColName); p += itm->nColName; diff --git a/source/libs/index/src/indexFst.c b/source/libs/index/src/indexFst.c index e87126b930..6f07df50fb 100644 --- a/source/libs/index/src/indexFst.c +++ b/source/libs/index/src/indexFst.c @@ -57,9 +57,17 @@ void fstUnFinishedNodesDestroy(FstUnFinishedNodes* nodes) { void fstUnFinishedNodesPushEmpty(FstUnFinishedNodes* nodes, bool isFinal) { FstBuilderNode* node = taosMemoryMalloc(sizeof(FstBuilderNode)); + if (node == NULL) { + return; + } + node->isFinal = isFinal; node->finalOutput = 0; node->trans = taosArrayInit(16, sizeof(FstTransition)); + if (node->trans == NULL) { + taosMemoryFree(node); + return; + } FstBuilderNodeUnfinished un = {.node = node, .last = NULL}; if (taosArrayPush(nodes->stack, &un) == NULL) { @@ -112,6 +120,9 @@ void fstUnFinishedNodesAddSuffix(FstUnFinishedNodes* nodes, FstSlice bs, Output for (uint64_t i = 1; i < len; i++) { FstBuilderNode* n = taosMemoryMalloc(sizeof(FstBuilderNode)); + if (n == NULL) { + return; + } n->isFinal = false; n->finalOutput = 0; n->trans = taosArrayInit(16, sizeof(FstTransition)); @@ -295,6 +306,9 @@ void fstStateCompileForAnyTrans(IdxFstFile* w, CompiledAddr addr, FstBuilderNode if (sz > TRANS_INDEX_THRESHOLD) { // A value of 255 indicates that no transition exists for the byte at that idx uint8_t* index = (uint8_t*)taosMemoryMalloc(sizeof(uint8_t) * 256); + if (index == NULL) { + return; + } memset(index, 255, sizeof(uint8_t) * 256); for (int32_t i = 0; i < sz; i++) { FstTransition* t = taosArrayGet(node->trans, i); @@ -973,6 +987,10 @@ Fst* fstCreate(FstSlice* slice) { fst->meta->checkSum = checkSum; FstSlice* s = taosMemoryCalloc(1, sizeof(FstSlice)); + if (s == NULL) { + goto FST_CREAT_FAILED; + } + *s = fstSliceCopy(slice, 0, FST_SLICE_LEN(slice) - 1); fst->data = s; @@ -1326,6 +1344,9 @@ FStmStRslt* stmStNextWith(FStmSt* sws, streamCallback__fn callback) { int32_t isz = taosArrayGetSize(sws->inp); uint8_t* buf = (uint8_t*)taosMemoryMalloc(isz * sizeof(uint8_t)); + if (buf == NULL) { + return NULL; + } for (uint32_t i = 0; i < isz; i++) { buf[i] = *(uint8_t*)taosArrayGet(sws->inp, i); } diff --git a/source/libs/index/src/indexFstAutomation.c b/source/libs/index/src/indexFstAutomation.c index 0c96d1aa0a..d0ea30997e 100644 --- a/source/libs/index/src/indexFstAutomation.c +++ b/source/libs/index/src/indexFstAutomation.c @@ -28,6 +28,11 @@ StartWithStateValue* startWithStateValueCreate(StartWithStateKind kind, ValueTyp } else if (ty == FST_CHAR) { size_t len = strlen((char*)val); sv->ptr = (char*)taosMemoryCalloc(1, len + 1); + if (sv->ptr == NULL) { + taosMemoryFree(sv); + return NULL; + } + memcpy(sv->ptr, val, len); } else if (ty == FST_ARRAY) { // TODO, @@ -63,6 +68,11 @@ StartWithStateValue* startWithStateValueDump(StartWithStateValue* sv) { } else if (nsv->type == FST_CHAR) { size_t len = strlen(sv->ptr); nsv->ptr = (char*)taosMemoryCalloc(1, len + 1); + if (nsv->ptr == NULL) { + taosMemoryFree(nsv); + return NULL; + } + memcpy(nsv->ptr, sv->ptr, len); } else if (nsv->type == FST_ARRAY) { // diff --git a/source/libs/index/src/indexFstFile.c b/source/libs/index/src/indexFstFile.c index efe890aa3a..73ecb22b3b 100644 --- a/source/libs/index/src/indexFstFile.c +++ b/source/libs/index/src/indexFstFile.c @@ -134,6 +134,9 @@ static int idxFileCtxDoReadFrom(IFileCtx* ctx, uint8_t* buf, int len, int32_t of int32_t cacheMemSize = sizeof(SDataBlock) + kBlockSize; SDataBlock* blk = taosMemoryCalloc(1, cacheMemSize); + if (blk == NULL) { + return terrno; + } blk->blockId = blkId; blk->nread = taosPReadFile(ctx->file.pFile, blk->buf, kBlockSize, blkId * kBlockSize); if (blk->nread < kBlockSize && blk->nread < len) { @@ -211,6 +214,10 @@ IFileCtx* idxFileCtxCreate(WriterType type, const char* path, bool readOnly, int ctx->file.wBufOffset = 0; ctx->file.wBufCap = kBlockSize * 4; ctx->file.wBuf = taosMemoryCalloc(1, ctx->file.wBufCap); + if (ctx->file.wBuf == NULL) { + indexError("failed to allocate memory for write buffer"); + goto END; + } } else { ctx->file.pFile = taosOpenFile(path, TD_FILE_READ); code = taosFStatFile(ctx->file.pFile, &ctx->file.size, NULL); @@ -228,6 +235,11 @@ IFileCtx* idxFileCtxCreate(WriterType type, const char* path, bool readOnly, int } } else if (ctx->type == TMEMORY) { ctx->mem.buf = taosMemoryCalloc(1, sizeof(char) * capacity); + if (ctx->mem.buf == NULL) { + indexError("failed to allocate memory for memory buffer"); + goto END; + } + ctx->mem.cap = capacity; } @@ -325,6 +337,10 @@ int idxFileFlush(IdxFstFile* write) { void idxFilePackUintIn(IdxFstFile* writer, uint64_t n, uint8_t nBytes) { uint8_t* buf = taosMemoryCalloc(8, sizeof(uint8_t)); + if (buf == NULL) { + indexError("failed to allocate memory for packing uint"); + return; + } for (uint8_t i = 0; i < nBytes; i++) { buf[i] = (uint8_t)n; n = n >> 8; diff --git a/source/libs/index/src/indexFstUtil.c b/source/libs/index/src/indexFstUtil.c index f0df2f7124..8c1095fb1d 100644 --- a/source/libs/index/src/indexFstUtil.c +++ b/source/libs/index/src/indexFstUtil.c @@ -80,6 +80,10 @@ FstSlice fstSliceCreate(uint8_t* data, uint64_t len) { str->ref = 1; str->len = len; str->data = taosMemoryMalloc(len * sizeof(uint8_t)); + if (str == NULL || str->data == NULL) { + taosMemoryFree(str); + return (FstSlice){.str = NULL, .start = 0, .end = 0}; + } if (data != NULL && str->data != NULL) { memcpy(str->data, data, len); diff --git a/source/libs/index/src/indexTfile.c b/source/libs/index/src/indexTfile.c index 78fd9da452..d92fec104b 100644 --- a/source/libs/index/src/indexTfile.c +++ b/source/libs/index/src/indexTfile.c @@ -799,6 +799,10 @@ static bool tfileIteratorNext(Iterate* iiter) { int32_t sz = 0; char* ch = (char*)fstSliceData(&rt->data, &sz); colVal = taosMemoryCalloc(1, sz + 1); + if (colVal == NULL) { + return false; + } + memcpy(colVal, ch, sz); offset = (uint64_t)(rt->out.out); @@ -835,6 +839,10 @@ Iterate* tfileIteratorCreate(TFileReader* reader) { } Iterate* iter = taosMemoryCalloc(1, sizeof(Iterate)); + if (iter == NULL) { + return NULL; + } + iter->iter = tfileFstIteratorCreate(reader); if (iter->iter == NULL) { taosMemoryFree(iter); @@ -843,6 +851,11 @@ Iterate* tfileIteratorCreate(TFileReader* reader) { iter->next = tfileIteratorNext; iter->getValue = tifileIterateGetValue; iter->val.val = taosArrayInit(1, sizeof(uint64_t)); + if (iter->val.val == NULL) { + tfileIteratorDestroy(iter); + return NULL; + } + iter->val.colVal = NULL; return iter; } diff --git a/source/libs/index/src/indexUtil.c b/source/libs/index/src/indexUtil.c index cbecb90d5e..12bff36553 100644 --- a/source/libs/index/src/indexUtil.c +++ b/source/libs/index/src/indexUtil.c @@ -90,6 +90,10 @@ int32_t iUnion(SArray *in, SArray *out) { } MergeIndex *mi = taosMemoryCalloc(sz, sizeof(MergeIndex)); + if (mi == NULL) { + return terrno; + } + for (int i = 0; i < sz; i++) { SArray *t = taosArrayGetP(in, i); mi[i].len = (int32_t)taosArrayGetSize(t);