From 990c45b661a9daa8e7fb87237c4e0459d0f3134c Mon Sep 17 00:00:00 2001 From: Hugo Saint-Vignes Date: Mon, 4 Jan 2021 17:56:44 +0100 Subject: [PATCH] [apps/shared] Prevent label method from being called on uninitialized chars --- apps/shared/curve_view.cpp | 13 ++++++++++--- apps/shared/curve_view_range.cpp | 4 ++-- apps/shared/curve_view_range.h | 2 +- apps/shared/interactive_curve_view_range.cpp | 7 ------- apps/shared/interactive_curve_view_range.h | 1 - 5 files changed, 13 insertions(+), 14 deletions(-) diff --git a/apps/shared/curve_view.cpp b/apps/shared/curve_view.cpp index 6ea0db849..1f1b38356 100644 --- a/apps/shared/curve_view.cpp +++ b/apps/shared/curve_view.cpp @@ -190,6 +190,8 @@ int CurveView::numberOfLabels(Axis axis) const { float minLabel = std::ceil(min(axis)/labelStep); float maxLabel = std::floor(max(axis)/labelStep); int numberOfLabels = maxLabel - minLabel + 1; + // Assert labels are up to date + assert(m_drawnRangeVersion == m_curveViewRange->rangeChecksum()); assert(numberOfLabels <= (axis == Axis::Horizontal ? k_maxNumberOfXLabels : k_maxNumberOfYLabels)); return numberOfLabels; } @@ -226,19 +228,24 @@ void CurveView::computeLabels(Axis axis) { if (axis == Axis::Horizontal) { if (labelBuffer[0] == 0) { - /* Some labels are too big and may overlap their neighbours. We write the + /* Some labels are too big and may overlap their neighbors. We write the * extrema labels only. */ computeHorizontalExtremaLabels(); - return; + break; } if (i > 0 && strcmp(labelBuffer, label(axis, i-1)) == 0) { /* We need to increase the number if significant digits, otherwise some * labels are rounded to the same value. */ computeHorizontalExtremaLabels(true); - return; + break; } } } + int maxNumberOfLabels = (axis == Axis::Horizontal ? k_maxNumberOfXLabels : k_maxNumberOfYLabels); + // All remaining labels are empty. They shouldn't be accessed anyway. + for (int i = axisLabelsCount; i < maxNumberOfLabels; i++) { + label(axis, i)[0] = 0; + } } void CurveView::simpleDrawBothAxesLabels(KDContext * ctx, KDRect rect) const { diff --git a/apps/shared/curve_view_range.cpp b/apps/shared/curve_view_range.cpp index 93ae6bc6d..f88e6b22e 100644 --- a/apps/shared/curve_view_range.cpp +++ b/apps/shared/curve_view_range.cpp @@ -10,8 +10,8 @@ namespace Shared { uint32_t CurveViewRange::rangeChecksum() { - float data[4] = {xMin(), xMax(), yMin(), yMax()}; - size_t dataLengthInBytes = 4*sizeof(float); + float data[7] = {xMin(), xMax(), yMin(), yMax(), xGridUnit(), yGridUnit(), offscreenYAxis()}; + size_t dataLengthInBytes = 7*sizeof(float); assert((dataLengthInBytes & 0x3) == 0); // Assert that dataLengthInBytes is a multiple of 4 return Ion::crc32Word((uint32_t *)data, dataLengthInBytes/sizeof(uint32_t)); } diff --git a/apps/shared/curve_view_range.h b/apps/shared/curve_view_range.h index 5867ab566..b463389ce 100644 --- a/apps/shared/curve_view_range.h +++ b/apps/shared/curve_view_range.h @@ -12,7 +12,7 @@ public: X, Y }; - virtual uint32_t rangeChecksum(); + uint32_t rangeChecksum(); virtual float xMin() const = 0; virtual float xMax() const = 0; diff --git a/apps/shared/interactive_curve_view_range.cpp b/apps/shared/interactive_curve_view_range.cpp index c4fec69bb..45b212eab 100644 --- a/apps/shared/interactive_curve_view_range.cpp +++ b/apps/shared/interactive_curve_view_range.cpp @@ -19,13 +19,6 @@ void InteractiveCurveViewRange::setDelegate(InteractiveCurveViewRangeDelegate * } } -uint32_t InteractiveCurveViewRange::rangeChecksum() { - float data[] = {xMin(), xMax(), yMin(), yMax()}; - size_t dataLengthInBytes = sizeof(data); - assert((dataLengthInBytes & 0x3) == 0); // Assert that dataLengthInBytes is a multiple of 4 - return Ion::crc32Word((uint32_t *)data, dataLengthInBytes/sizeof(uint32_t)); -} - void InteractiveCurveViewRange::setZoomAuto(bool v) { if (m_zoomAuto == v) { return; diff --git a/apps/shared/interactive_curve_view_range.h b/apps/shared/interactive_curve_view_range.h index d56f43bbc..9a4ae27c2 100644 --- a/apps/shared/interactive_curve_view_range.h +++ b/apps/shared/interactive_curve_view_range.h @@ -29,7 +29,6 @@ public: bool isOrthonormal() const; void setDelegate(InteractiveCurveViewRangeDelegate * delegate); - uint32_t rangeChecksum() override; bool zoomAuto() const { return m_zoomAuto; } void setZoomAuto(bool v);