From b1594a68574fe8a6d92b07de68d32fbb17cb5200 Mon Sep 17 00:00:00 2001 From: Liu Jicong Date: Mon, 10 Oct 2022 11:06:57 +0800 Subject: [PATCH] fix coverity scan --- source/dnode/mnode/impl/src/mndConsumer.c | 3 +++ source/dnode/mnode/impl/src/mndDef.c | 7 +++++-- source/dnode/mnode/impl/src/mndSubscribe.c | 11 ++++++++--- source/dnode/vnode/src/tq/tq.c | 14 ++++++++------ source/dnode/vnode/src/tq/tqMeta.c | 4 +++- source/dnode/vnode/src/tq/tqOffsetSnapshot.c | 11 ++++++++--- source/dnode/vnode/src/tq/tqPush.c | 3 +++ source/dnode/vnode/src/tq/tqRead.c | 1 + source/libs/wal/inc/walInt.h | 8 ++++---- source/libs/wal/src/walMeta.c | 9 ++++++++- source/libs/wal/src/walMgmt.c | 1 + source/libs/wal/src/walRead.c | 3 +-- source/libs/wal/src/walRef.c | 4 +++- source/libs/wal/src/walWrite.c | 1 + 14 files changed, 57 insertions(+), 23 deletions(-) diff --git a/source/dnode/mnode/impl/src/mndConsumer.c b/source/dnode/mnode/impl/src/mndConsumer.c index 3dfc10e554..c853828bf2 100644 --- a/source/dnode/mnode/impl/src/mndConsumer.c +++ b/source/dnode/mnode/impl/src/mndConsumer.c @@ -149,10 +149,13 @@ static int32_t mndProcessConsumerRecoverMsg(SRpcMsg *pMsg) { if (mndSetConsumerCommitLogs(pMnode, pTrans, pConsumerNew) != 0) goto FAIL; if (mndTransPrepare(pMnode, pTrans) != 0) goto FAIL; + tDeleteSMqConsumerObj(pConsumerNew); + taosMemoryFree(pConsumerNew); mndTransDrop(pTrans); return 0; FAIL: tDeleteSMqConsumerObj(pConsumerNew); + taosMemoryFree(pConsumerNew); mndTransDrop(pTrans); return -1; } diff --git a/source/dnode/mnode/impl/src/mndDef.c b/source/dnode/mnode/impl/src/mndDef.c index 42e56cd488..059bfdeeb3 100644 --- a/source/dnode/mnode/impl/src/mndDef.c +++ b/source/dnode/mnode/impl/src/mndDef.c @@ -118,7 +118,10 @@ int32_t tDecodeSStreamObj(SDecoder *pDecoder, SStreamObj *pObj) { for (int32_t j = 0; j < innerSz; j++) { SStreamTask *pTask = taosMemoryCalloc(1, sizeof(SStreamTask)); if (pTask == NULL) return -1; - if (tDecodeSStreamTask(pDecoder, pTask) < 0) return -1; + if (tDecodeSStreamTask(pDecoder, pTask) < 0) { + taosMemoryFree(pTask); + return -1; + } taosArrayPush(pArray, &pTask); } taosArrayPush(pObj->tasks, &pArray); @@ -353,7 +356,7 @@ SMqConsumerEp *tCloneSMqConsumerEp(const SMqConsumerEp *pConsumerEpOld) { } void tDeleteSMqConsumerEp(void *data) { - SMqConsumerEp *pConsumerEp = (SMqConsumerEp*)data; + SMqConsumerEp *pConsumerEp = (SMqConsumerEp *)data; taosArrayDestroyP(pConsumerEp->vgs, (FDelete)tDeleteSMqVgEp); } diff --git a/source/dnode/mnode/impl/src/mndSubscribe.c b/source/dnode/mnode/impl/src/mndSubscribe.c index f2fec27bc5..b797bfb4b2 100644 --- a/source/dnode/mnode/impl/src/mndSubscribe.c +++ b/source/dnode/mnode/impl/src/mndSubscribe.c @@ -678,25 +678,30 @@ static int32_t mndProcessDropCgroupReq(SRpcMsg *pReq) { if (pTrans == NULL) { mError("cgroup: %s on topic:%s, failed to drop since %s", dropReq.cgroup, dropReq.topic, terrstr()); mndReleaseSubscribe(pMnode, pSub); + mndTransDrop(pTrans); return -1; } mInfo("trans:%d, used to drop cgroup:%s on topic %s", pTrans->id, dropReq.cgroup, dropReq.topic); if (mndDropOffsetBySubKey(pMnode, pTrans, pSub->key) < 0) { - ASSERT(0); mndReleaseSubscribe(pMnode, pSub); + mndTransDrop(pTrans); return -1; } if (mndSetDropSubCommitLogs(pMnode, pTrans, pSub) < 0) { mError("cgroup %s on topic:%s, failed to drop since %s", dropReq.cgroup, dropReq.topic, terrstr()); mndReleaseSubscribe(pMnode, pSub); + mndTransDrop(pTrans); return -1; } - mndTransPrepare(pMnode, pTrans); - + if (mndTransPrepare(pMnode, pTrans) < 0) { + mndReleaseSubscribe(pMnode, pSub); + mndTransDrop(pTrans); + return -1; + } mndReleaseSubscribe(pMnode, pSub); return TSDB_CODE_ACTION_IN_PROGRESS; diff --git a/source/dnode/vnode/src/tq/tq.c b/source/dnode/vnode/src/tq/tq.c index ed5a894416..83cb3955e3 100644 --- a/source/dnode/vnode/src/tq/tq.c +++ b/source/dnode/vnode/src/tq/tq.c @@ -93,7 +93,8 @@ STQ* tqOpen(const char* path, SVnode* pVnode) { ASSERT(0); } - if (tqOffsetOpen(pTq) < 0) { + pTq->pOffsetStore = tqOffsetOpen(pTq); + if (pTq->pOffsetStore == NULL) { ASSERT(0); } @@ -648,7 +649,7 @@ int32_t tqProcessPollReq(STQ* pTq, SRpcMsg* pMsg) { code = -1; } tDeleteSTaosxRsp(&taosxRsp); - if (pCkHead) taosMemoryFree(pCkHead); + taosMemoryFreeClear(pCkHead); return code; } @@ -671,7 +672,7 @@ int32_t tqProcessPollReq(STQ* pTq, SRpcMsg* pMsg) { code = -1; } tDeleteSTaosxRsp(&taosxRsp); - if (pCkHead) taosMemoryFree(pCkHead); + taosMemoryFreeClear(pCkHead); return code; } else { fetchVer++; @@ -687,18 +688,19 @@ int32_t tqProcessPollReq(STQ* pTq, SRpcMsg* pMsg) { metaRsp.metaRsp = pHead->body; if (tqSendMetaPollRsp(pTq, pMsg, pReq, &metaRsp) < 0) { code = -1; - taosMemoryFree(pCkHead); + taosMemoryFreeClear(pCkHead); tDeleteSTaosxRsp(&taosxRsp); return code; } code = 0; - if (pCkHead) taosMemoryFree(pCkHead); + taosMemoryFreeClear(pCkHead); tDeleteSTaosxRsp(&taosxRsp); return code; } } } tDeleteSTaosxRsp(&taosxRsp); + taosMemoryFreeClear(pCkHead); return 0; } @@ -767,7 +769,7 @@ int32_t tqProcessVgChangeReq(STQ* pTq, int64_t version, char* msg, int32_t msgLe STqHandle* pHandle = taosHashGet(pTq->pHandle, req.subKey, strlen(req.subKey)); if (pHandle == NULL) { if (req.oldConsumerId != -1) { - tqError("vgId:%d, build new consumer handle %s for consumer %d, but old consumerId is %ld", req.vgId, req.subKey, + tqError("vgId:%d, build new consumer handle %s for consumer %ld, but old consumerId is %ld", req.vgId, req.subKey, req.newConsumerId, req.oldConsumerId); } if (req.newConsumerId == -1) { diff --git a/source/dnode/vnode/src/tq/tqMeta.c b/source/dnode/vnode/src/tq/tqMeta.c index c55e1059cf..c29541873e 100644 --- a/source/dnode/vnode/src/tq/tqMeta.c +++ b/source/dnode/vnode/src/tq/tqMeta.c @@ -170,11 +170,13 @@ int32_t tqMetaRestoreCheckInfo(STQ* pTq) { tDecoderInit(&decoder, (uint8_t*)pVal, vLen); if (tDecodeSTqCheckInfo(&decoder, &info) < 0) { terrno = TSDB_CODE_OUT_OF_MEMORY; + tdbTbcClose(pCur); return -1; } tDecoderClear(&decoder); if (taosHashPut(pTq->pCheckInfo, info.topic, strlen(info.topic), &info, sizeof(STqCheckInfo)) < 0) { terrno = TSDB_CODE_OUT_OF_MEMORY; + tdbTbcClose(pCur); return -1; } } @@ -188,7 +190,7 @@ int32_t tqMetaSaveHandle(STQ* pTq, const char* key, const STqHandle* pHandle) { tEncodeSize(tEncodeSTqHandle, pHandle, vlen, code); ASSERT(code == 0); - tqDebug("tq save %s(%d) consumer %" PRId64 " vgId:%d", pHandle->subKey, strlen(pHandle->subKey), pHandle->consumerId, + tqDebug("tq save %s(%d) consumer %ld vgId:%d", pHandle->subKey, (int32_t)strlen(pHandle->subKey), pHandle->consumerId, TD_VID(pTq->pVnode)); void* buf = taosMemoryCalloc(1, vlen); diff --git a/source/dnode/vnode/src/tq/tqOffsetSnapshot.c b/source/dnode/vnode/src/tq/tqOffsetSnapshot.c index 292c234f49..084959af65 100644 --- a/source/dnode/vnode/src/tq/tqOffsetSnapshot.c +++ b/source/dnode/vnode/src/tq/tqOffsetSnapshot.c @@ -54,8 +54,8 @@ int32_t tqOffsetSnapRead(STqOffsetReader* pReader, uint8_t** ppData) { char* fname = tqOffsetBuildFName(pReader->pTq->path, 0); TdFilePtr pFile = taosOpenFile(fname, TD_FILE_READ); - taosMemoryFree(fname); if (pFile != NULL) { + taosMemoryFree(fname); return 0; } @@ -63,6 +63,7 @@ int32_t tqOffsetSnapRead(STqOffsetReader* pReader, uint8_t** ppData) { if (taosStatFile(fname, &sz, NULL) < 0) { ASSERT(0); } + taosMemoryFree(fname); SSnapDataHdr* buf = taosMemoryCalloc(1, sz + sizeof(SSnapDataHdr)); if (buf == NULL) { @@ -120,9 +121,13 @@ int32_t tqOffsetWriterClose(STqOffsetWriter** ppWriter, int8_t rollback) { char* fname = tqOffsetBuildFName(pTq->path, 0); if (rollback) { - taosRemoveFile(pWriter->fname); + if (taosRemoveFile(pWriter->fname) < 0) { + ASSERT(0); + } } else { - taosRenameFile(pWriter->fname, fname); + if (taosRenameFile(pWriter->fname, fname) < 0) { + ASSERT(0); + } if (tqOffsetRestoreFromFile(pTq->pOffsetStore, fname) < 0) { ASSERT(0); } diff --git a/source/dnode/vnode/src/tq/tqPush.c b/source/dnode/vnode/src/tq/tqPush.c index dcfb07f0ff..bcdac1941a 100644 --- a/source/dnode/vnode/src/tq/tqPush.c +++ b/source/dnode/vnode/src/tq/tqPush.c @@ -226,6 +226,8 @@ int tqPushMsg(STQ* pTq, void* msg, int32_t msgLen, tmsg_t msgType, int64_t ver) if (data == NULL) { terrno = TSDB_CODE_OUT_OF_MEMORY; tqError("failed to copy data for stream since out of memory"); + taosArrayDestroyP(cachedKeys, (FDelete)taosMemoryFree); + taosArrayDestroy(cachedKeyLens); return -1; } memcpy(data, msg, msgLen); @@ -299,6 +301,7 @@ int tqPushMsg(STQ* pTq, void* msg, int32_t msgLen, tmsg_t msgType, int64_t ver) } taosArrayDestroyP(cachedKeys, (FDelete)taosMemoryFree); taosArrayDestroy(cachedKeyLens); + taosMemoryFree(data); } // unlock taosWUnLockLatch(&pTq->pushLock); diff --git a/source/dnode/vnode/src/tq/tqRead.c b/source/dnode/vnode/src/tq/tqRead.c index 3bd31e6660..30b6c3b983 100644 --- a/source/dnode/vnode/src/tq/tqRead.c +++ b/source/dnode/vnode/src/tq/tqRead.c @@ -253,6 +253,7 @@ STqReader* tqOpenReader(SVnode* pVnode) { pReader->pWalReader = walOpenReader(pVnode->pWal, NULL); if (pReader->pWalReader == NULL) { + taosMemoryFree(pReader); return NULL; } diff --git a/source/libs/wal/inc/walInt.h b/source/libs/wal/inc/walInt.h index e4b27292bb..1aea0e8148 100644 --- a/source/libs/wal/inc/walInt.h +++ b/source/libs/wal/inc/walInt.h @@ -97,12 +97,12 @@ static inline SWalFileInfo* walGetCurFileInfo(SWal* pWal) { return (SWalFileInfo*)taosArrayGet(pWal->fileInfoSet, pWal->writeCur); } -static inline int walBuildLogName(SWal* pWal, int64_t fileFirstVer, char* buf) { - return sprintf(buf, "%s/%020" PRId64 "." WAL_LOG_SUFFIX, pWal->path, fileFirstVer); +static inline void walBuildLogName(SWal* pWal, int64_t fileFirstVer, char* buf) { + sprintf(buf, "%s/%020" PRId64 "." WAL_LOG_SUFFIX, pWal->path, fileFirstVer); } -static inline int walBuildIdxName(SWal* pWal, int64_t fileFirstVer, char* buf) { - return sprintf(buf, "%s/%020" PRId64 "." WAL_INDEX_SUFFIX, pWal->path, fileFirstVer); +static inline void walBuildIdxName(SWal* pWal, int64_t fileFirstVer, char* buf) { + sprintf(buf, "%s/%020" PRId64 "." WAL_INDEX_SUFFIX, pWal->path, fileFirstVer); } static inline int walValidHeadCksum(SWalCkHead* pHead) { diff --git a/source/libs/wal/src/walMeta.c b/source/libs/wal/src/walMeta.c index c69046f707..5e70dce72d 100644 --- a/source/libs/wal/src/walMeta.c +++ b/source/libs/wal/src/walMeta.c @@ -157,6 +157,8 @@ int walCheckAndRepairMeta(SWal* pWal) { TdDirPtr pDir = taosOpenDir(pWal->path); if (pDir == NULL) { + regfree(&logRegPattern); + regfree(&idxRegPattern); wError("vgId:%d, path:%s, failed to open since %s", pWal->cfg.vgId, pWal->path, strerror(errno)); return -1; } @@ -304,7 +306,12 @@ int walCheckAndRepairIdx(SWal* pWal) { return -1; } while (idxEntry.ver < pFileInfo->lastVer) { - taosLSeekFile(pLogFile, idxEntry.offset, SEEK_SET); + if (taosLSeekFile(pLogFile, idxEntry.offset, SEEK_SET) == -1) { + terrno = TAOS_SYSTEM_ERROR(errno); + wError("vgId:%d, cannot seek file %s at %ld, since %s", pWal->cfg.vgId, fLogNameStr, idxEntry.offset, + terrstr()); + return -1; + } SWalCkHead ckHead; taosReadFile(pLogFile, &ckHead, sizeof(SWalCkHead)); if (idxEntry.ver != ckHead.head.version) { diff --git a/source/libs/wal/src/walMgmt.c b/source/libs/wal/src/walMgmt.c index a55f00d277..7974f3e32e 100644 --- a/source/libs/wal/src/walMgmt.c +++ b/source/libs/wal/src/walMgmt.c @@ -98,6 +98,7 @@ SWal *walOpen(const char *path, SWalCfg *pCfg) { tstrncpy(pWal->path, path, sizeof(pWal->path)); if (taosMkDir(pWal->path) != 0) { wError("vgId:%d, path:%s, failed to create directory since %s", pWal->cfg.vgId, pWal->path, strerror(errno)); + taosMemoryFree(pWal); return NULL; } diff --git a/source/libs/wal/src/walRead.c b/source/libs/wal/src/walRead.c index 5c437e6f7a..cc6f827b8e 100644 --- a/source/libs/wal/src/walRead.c +++ b/source/libs/wal/src/walRead.c @@ -494,7 +494,7 @@ int32_t walReadVer(SWalReader *pReader, int64_t ver) { taosThreadMutexUnlock(&pReader->mutex); return -1; } - pReader->pHead = ptr; + pReader->pHead = (SWalCkHead *)ptr; pReader->capacity = pReader->pHead->head.bodyLen; } @@ -504,7 +504,6 @@ int32_t walReadVer(SWalReader *pReader, int64_t ver) { terrno = TAOS_SYSTEM_ERROR(errno); else { terrno = TSDB_CODE_WAL_FILE_CORRUPTED; - ASSERT(0); } taosThreadMutexUnlock(&pReader->mutex); return -1; diff --git a/source/libs/wal/src/walRef.c b/source/libs/wal/src/walRef.c index 2b29012040..2c45fbbdaf 100644 --- a/source/libs/wal/src/walRef.c +++ b/source/libs/wal/src/walRef.c @@ -33,7 +33,9 @@ SWalRef *walOpenRef(SWal *pWal) { } void walCloseRef(SWal *pWal, int64_t refId) { - SWalRef *pRef = *(SWalRef **)taosHashGet(pWal->pRefHash, &refId, sizeof(int64_t)); + SWalRef **ppRef = taosHashGet(pWal->pRefHash, &refId, sizeof(int64_t)); + if (ppRef == NULL) return; + SWalRef *pRef = *ppRef; taosHashRemove(pWal->pRefHash, &refId, sizeof(int64_t)); taosMemoryFree(pRef); } diff --git a/source/libs/wal/src/walWrite.c b/source/libs/wal/src/walWrite.c index fad3977a21..628b432446 100644 --- a/source/libs/wal/src/walWrite.c +++ b/source/libs/wal/src/walWrite.c @@ -28,6 +28,7 @@ int32_t walRestoreFromSnapshot(SWal *pWal, int64_t ver) { SWalRef *pRef = (SWalRef *)pIter; if (pRef->refVer != -1 && pRef->refVer <= ver) { taosHashCancelIterate(pWal->pRefHash, pIter); + taosThreadMutexUnlock(&pWal->mutex); return -1; } } -- GitLab