From 941705c5d6be4c76d54e40aa879b95db4677e34b Mon Sep 17 00:00:00 2001 From: Bomin Zhang Date: Sat, 8 Aug 2020 10:29:50 +0800 Subject: [PATCH] td-1089: fix memory leak and invalid read --- src/client/inc/tscUtil.h | 1 + src/client/src/tscSub.c | 16 +++++++++++++++- src/client/src/tscUtil.c | 12 +++++++++++- 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/client/inc/tscUtil.h b/src/client/inc/tscUtil.h index fbec2e2167..b15286fe80 100644 --- a/src/client/inc/tscUtil.h +++ b/src/client/inc/tscUtil.h @@ -230,6 +230,7 @@ int32_t tscAddSubqueryInfo(SSqlCmd *pCmd); void tscInitQueryInfo(SQueryInfo* pQueryInfo); void tscClearSubqueryInfo(SSqlCmd* pCmd); +void tscFreeVgroupTableInfo(SArray* pVgroupTables); int tscGetSTableVgroupInfo(SSqlObj* pSql, int32_t clauseIndex); int tscGetTableMeta(SSqlObj* pSql, STableMetaInfo* pTableMetaInfo); diff --git a/src/client/src/tscSub.c b/src/client/src/tscSub.c index bef754d69f..f740e3cce9 100644 --- a/src/client/src/tscSub.c +++ b/src/client/src/tscSub.c @@ -230,6 +230,19 @@ static SArray* getTableList( SSqlObj* pSql ) { return result; } +static int32_t compareTidTag(const void* p1, const void* p2) { + const STidTags* t1 = (const STidTags*)p1; + const STidTags* t2 = (const STidTags*)p2; + + if (t1->vgId != t2->vgId) { + return (t1->vgId > t2->vgId) ? 1 : -1; + } + if (t1->tid != t2->tid) { + return (t1->tid > t2->tid) ? 1 : -1; + } + return 0; +} + static int tscUpdateSubscription(STscObj* pObj, SSub* pSub) { SSqlObj* pSql = pSub->pSql; @@ -270,7 +283,8 @@ static int tscUpdateSubscription(STscObj* pObj, SSub* pSub) { pSub->progress = progress; if (UTIL_TABLE_IS_SUPER_TABLE(pTableMetaInfo)) { - taosArraySort( tables, tscCompareTidTags ); + taosArraySort( tables, compareTidTag ); + tscFreeVgroupTableInfo(pTableMetaInfo->pVgroupTables); tscBuildVgroupTableInfo(pSql, pTableMetaInfo, tables); } taosArrayDestroy(tables); diff --git a/src/client/src/tscUtil.c b/src/client/src/tscUtil.c index c74cc82f85..11add5d47e 100644 --- a/src/client/src/tscUtil.c +++ b/src/client/src/tscUtil.c @@ -1556,12 +1556,22 @@ void tscClearSubqueryInfo(SSqlCmd* pCmd) { } } +void tscFreeVgroupTableInfo(SArray* pVgroupTables) { + if (pVgroupTables != NULL) { + for (size_t i = 0; i < taosArrayGetSize(pVgroupTables); i++) { + SVgroupTableInfo* pInfo = taosArrayGet(pVgroupTables, i); + taosArrayDestroy(pInfo->itemList); + } + taosArrayDestroy(pVgroupTables); + } +} + void clearAllTableMetaInfo(SQueryInfo* pQueryInfo, const char* address, bool removeFromCache) { tscDebug("%p deref the table meta in cache, numOfTables:%d", address, pQueryInfo->numOfTables); for(int32_t i = 0; i < pQueryInfo->numOfTables; ++i) { STableMetaInfo* pTableMetaInfo = tscGetMetaInfo(pQueryInfo, i); - + tscFreeVgroupTableInfo(pTableMetaInfo->pVgroupTables); tscClearTableMetaInfo(pTableMetaInfo, removeFromCache); free(pTableMetaInfo); } -- GitLab