From a63b848a979b394559efb304d359d39aa62a9906 Mon Sep 17 00:00:00 2001 From: hjxilinx Date: Thu, 16 Jan 2020 21:47:33 +0800 Subject: [PATCH] fix memory leaks --- src/client/src/tscAst.c | 6 +++--- src/client/src/tscFunctionImpl.c | 4 ++-- src/client/src/tscSQLParser.c | 15 ++++++++++----- src/system/detail/src/vnodeQueryImpl.c | 15 ++++++++------- src/util/src/ttypes.c | 3 +-- 5 files changed, 24 insertions(+), 19 deletions(-) diff --git a/src/client/src/tscAst.c b/src/client/src/tscAst.c index d071358dbf..9f12f8c1ae 100644 --- a/src/client/src/tscAst.c +++ b/src/client/src/tscAst.c @@ -492,12 +492,12 @@ static void setInitialValueForRangeQueryCondition(tSKipListQueryCond *q, int8_t case TSDB_DATA_TYPE_NCHAR: case TSDB_DATA_TYPE_BINARY: { q->upperBnd.nType = type; - q->upperBnd.pz = "\0"; + q->upperBnd.pz = NULL; q->upperBnd.nLen = -1; q->lowerBnd.nType = type; - q->lowerBnd.pz = "\0"; - q->lowerBnd.nLen = 0; + q->lowerBnd.pz = NULL; + q->lowerBnd.nLen = -1; } } } diff --git a/src/client/src/tscFunctionImpl.c b/src/client/src/tscFunctionImpl.c index 15509d1a83..22875593c2 100644 --- a/src/client/src/tscFunctionImpl.c +++ b/src/client/src/tscFunctionImpl.c @@ -3272,7 +3272,7 @@ char *arithmetic_callback_function(void *param, char *name, int32_t colId) { static void arithmetic_function(SQLFunctionCtx *pCtx) { GET_RES_INFO(pCtx)->numOfRes += pCtx->size; - SArithmeticSupport *sas = (SArithmeticSupport *)pCtx->param[0].pz; + SArithmeticSupport *sas = (SArithmeticSupport *)pCtx->param[1].pz; tSQLBinaryExprCalcTraverse(sas->pExpr->pBinExprInfo.pBinExpr, pCtx->size, pCtx->aOutputBuf, sas, pCtx->order, arithmetic_callback_function); @@ -3282,7 +3282,7 @@ static void arithmetic_function(SQLFunctionCtx *pCtx) { static void arithmetic_function_f(SQLFunctionCtx *pCtx, int32_t index) { INC_INIT_VAL(pCtx, 1); - SArithmeticSupport *sas = (SArithmeticSupport *)pCtx->param[0].pz; + SArithmeticSupport *sas = (SArithmeticSupport *)pCtx->param[1].pz; sas->offset = index; tSQLBinaryExprCalcTraverse(sas->pExpr->pBinExprInfo.pBinExpr, 1, pCtx->aOutputBuf, sas, pCtx->order, diff --git a/src/client/src/tscSQLParser.c b/src/client/src/tscSQLParser.c index 422ae707ad..b6b3a63b5f 100644 --- a/src/client/src/tscSQLParser.c +++ b/src/client/src/tscSQLParser.c @@ -5205,6 +5205,13 @@ void addGroupInfoForSubquery(SSqlObj* pParentObj, SSqlObj* pSql, int32_t tableIn } } +// limit the output to be 1 for each state value +static void doLimitOutputNormalColOfGroupby(SSqlExpr* pExpr) { + int32_t outputRow = 1; + tVariantCreateFromBinary(&pExpr->param[0], (char*) &outputRow, sizeof(int32_t), TSDB_DATA_TYPE_INT); + pExpr->numOfParams = 1; +} + void doAddGroupColumnForSubquery(SSqlCmd* pCmd, int32_t tagIndex) { int32_t index = pCmd->groupbyExpr.columnInfo[tagIndex].colIdx; @@ -5217,9 +5224,8 @@ void doAddGroupColumnForSubquery(SSqlCmd* pCmd, int32_t tagIndex) { pSchema->bytes, pSchema->bytes); pExpr->colInfo.flag = TSDB_COL_NORMAL; - pExpr->param[0].i64Key = 1; - pExpr->numOfParams = 1; - + doLimitOutputNormalColOfGroupby(pExpr); + // NOTE: tag column does not add to source column list SColumnList list = {0}; list.num = 1; @@ -5263,8 +5269,7 @@ static void doUpdateSqlFunctionForColPrj(SSqlCmd* pCmd) { for (int32_t j = 0; j < pCmd->groupbyExpr.numOfGroupCols; ++j) { if (pExpr->colInfo.colId == pCmd->groupbyExpr.columnInfo[j].colId) { qualifiedCol = true; - - pExpr->param[0].i64Key = 1; // limit the output to be 1 for each state value + doLimitOutputNormalColOfGroupby(pExpr); pExpr->numOfParams = 1; break; } diff --git a/src/system/detail/src/vnodeQueryImpl.c b/src/system/detail/src/vnodeQueryImpl.c index 3673e5751d..3158ecf15b 100644 --- a/src/system/detail/src/vnodeQueryImpl.c +++ b/src/system/detail/src/vnodeQueryImpl.c @@ -2028,7 +2028,7 @@ void setExecParams(SQuery *pQuery, SQLFunctionCtx *pCtx, int64_t startQueryTimes pCtx->ptsList = (int64_t *)(primaryColumnData + startOffset * TSDB_KEYSIZE); } else if (functionId == TSDB_FUNC_ARITHM) { - pCtx->param[0].pz = param; + pCtx->param[1].pz = param; } pCtx->startOffset = startOffset; @@ -2117,14 +2117,15 @@ static int32_t setupQueryRuntimeEnv(SMeterObj *pMeterObj, SQuery *pQuery, SQuery pCtx->order = pQuery->order.order; pCtx->functionId = pSqlFuncMsg->functionId; - /* - * tricky: in case of char array parameters, we employ the shallow copy - * method and get the ownership of the char array, it later release the allocated memory if exists - */ pCtx->numOfParams = pSqlFuncMsg->numOfParams; for (int32_t j = 0; j < pCtx->numOfParams; ++j) { - pCtx->param[j].nType = pSqlFuncMsg->arg[j].argType; - pCtx->param[j].i64Key = pSqlFuncMsg->arg[j].argValue.i64; + int16_t type = pSqlFuncMsg->arg[j].argType; + int16_t bytes = pSqlFuncMsg->arg[j].argBytes; + if (type == TSDB_DATA_TYPE_BINARY || type == TSDB_DATA_TYPE_NCHAR) { + tVariantCreateFromBinary(&pCtx->param[j], pSqlFuncMsg->arg->argValue.pz, bytes, type); + } else { + tVariantCreateFromBinary(&pCtx->param[j], (char*) &pSqlFuncMsg->arg[j].argValue.i64, bytes, type); + } } // set the order information for top/bottom query diff --git a/src/util/src/ttypes.c b/src/util/src/ttypes.c index b048748d95..5fcbe8605c 100644 --- a/src/util/src/ttypes.c +++ b/src/util/src/ttypes.c @@ -163,10 +163,9 @@ void tVariantCreateFromBinary(tVariant *pVar, char *pz, uint32_t len, uint32_t t void tVariantDestroy(tVariant *pVar) { if (pVar == NULL) return; - if ((pVar->nType == TSDB_DATA_TYPE_BINARY || pVar->nType == TSDB_DATA_TYPE_NCHAR) && pVar->nLen > 0) { + if (pVar->nType == TSDB_DATA_TYPE_BINARY || pVar->nType == TSDB_DATA_TYPE_NCHAR) { free(pVar->pz); pVar->pz = NULL; - pVar->nLen = 0; } } -- GitLab