about summary refs log tree commit diff
path: root/src/libcore
diff options
context:
space:
mode:
authorTim Chevalier <chevalier@alum.wellesley.edu>2012-11-24 09:00:55 -0800
committerTim Chevalier <chevalier@alum.wellesley.edu>2012-11-24 09:00:55 -0800
commit626a7aa4f4cafde876d4a0ed3523a3e90ff4c066 (patch)
tree725036f5ef1d5cb86f6fdbef6af1b724bdfa5cb6 /src/libcore
parent117e5e35831ded02e71554d2e13d6300215119b0 (diff)
parentf841d43f54846111d105dedd046c9646fcce98da (diff)
Merge pull request #4029 from Dretch/ioreadfixes
Fix a bug where read(buf, len) would fail if buf was big enough and ...
Diffstat (limited to 'src/libcore')
-rw-r--r--src/libcore/io.rs25
1 files changed, 24 insertions, 1 deletions
diff --git a/src/libcore/io.rs b/src/libcore/io.rs
index 2c92e70c7d9..3c2318d2ed0 100644
--- a/src/libcore/io.rs
+++ b/src/libcore/io.rs
@@ -404,7 +404,7 @@ fn convert_whence(whence: SeekStyle) -> i32 {
 impl *libc::FILE: Reader {
     fn read(bytes: &[mut u8], len: uint) -> uint {
         do vec::as_mut_buf(bytes) |buf_p, buf_len| {
-            assert buf_len <= len;
+            assert buf_len >= len;
 
             let count = libc::fread(buf_p as *mut c_void, 1u as size_t,
                                     len as size_t, self);
@@ -1209,6 +1209,29 @@ mod tests {
     }
 
     #[test]
+    #[should_fail]
+    fn test_read_buffer_too_small() {
+        let path = &Path("tmp/lib-io-test-read-buffer-too-small.tmp");
+        // ensure the file exists
+        io::file_writer(path, [io::Create]).get();
+
+        let file = io::file_reader(path).get();
+        let mut buf = vec::from_elem(5, 0);
+        file.read(buf, 6); // this should fail because buf is too small
+    }
+
+    #[test]
+    fn test_read_buffer_big_enough() {
+        let path = &Path("tmp/lib-io-test-read-buffer-big-enough.tmp");
+        // ensure the file exists
+        io::file_writer(path, [io::Create]).get();
+
+        let file = io::file_reader(path).get();
+        let mut buf = vec::from_elem(5, 0);
+        file.read(buf, 4); // this should succeed because buf is big enough
+    }
+
+    #[test]
     fn test_write_empty() {
         let file = io::file_writer(&Path("tmp/lib-io-test-write-empty.tmp"),
                                    [io::Create]).get();