readdir more carefully

This is another check against the kind of error that led to #3065