Previously, this never checked if `position + 2` was valid. This slightly reorders the loop so all indices are checked. Fixes #22163