From 4f4c4368b39961fc56615e49954dd1d9ba19c383 Mon Sep 17 00:00:00 2001 From: yifan hao Date: Tue, 5 May 2020 22:51:46 -0600 Subject: [PATCH] [vnode] Handle failure of adding to hash in vnodeOpen(). This patch adds error handling for call to vnodeOpen() in vnodeOpen. Previously the error is not handled, which could cause the allocated pVnode not registered anywhere, and this could cause memory leak. This patch also adds a missing deallocation of pVnode on failure. taosDeleteIntHash() only removes the pVnode from hash table, but pVnode needs to be deallocated explicitly. --- src/vnode/src/vnodeMain.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/vnode/src/vnodeMain.c b/src/vnode/src/vnodeMain.c index 9cb47d367b..5cb1298757 100644 --- a/src/vnode/src/vnodeMain.c +++ b/src/vnode/src/vnodeMain.c @@ -192,7 +192,12 @@ int32_t vnodeOpen(int32_t vnode, char *rootDir) { pVnode->status = TAOS_VN_STATUS_INIT; pVnode->refCount = 1; pVnode->version = 0; - taosAddIntHash(tsDnodeVnodesHash, pVnode->vgId, (char *)(&pVnode)); + void *pData = taosAddIntHash(tsDnodeVnodesHash, pVnode->vgId, (char *)(&pVnode)); + if (pData == NULL) { + dError("pVnode:%p vgId:%d, failed to add to hash", pVnode, pVnode->vgId); + code = TSDB_CODE_VG_INIT_FAILED; + goto vnodeOpenError; + } code = vnodeReadCfg(pVnode); if (code != TSDB_CODE_SUCCESS) { @@ -278,6 +283,7 @@ vnodeOpenError: } if (pVnode != NULL) { taosDeleteIntHash(tsDnodeVnodesHash, pVnode->vgId); + free(pVnode); } return code; } -- GitLab