From 8973535fdcb9e090009e2e875ed39f22a6c57199 Mon Sep 17 00:00:00 2001 From: oddcoder Date: Wed, 3 Jun 2020 23:18:07 +0200 Subject: [PATCH] Make scanf write to string and increase match count only when a pattern is matched This is the behavior of glibc which I assume to be right --- src/header/stdio/scanf.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/header/stdio/scanf.rs b/src/header/stdio/scanf.rs index d13444e51b..8a88a31630 100644 --- a/src/header/stdio/scanf.rs +++ b/src/header/stdio/scanf.rs @@ -409,11 +409,13 @@ unsafe fn inner_scanf( let mut ptr: Option<*mut c_char> = if ignore { None } else { Some(ap.arg()) }; // While we haven't used up all the width, and it matches + let mut data_stored = false; while width.map(|w| w > 0).unwrap_or(true) && !invert == matches.contains(&byte) { if let Some(ref mut ptr) = ptr { **ptr = byte as c_char; *ptr = ptr.offset(1); + data_stored = true; } r.commit(); // Decrease the width, and read a new character unless the width is 0 @@ -426,8 +428,8 @@ unsafe fn inner_scanf( } } - if let Some(ptr) = ptr { - *ptr = 0; + if data_stored { + *ptr.unwrap() = 0; matched += 1; } }