How can an Arduino crash?

I had the right idea back there, I think.

Build up the buffer first in a simple, clear, easy-to-understand function. Then analyze it. Your code is so confusing with flags, numbers, tests, etc. that I can't say whether or not it will work, or crash, or anything.

Don't try to do everything (build buffer, analyze, handle overflow etc.) in one long piece of confusing code. It might be OK from your point of view if it works, but you have said it doesn't.