I didn't know that whiles were less normal than fors.
And though I don't object to for loops in this situation, I really had to puzzle yours out, because of the way your loop pointer is one behind the place of interest.
I believe you when you say it is segfault-free, but again I would have to puzzle at the code to be sure.