From beff53a42a7dac59878ca797cd5e9aad530f5173 Mon Sep 17 00:00:00 2001 From: AlexDuan <417921451@qq.com> Date: Sun, 10 Oct 2021 14:21:30 +0800 Subject: [PATCH] recommit apercentile coredump fixed code --- src/query/inc/qHistogram.h | 2 +- src/query/src/qHistogram.c | 9 +++++---- src/query/tests/histogramTest.cpp | 10 +++++----- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/query/inc/qHistogram.h b/src/query/inc/qHistogram.h index 3b5c2b4cfb..ba32d4dfc8 100644 --- a/src/query/inc/qHistogram.h +++ b/src/query/inc/qHistogram.h @@ -67,7 +67,7 @@ void tHistogramDestroy(SHistogramInfo** pHisto); void tHistogramPrint(SHistogramInfo* pHisto); -int32_t histoBinarySearch(SHistBin* pEntry, int32_t len, double val); +int32_t histoBinarySearch(SHistBin* pEntry, int32_t len, double val, int32_t maxEntries); SHeapEntry* tHeapCreate(int32_t numOfEntries); void tHeapSort(SHeapEntry* pEntry, int32_t len); diff --git a/src/query/src/qHistogram.c b/src/query/src/qHistogram.c index 5fa35d0ee5..8544224a64 100644 --- a/src/query/src/qHistogram.c +++ b/src/query/src/qHistogram.c @@ -161,8 +161,8 @@ int32_t tHistogramAdd(SHistogramInfo** pHisto, double val) { } #if defined(USE_ARRAYLIST) - int32_t idx = histoBinarySearch((*pHisto)->elems, (*pHisto)->numOfEntries, val); - assert(idx >= 0 && idx <= (*pHisto)->maxEntries && (*pHisto)->elems != NULL); + int32_t idx = histoBinarySearch((*pHisto)->elems, (*pHisto)->numOfEntries, val, (*pHisto)->maxEntries); + assert(idx >= 0 && idx < (*pHisto)->maxEntries && (*pHisto)->elems != NULL); if ((*pHisto)->elems[idx].val == val && idx >= 0) { (*pHisto)->elems[idx].num += 1; @@ -359,7 +359,7 @@ int32_t tHistogramAdd(SHistogramInfo** pHisto, double val) { return 0; } -int32_t histoBinarySearch(SHistBin* pEntry, int32_t len, double val) { +int32_t histoBinarySearch(SHistBin* pEntry, int32_t len, double val, int32_t maxEntries) { int32_t end = len - 1; int32_t start = 0; @@ -377,6 +377,7 @@ int32_t histoBinarySearch(SHistBin* pEntry, int32_t len, double val) { } int32_t ret = start > end ? start : end; + if(ret >= maxEntries) ret = maxEntries - 1; if (ret < 0) { return 0; } else { @@ -469,7 +470,7 @@ void tHistogramPrint(SHistogramInfo* pHisto) { */ int64_t tHistogramSum(SHistogramInfo* pHisto, double v) { #if defined(USE_ARRAYLIST) - int32_t slotIdx = histoBinarySearch(pHisto->elems, pHisto->numOfEntries, v); + int32_t slotIdx = histoBinarySearch(pHisto->elems, pHisto->numOfEntries, v, pHisto->maxEntries); if (pHisto->elems[slotIdx].val != v) { slotIdx -= 1; diff --git a/src/query/tests/histogramTest.cpp b/src/query/tests/histogramTest.cpp index 0266ecffc1..70c334631c 100644 --- a/src/query/tests/histogramTest.cpp +++ b/src/query/tests/histogramTest.cpp @@ -98,19 +98,19 @@ TEST(testCase, histogram_binary_search) { pHisto->elems[i].val = i; } - int32_t idx = histoBinarySearch(pHisto->elems, pHisto->numOfEntries, 1); + int32_t idx = histoBinarySearch(pHisto->elems, pHisto->numOfEntries, 1, pHisto->maxEntries); assert(idx == 1); - idx = histoBinarySearch(pHisto->elems, pHisto->numOfEntries, 9); + idx = histoBinarySearch(pHisto->elems, pHisto->numOfEntries, 9, pHisto->maxEntries); assert(idx == 9); - idx = histoBinarySearch(pHisto->elems, pHisto->numOfEntries, 20); + idx = histoBinarySearch(pHisto->elems, pHisto->numOfEntries, 20, pHisto->maxEntries); assert(idx == 10); - idx = histoBinarySearch(pHisto->elems, pHisto->numOfEntries, -1); + idx = histoBinarySearch(pHisto->elems, pHisto->numOfEntries, -1, pHisto->maxEntries); assert(idx == 0); - idx = histoBinarySearch(pHisto->elems, pHisto->numOfEntries, 3.9); + idx = histoBinarySearch(pHisto->elems, pHisto->numOfEntries, 3.9, pHisto->maxEntries); assert(idx == 4); free(pHisto); -- GitLab