From 99b2de85b5f71ad7bfd57bda5f0fb3b595b2b94f Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Mon, 26 Nov 2018 22:08:29 +0100 Subject: Dive list: fix off-by-two bug in DiveTripModel Commit 911edfca712a046944de6d033cc4b8dd50cedfc3 changed the dive list on desktop to update positions of trips when adding/removing dives. A very unlikely case, but necessary for consistency. For a trip to be moveable down, its index has to be one-less than the maximum index, which is "items - 1". The code was doubly wrong: it forget the "1" and checked for less-or-equal instead less-than. Thus this was effectively an off-by-two error. Fix it. Signed-off-by: Berthold Stoeger --- qt-models/divetripmodel.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/qt-models/divetripmodel.cpp b/qt-models/divetripmodel.cpp index ae7641ba2..7a76ffbdb 100644 --- a/qt-models/divetripmodel.cpp +++ b/qt-models/divetripmodel.cpp @@ -696,12 +696,13 @@ void DiveTripModel::topLevelChanged(int idx) // If that didn't change, try to move forward if (newIdx == idx) { - while (newIdx <= (int)items.size() && !dive_or_trip_less_than(items[idx].d_or_t, items[newIdx + 1].d_or_t)) + ++newIdx; + while (newIdx < (int)items.size() && !dive_or_trip_less_than(items[idx].d_or_t, items[newIdx].d_or_t)) ++newIdx; } // If index changed, move items - if (newIdx != idx) { + if (newIdx != idx && newIdx != idx + 1) { beginMoveRows(QModelIndex(), idx, idx, QModelIndex(), newIdx); moveInVector(items, idx, idx + 1, newIdx); endMoveRows(); -- cgit v1.2.3-70-g09d2