lint: fix array indices being used before limit check

Clean up:
cppcheck found some occurrences where arrays were accessed at a given index while the check of the bounds was done afterwards.
This PR fixes this by switching access and check such that the check occurs before the access.