From db1a274bf54343605c8ac3a38a6c69f7d930192b Mon Sep 17 00:00:00 2001 From: Fedor Indutny Date: Wed, 8 May 2013 01:01:04 +0400 Subject: [PATCH] io: handle fread() errors When, occasionally, trying to read directory instead of file, `fread()` returns `EISDIR` error. And, considering, absence of error handling, `read_whole_stream()` just loops indefinitely. --- src/libcore/io.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/libcore/io.rs b/src/libcore/io.rs index 460fd60d4c5..64b69a7928b 100644 --- a/src/libcore/io.rs +++ b/src/libcore/io.rs @@ -868,9 +868,19 @@ impl Reader for *libc::FILE { assert!(buf_len >= len); let count = libc::fread(buf_p as *mut c_void, 1u as size_t, - len as size_t, *self); + len as size_t, *self) as uint; + if count < len { + match libc::ferror(*self) { + 0 => (), + _ => { + error!("error reading buffer"); + error!("%s", os::last_os_error()); + fail!(); + } + } + } - count as uint + count } } }