From 66b47472f03fe11d64239533939124eef8e8a506 Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Sat, 26 Mar 2022 22:46:12 +0800 Subject: [PATCH] [td-13039] fix memory leak and invlad memory operations. --- source/libs/scalar/inc/sclInt.h | 2 +- source/libs/scalar/src/filter.c | 7 ++-- source/libs/scalar/src/scalar.c | 39 +++++++++++-------- .../libs/scalar/test/scalar/scalarTests.cpp | 2 +- 4 files changed, 28 insertions(+), 22 deletions(-) diff --git a/source/libs/scalar/inc/sclInt.h b/source/libs/scalar/inc/sclInt.h index 721911d48d..ede960b8b8 100644 --- a/source/libs/scalar/inc/sclInt.h +++ b/source/libs/scalar/inc/sclInt.h @@ -43,7 +43,7 @@ typedef struct SScalarCtx { #define SCL_RET(c) do { int32_t _code = c; if (_code != TSDB_CODE_SUCCESS) { terrno = _code; } return _code; } while (0) #define SCL_ERR_JRET(c) do { code = c; if (code != TSDB_CODE_SUCCESS) { terrno = code; goto _return; } } while (0) -int32_t doConvertDataType(SScalarParam* in, SScalarParam* out, SValueNode* pValueNode); +int32_t doConvertDataType(SValueNode* pValueNode, SScalarParam* out); SColumnInfoData* createColumnInfoData(SDataType* pType, int32_t numOfRows); //int32_t sclMoveParamListData(SScalarParam *params, int32_t listNum, int32_t idx); diff --git a/source/libs/scalar/src/filter.c b/source/libs/scalar/src/filter.c index bf7e8553ee..06d05edbd8 100644 --- a/source/libs/scalar/src/filter.c +++ b/source/libs/scalar/src/filter.c @@ -1022,12 +1022,12 @@ int32_t fltAddGroupUnitFromNode(SFilterInfo *info, SNode* tree, SArray *group) { SNodeListNode *listNode = (SNodeListNode *)node->pRight; SListCell *cell = listNode->pNodeList->pHead; - SScalarParam in = {.columnData = calloc(1, sizeof(SColumnInfoData))}, out = {.columnData = calloc(1, sizeof(SColumnInfoData))}; + SScalarParam out = {.columnData = calloc(1, sizeof(SColumnInfoData))}; out.columnData->info.type = type; for (int32_t i = 0; i < listNode->pNodeList->length; ++i) { SValueNode *valueNode = (SValueNode *)cell->pNode; - code = doConvertDataType(&in, &out, valueNode); + code = doConvertDataType(valueNode, &out); if (code) { // fltError("convert from %d to %d failed", in.type, out.type); FLT_ERR_RET(code); @@ -1791,12 +1791,11 @@ int32_t fltInitValFieldData(SFilterInfo *info) { if (dType->type == type) { assignVal(fi->data, nodesGetValueFromNode(var), dType->bytes, type); } else { - SScalarParam in = {.columnData = calloc(1, sizeof(SColumnInfoData))}; SScalarParam out = {.columnData = calloc(1, sizeof(SColumnInfoData))}; out.columnData->info.type = type; // todo refactor the convert - int32_t code = doConvertDataType(&in, &out, var); + int32_t code = doConvertDataType(var, &out); if (code != TSDB_CODE_SUCCESS) { qError("convert value to type[%d] failed", type); return TSDB_CODE_TSC_INVALID_OPERATION; diff --git a/source/libs/scalar/src/scalar.c b/source/libs/scalar/src/scalar.c index 733acd0bea..2a2d3deff6 100644 --- a/source/libs/scalar/src/scalar.c +++ b/source/libs/scalar/src/scalar.c @@ -38,13 +38,17 @@ SColumnInfoData* createColumnInfoData(SDataType* pType, int32_t numOfRows) { } } -int32_t doConvertDataType(SScalarParam* in, SScalarParam* out, SValueNode* pValueNode) { - in->columnData = createColumnInfoData(&pValueNode->node.resType, 1); - colDataAppend(in->columnData, 0, nodesGetValueFromNode(pValueNode), false); - in->numOfRows = 1; +int32_t doConvertDataType(SValueNode* pValueNode, SScalarParam* out) { + SScalarParam in = {.numOfRows = 1}; + in.columnData = createColumnInfoData(&pValueNode->node.resType, 1); + colDataAppend(in.columnData, 0, nodesGetValueFromNode(pValueNode), false); blockDataEnsureColumnCapacity(out->columnData, 1); - return vectorConvertImpl(in, out); + + int32_t code = vectorConvertImpl(&in, out); + sclFreeParam(&in); + + return code; } int32_t scalarGenerateSetFromList(void **data, void *pNode, uint32_t type) { @@ -60,7 +64,7 @@ int32_t scalarGenerateSetFromList(void **data, void *pNode, uint32_t type) { SNodeListNode *nodeList = (SNodeListNode *)pNode; SListCell *cell = nodeList->pNodeList->pHead; - SScalarParam in = {.columnData = calloc(1, sizeof(SColumnInfoData))}, out = {.columnData = calloc(1, sizeof(SColumnInfoData))}; + SScalarParam out = {.columnData = calloc(1, sizeof(SColumnInfoData))}; int32_t len = 0; void *buf = NULL; @@ -72,8 +76,8 @@ int32_t scalarGenerateSetFromList(void **data, void *pNode, uint32_t type) { out.columnData->info.type = type; out.columnData->info.bytes = tDataTypes[type].bytes; - doConvertDataType(&in, &out, valueNode); - if (code) { + code = doConvertDataType(valueNode, &out); + if (code != TSDB_CODE_SUCCESS) { // sclError("convert data from %d to %d failed", in.type, out.type); SCL_ERR_JRET(code); } @@ -104,6 +108,7 @@ int32_t scalarGenerateSetFromList(void **data, void *pNode, uint32_t type) { } *data = pObj; + return TSDB_CODE_SUCCESS; _return: @@ -131,13 +136,15 @@ void sclFreeParamNoData(SScalarParam *param) { void sclFreeParam(SScalarParam *param) { sclFreeParamNoData(param); -// if (!param->dataInBlock) { -// if (SCL_DATA_TYPE_DUMMY_HASH == param->type) { -// taosHashCleanup((SHashObj *)param->orig.data); -// } else { -// tfree(param->orig.data); -// } -// } + + if (param->columnData != NULL) { + colDataDestroy(param->columnData); + tfree(param->columnData); + } + + if (param->pHashFilter != NULL) { + taosHashCleanup(param->pHashFilter); + } } int32_t sclCopyValueNodeValue(SValueNode *pNode, void **res) { @@ -413,7 +420,7 @@ int32_t sclExecOperator(SOperatorNode *node, SScalarCtx *ctx, SScalarParam *outp _return: for (int32_t i = 0; i < paramNum; ++i) { - sclFreeParamNoData(params + i); +// sclFreeParam(¶ms[i]); } tfree(params); diff --git a/source/libs/scalar/test/scalar/scalarTests.cpp b/source/libs/scalar/test/scalar/scalarTests.cpp index 9e691ba10d..fdd0e5e753 100644 --- a/source/libs/scalar/test/scalar/scalarTests.cpp +++ b/source/libs/scalar/test/scalar/scalarTests.cpp @@ -1270,7 +1270,7 @@ TEST(columnTest, binary_column_is_true) { } int32_t rowNum = sizeof(leftv)/sizeof(leftv[0]); - scltMakeColumnNode(&pLeft, &src, TSDB_DATA_TYPE_BINARY, 3, rowNum, leftv); + scltMakeColumnNode(&pLeft, &src, TSDB_DATA_TYPE_BINARY, 5, rowNum, leftv); scltMakeOpNode(&opNode, OP_TYPE_IS_TRUE, TSDB_DATA_TYPE_BOOL, pLeft, NULL);