Add a safeguard in case reading hits EOF

Fixes issue 412.