From d02a125f34ea35f469b5f4e20e89fc8dc519d699 Mon Sep 17 00:00:00 2001 From: kailixu Date: Fri, 15 Mar 2024 16:29:10 +0800 Subject: [PATCH] fix: memory leak when alter json tag to NULL --- include/common/tmsg.h | 1 + source/libs/parser/src/parInsertSql.c | 4 +++- source/libs/parser/src/parTranslater.c | 4 ++++ tests/script/tsim/tag/add.sim | 1 - 4 files changed, 8 insertions(+), 2 deletions(-) diff --git a/include/common/tmsg.h b/include/common/tmsg.h index 06381a433f..03954e789e 100644 --- a/include/common/tmsg.h +++ b/include/common/tmsg.h @@ -2820,6 +2820,7 @@ typedef struct { char* tagName; int8_t isNull; int8_t tagType; + int8_t nullToken; uint32_t nTagVal; uint8_t* pTagVal; SArray* pTagArray; diff --git a/source/libs/parser/src/parInsertSql.c b/source/libs/parser/src/parInsertSql.c index 9efe25b563..5eb0b67884 100644 --- a/source/libs/parser/src/parInsertSql.c +++ b/source/libs/parser/src/parInsertSql.c @@ -922,7 +922,8 @@ static int32_t parseTagsClauseImpl(SInsertParseContext* pCxt, SVnodeModifyOpStmt if (!(pTagVals = taosArrayInit(pCxt->tags.numOfBound, sizeof(STagVal))) || !(pTagName = taosArrayInit(pCxt->tags.numOfBound, TSDB_COL_NAME_LEN))) { - return TSDB_CODE_OUT_OF_MEMORY; + code = TSDB_CODE_OUT_OF_MEMORY; + goto _exit; } for (int i = 0; TSDB_CODE_SUCCESS == code && i < pCxt->tags.numOfBound; ++i) { @@ -967,6 +968,7 @@ static int32_t parseTagsClauseImpl(SInsertParseContext* pCxt, SVnodeModifyOpStmt pTag = NULL; } +_exit: for (int32_t i = 0; i < TARRAY_SIZE(pTagVals); ++i) { STagVal* p = (STagVal*)TARRAY_GET_ELEM(pTagVals, i); if (IS_VAR_DATA_TYPE(p->type)) { diff --git a/source/libs/parser/src/parTranslater.c b/source/libs/parser/src/parTranslater.c index b29cd85294..1290a640cb 100644 --- a/source/libs/parser/src/parTranslater.c +++ b/source/libs/parser/src/parTranslater.c @@ -10442,6 +10442,9 @@ static int32_t buildUpdateTagValReq(STranslateContext* pCxt, SAlterTableStmt* pS if (TSDB_CODE_SUCCESS == code) { code = parseTagValue(&pCxt->msgBuf, &tagStr, pTableMeta->tableInfo.precision, pSchema, &token, NULL, pReq->pTagArray, &pTag); + if (pSchema->type == TSDB_DATA_TYPE_JSON && token.type == TK_NULL && code == TSDB_CODE_SUCCESS) { + pReq->nullToken = true; + } } if (TSDB_CODE_SUCCESS == code && tagStr) { NEXT_VALID_TOKEN(tagStr, token); @@ -10703,6 +10706,7 @@ static void destoryAlterTbReq(SVAlterTbReq* pReq) { } } taosArrayDestroy(pReq->pTagArray); + if(pReq->nullToken) tTagFree((STag*)pReq->pTagVal); } static int32_t rewriteAlterTableImpl(STranslateContext* pCxt, SAlterTableStmt* pStmt, STableMeta* pTableMeta, diff --git a/tests/script/tsim/tag/add.sim b/tests/script/tsim/tag/add.sim index 34a757a5ed..0e0755111d 100644 --- a/tests/script/tsim/tag/add.sim +++ b/tests/script/tsim/tag/add.sim @@ -395,7 +395,6 @@ sql alter table $tb set tag tgcol6='1' sql reset query cache sql select * from $mt where tgcol5 = '5' -print $mt print select * from $mt where tgcol5 = 5 print $data01 $data02 $data03 $data04 if $rows != 1 then