It doesn’t throw the error because the limit on your “for” range stops it from going off the end in your first sample code. But that code is incorrect for the reasons that Tom explained:
The loops here are over the output space, not the input space. We must touch every position in the output space and not skip any. Then for each output position, we have to compute where that comes from in the input space. That is where the stride comes in. It is a fundamental mistake to use the stride in the range of the for loop.
The second set of code should work. Of course the horizontal and vertical logic should look the same.