Skip to content

Commit 626a7aa

Browse files
committed
Merge pull request #4029 from Dretch/ioreadfixes
Fix a bug where read(buf, len) would fail if buf was big enough and ...
2 parents 117e5e3 + f841d43 commit 626a7aa

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

src/libcore/io.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ fn convert_whence(whence: SeekStyle) -> i32 {
404404
impl *libc::FILE: Reader {
405405
fn read(bytes: &[mut u8], len: uint) -> uint {
406406
do vec::as_mut_buf(bytes) |buf_p, buf_len| {
407-
assert buf_len <= len;
407+
assert buf_len >= len;
408408

409409
let count = libc::fread(buf_p as *mut c_void, 1u as size_t,
410410
len as size_t, self);
@@ -1208,6 +1208,29 @@ mod tests {
12081208
}
12091209
}
12101210

1211+
#[test]
1212+
#[should_fail]
1213+
fn test_read_buffer_too_small() {
1214+
let path = &Path("tmp/lib-io-test-read-buffer-too-small.tmp");
1215+
// ensure the file exists
1216+
io::file_writer(path, [io::Create]).get();
1217+
1218+
let file = io::file_reader(path).get();
1219+
let mut buf = vec::from_elem(5, 0);
1220+
file.read(buf, 6); // this should fail because buf is too small
1221+
}
1222+
1223+
#[test]
1224+
fn test_read_buffer_big_enough() {
1225+
let path = &Path("tmp/lib-io-test-read-buffer-big-enough.tmp");
1226+
// ensure the file exists
1227+
io::file_writer(path, [io::Create]).get();
1228+
1229+
let file = io::file_reader(path).get();
1230+
let mut buf = vec::from_elem(5, 0);
1231+
file.read(buf, 4); // this should succeed because buf is big enough
1232+
}
1233+
12111234
#[test]
12121235
fn test_write_empty() {
12131236
let file = io::file_writer(&Path("tmp/lib-io-test-write-empty.tmp"),

0 commit comments

Comments
 (0)