Bug 421079

Summary: Array is used before checking index
Product: [Applications] krita Reporter: Rafał Mikrut <mikrutrafal>
Component: GeneralAssignee: Krita Bugs <krita-bugs-null>
Status: RESOLVED FIXED    
Severity: normal    
Priority: NOR    
Version First Reported In: git master (please specify the git hash!)   
Target Milestone: ---   
Platform: Compiled Sources   
OS: Linux   
Latest Commit: Version Fixed/Implemented In:
Sentry Crash Report:

Description Rafał Mikrut 2020-05-05 20:19:13 UTC
``` 
  while (d->effectiveZoom > d->sliderLookup[i] - eps &&
           i < d->sliderLookup.size() - 1) i++;
```
https://invent.kde.org/kde/krita/-/blob/master/libs/widgets/KoZoomAction.cpp#L236
Comment 1 Tiar 2020-05-06 01:03:15 UTC
Git commit 10d47bd625663ed576ccc527f3219e8aa133995f by Agata Cacko.
Committed on 06/05/2020 at 00:44.
Pushed by tymond into branch 'master'.

Change order in conditions in while() in KoZoomAction

Before this commit, while loop would first check the value of the
array in a specific index and only later if the index is in range.
That shouldn't lead to any problems per se unless the array is very
short, because it always starts from the other end of the array and
the loop ends before the index can fall out of range.
However it seems like it triggered some statis analysis tool and it
might potentially deceive a future programmer if some of the external
conditions (like the starting index) changes.
This commit reverses the order of the conditions so the index is checked
first.

M  +4    -3    libs/widgets/KoZoomAction.cpp

https://invent.kde.org/kde/krita/commit/10d47bd625663ed576ccc527f3219e8aa133995f
Comment 2 Halla Rempt 2020-05-06 09:48:27 UTC
Git commit 6df18b5fea8ea63f7a5bdd27630176c7a4185f67 by Boudewijn Rempt, on behalf of Agata Cacko.
Committed on 06/05/2020 at 09:47.
Pushed by rempt into branch 'krita/4.3'.

Change order in conditions in while() in KoZoomAction

Before this commit, while loop would first check the value of the
array in a specific index and only later if the index is in range.
That shouldn't lead to any problems per se unless the array is very
short, because it always starts from the other end of the array and
the loop ends before the index can fall out of range.
However it seems like it triggered some statis analysis tool and it
might potentially deceive a future programmer if some of the external
conditions (like the starting index) changes.
This commit reverses the order of the conditions so the index is checked
first.
(cherry picked from commit 10d47bd625663ed576ccc527f3219e8aa133995f)

M  +4    -3    libs/widgets/KoZoomAction.cpp

https://invent.kde.org/kde/krita/commit/6df18b5fea8ea63f7a5bdd27630176c7a4185f67