MFC r317583: Fix some cases where an index was used before its limits check.
Obtained from: DragonFlyBSD (git 799ba435)