This is a bit sketchy; nread can be less than zero in exceptional conditions. Better to test for nread < 0 and skip the loop.