From 98887e87d5b9fe53ef2d34ca5165c9fce383ea6d Mon Sep 17 00:00:00 2001 From: Noah Date: Thu, 8 Feb 2024 03:32:40 +0100 Subject: [PATCH] fix memory corruption bug --- src/lib/cimb_translator/AdjacentCellFinder.cpp | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/lib/cimb_translator/AdjacentCellFinder.cpp b/src/lib/cimb_translator/AdjacentCellFinder.cpp index 49dbba77..6637b487 100644 --- a/src/lib/cimb_translator/AdjacentCellFinder.cpp +++ b/src/lib/cimb_translator/AdjacentCellFinder.cpp @@ -43,10 +43,10 @@ int AdjacentCellFinder::in_row_with_margin(int index) const int AdjacentCellFinder::right(int index) const { - int next = index+1; - if (next >= (int)_positions.size()) + if (index < 0 || index >= (int)_positions.size() - 1) return -1; - if (_positions[next].first < _positions[index].first) // loop + int next = index + 1; + if (_positions[next].first < _positions[index].first) return -1; return next; } @@ -63,16 +63,17 @@ int AdjacentCellFinder::left(int index) const int AdjacentCellFinder::bottom(int index) const { + if (index < 0 || index >= (int)_positions.size()) + return -1; int increment = _dimensions; if (in_row_with_margin(index)) increment -= _markerSize; int next = index + increment; if (in_row_with_margin(next)) next -= _markerSize; - - if (next >= (int)_positions.size()) + if (next < 0 || next >= (int)_positions.size()) return -1; - if (_positions[next].first != _positions[index].first) // near anchor + if (_positions[next].first != _positions[index].first) return -1; return next; }