]> git.lizzy.rs Git - rust.git/commitdiff
Fix a bug where read(buf, len) would fail if buf was big enough and succeed if it...
authorGareth Daniel Smith <garethdanielsmith@gmail.com>
Sat, 24 Nov 2012 15:19:51 +0000 (15:19 +0000)
committerGareth Daniel Smith <garethdanielsmith@gmail.com>
Sat, 24 Nov 2012 15:19:51 +0000 (15:19 +0000)
src/libcore/io.rs

index 2c92e70c7d99b09629ce008f801851a2605c5814..3c2318d2ed02afdab18cc117a51f51baa5861496 100644 (file)
@@ -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);
@@ -1208,6 +1208,29 @@ fn file_reader_not_exist() {
         }
     }
 
+    #[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"),