Ref T74533
I did not investigate further what are the conditions that need to be followed for the crash.
But I could see that the code is not correct, as it does not repeat the same conditions as the initial loop when building the index buffer.
The proposed solution is simply to repeat the same conditions of the first loop.