From 8474fc088e19ed297183e4316c9821566adc59ba Mon Sep 17 00:00:00 2001 From: slzhou Date: Wed, 23 Nov 2022 13:54:59 +0800 Subject: [PATCH] fix: core dump due to assert reallocation result when reallocate fails --- src/query/inc/qExtbuffer.h | 4 +--- src/query/src/qExecutor.c | 8 +++++++- src/query/src/qExtbuffer.c | 10 ++++++++-- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/query/inc/qExtbuffer.h b/src/query/inc/qExtbuffer.h index 9938f599ed..509549ba22 100644 --- a/src/query/inc/qExtbuffer.h +++ b/src/query/inc/qExtbuffer.h @@ -227,8 +227,6 @@ tOrderDescriptor *tOrderDesCreate(const int32_t *orderColIdx, int32_t numOfOrder void tOrderDescDestroy(tOrderDescriptor *pDesc); -void taoscQSort(void** pCols, SSchema* pSchema, int32_t numOfCols, int32_t numOfRows, int32_t index, __compar_fn_t compareFn); - void tColModelAppend(SColumnModel *dstModel, tFilePage *dstPage, void *srcData, int32_t srcStartRows, int32_t numOfRowsToWrite, int32_t srcCapacity); @@ -239,7 +237,7 @@ void tColDataQSort(tOrderDescriptor *, int32_t numOfRows, int32_t start, int32_t void tColDataMergeSort(tOrderDescriptor *, int32_t numOfRows, int32_t start, int32_t end, char *data, int32_t orderType); -void taoscQSort(void** pCols, SSchema* pSchema, int32_t numOfCols, int32_t numOfRows, int32_t index, __compar_fn_t compareFn); +int32_t taoscQSort(void** pCols, SSchema* pSchema, int32_t numOfCols, int32_t numOfRows, int32_t index, __compar_fn_t compareFn); int32_t compare_sa(tOrderDescriptor *, int32_t numOfRows, int32_t idx1, int32_t idx2, char *data); diff --git a/src/query/src/qExecutor.c b/src/query/src/qExecutor.c index 9424200840..dc2837869f 100644 --- a/src/query/src/qExecutor.c +++ b/src/query/src/qExecutor.c @@ -5959,7 +5959,13 @@ static SSDataBlock* doSort(void* param, bool* newgroup) { __compar_fn_t comp = getKeyComparFunc(pSchema[pInfo->colIndex].type, pInfo->order); if (pInfo->pDataBlock->info.rows) { - taoscQSort(pCols, pSchema, numOfCols, pInfo->pDataBlock->info.rows, pInfo->colIndex, comp); + int32_t code = taoscQSort(pCols, pSchema, numOfCols, pInfo->pDataBlock->info.rows, pInfo->colIndex, comp); + if (code != TSDB_CODE_SUCCESS) { + qError("QInfo:0x%"PRIx64 " can not sort since %s", GET_QID(pOperator->pRuntimeEnv), tstrerror(code)); + tfree(pCols); + tfree(pSchema); + longjmp(pOperator->pRuntimeEnv->env, code); + } } tfree(pCols); diff --git a/src/query/src/qExtbuffer.c b/src/query/src/qExtbuffer.c index d49d6ecf0b..f3fcab9ff7 100644 --- a/src/query/src/qExtbuffer.c +++ b/src/query/src/qExtbuffer.c @@ -1246,7 +1246,7 @@ void tOrderDescDestroy(tOrderDescriptor *pDesc) { tfree(pDesc); } -void taoscQSort(void** pCols, SSchema* pSchema, int32_t numOfCols, int32_t numOfRows, int32_t idx, __compar_fn_t compareFn) { +int32_t taoscQSort(void** pCols, SSchema* pSchema, int32_t numOfCols, int32_t numOfRows, int32_t idx, __compar_fn_t compareFn) { assert(numOfRows > 0 && numOfCols > 0 && idx >= 0 && idx < numOfCols); int32_t bytes = pSchema[idx].bytes; @@ -1278,7 +1278,12 @@ void taoscQSort(void** pCols, SSchema* pSchema, int32_t numOfCols, int32_t numOf // make sure memory buffer is enough if (prevLength < bytes1) { char *tmp = realloc(p, bytes1 * numOfRows); - assert(tmp); + if (tmp == NULL) { + qError("can not allocated memory. bytes:%d", bytes*numOfRows); + tfree(p); + tfree(buf); + return TSDB_CODE_QRY_OUT_OF_MEMORY; + } p = tmp; prevLength = bytes1; @@ -1298,4 +1303,5 @@ void taoscQSort(void** pCols, SSchema* pSchema, int32_t numOfCols, int32_t numOf tfree(buf); tfree(p); + return TSDB_CODE_SUCCESS; } -- GitLab