From 5b6b11cb6572f0af71e8afff0c4f3d895addce34 Mon Sep 17 00:00:00 2001 From: lmiskiew Date: Mon, 17 Dec 2018 02:01:36 +0100 Subject: [PATCH] Fix panic in fwrite --- src/header/stdio/mod.rs | 7 +++++-- tests/Makefile | 1 + tests/expected/stdio/fputs.stderr | 0 tests/expected/stdio/fputs.stdout | 0 tests/stdio/fputs.c | 11 +++++++++++ tests/stdio/fputs.out | 1 + tests/stdio/fwrite.c | 17 +++++++++++++++-- tests/stdio/fwrite.out | Bin 12 -> 13 bytes 8 files changed, 33 insertions(+), 4 deletions(-) create mode 100644 tests/expected/stdio/fputs.stderr create mode 100644 tests/expected/stdio/fputs.stdout create mode 100644 tests/stdio/fputs.c create mode 100644 tests/stdio/fputs.out diff --git a/src/header/stdio/mod.rs b/src/header/stdio/mod.rs index e313849e05..4a17e1d7f7 100644 --- a/src/header/stdio/mod.rs +++ b/src/header/stdio/mod.rs @@ -562,11 +562,14 @@ pub unsafe extern "C" fn funlockfile(file: *mut FILE) { pub unsafe extern "C" fn fwrite( ptr: *const c_void, size: size_t, - count: size_t, + nitems: size_t, stream: *mut FILE, ) -> size_t { + if size == 0 || nitems == 0 { + return 0 + } let mut stream = (*stream).lock(); - let buf = slice::from_raw_parts_mut(ptr as *mut u8, size as usize * count as usize); + let buf = slice::from_raw_parts_mut(ptr as *mut u8, size as usize * nitems as usize); let mut written = 0; while written < buf.len() { match stream.write(&mut buf[written..]) { diff --git a/tests/Makefile b/tests/Makefile index efdf091019..ae16621e8b 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -23,6 +23,7 @@ EXPECT_BINS=\ stdio/all \ stdio/buffer \ stdio/fgets \ + stdio/fputs \ stdio/fread \ stdio/freopen \ stdio/fseek \ diff --git a/tests/expected/stdio/fputs.stderr b/tests/expected/stdio/fputs.stderr new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/expected/stdio/fputs.stdout b/tests/expected/stdio/fputs.stdout new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/stdio/fputs.c b/tests/stdio/fputs.c new file mode 100644 index 0000000000..34ac5f361a --- /dev/null +++ b/tests/stdio/fputs.c @@ -0,0 +1,11 @@ +#include +#include +#include + +int main(int argc, char ** argv) { + FILE *f = fopen("stdio/fputs.out", "w"); + char *in = "Hello World!"; + fputs(in, f); // calls fwrite, helpers::fwritex, internal::to_write and internal::stdio_write + fclose(f); + return 0; +} diff --git a/tests/stdio/fputs.out b/tests/stdio/fputs.out new file mode 100644 index 0000000000..c57eff55eb --- /dev/null +++ b/tests/stdio/fputs.out @@ -0,0 +1 @@ +Hello World! \ No newline at end of file diff --git a/tests/stdio/fwrite.c b/tests/stdio/fwrite.c index da172d5b58..85da119054 100644 --- a/tests/stdio/fwrite.c +++ b/tests/stdio/fwrite.c @@ -4,8 +4,21 @@ int main(int argc, char ** argv) { FILE *f = fopen("stdio/fwrite.out", "w"); - char *in = "Hello World!"; - fputs(in, f); // calls fwrite, helpers::fwritex, internal::to_write and internal::stdio_write + const char ptr[] = "Hello World!"; + + if (fwrite(ptr, 0, 17, f)) { + return -1; + } + + if (fwrite(ptr, 7, 0, f)) { + return -1; + } + + if (fwrite(ptr, 0, 0, f)) { + return -1; + } + + fwrite(ptr, sizeof(ptr), 1, f); fclose(f); return 0; } diff --git a/tests/stdio/fwrite.out b/tests/stdio/fwrite.out index c57eff55ebc0c54973903af5f72bac72762cf4f4..3615520240bf84fe2471475fd53ce54de9600604 100644 GIT binary patch literal 13 UcmeZB&B@7E2+uFdNl|0~03e41J^%m! literal 12 TcmeZB&B@7E2+uFdNl^p<97zN|