From d7d3e008672d450d2ddda6a79f85cdb29147a1e4 Mon Sep 17 00:00:00 2001 From: oddcoder Date: Sat, 23 May 2020 13:20:52 +0200 Subject: [PATCH] Fix off-by-1 error in vfscanf Scanf function requires look ahead to function properly, In case of scanning from a buffer that will not be an issue, but in our case we are reading from file, so lookaheads needs to be undone (via lseek) in our case. The only problem here is that if we opened a file that doesn't support lseek such as many of the file /dev/* --- src/header/stdio/mod.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/header/stdio/mod.rs b/src/header/stdio/mod.rs index 260f6e5cde..375409c2e0 100644 --- a/src/header/stdio/mod.rs +++ b/src/header/stdio/mod.rs @@ -1041,8 +1041,12 @@ pub unsafe extern "C" fn vsprintf(s: *mut c_char, format: *const c_char, ap: va_ #[no_mangle] pub unsafe extern "C" fn vfscanf(file: *mut FILE, format: *const c_char, ap: va_list) -> c_int { - let mut file = (*file).lock(); - scanf::scanf(&mut *file, format, ap) + let ret = { + let mut file = (*file).lock(); + scanf::scanf(&mut *file, format, ap) + }; + fseeko(file, -1, SEEK_CUR); + ret } #[no_mangle]