When the read returns 0, it could mean the file was trimmed or some other type of problem. In this case abort the potentially endless loop.