Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a bug where read(buf, len) would fail if buf was big enough and ... #4029

Merged
merged 1 commit into from
Nov 24, 2012
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion src/libcore/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -1208,6 +1208,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"),
Expand Down