From: "Erich E. Hoover" Subject: [PATCH] msvcrt: Fix fscanf return when EOF is immediately after an end of line. Message-Id: Date: Wed, 16 Oct 2019 16:24:44 -0600 Spotted while compiling VTK for Python under Wine. From 5dc1d1374dec2176bb27ed5bfe126cda6e603b42 Mon Sep 17 00:00:00 2001 From: "Erich E. Hoover" Date: Wed, 16 Oct 2019 16:09:16 -0600 Subject: msvcrt: Fix fscanf return when EOF is immediately after an end of line. Signed-off-by: Erich E. Hoover --- dlls/msvcrt/scanf.h | 2 +- dlls/msvcrt/tests/scanf.c | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/dlls/msvcrt/scanf.h b/dlls/msvcrt/scanf.h index 1058a7a911..4f0757e142 100644 --- a/dlls/msvcrt/scanf.h +++ b/dlls/msvcrt/scanf.h @@ -709,7 +709,7 @@ _FUNCTION_ { } format++; } - if (nch!=_EOF_) { + if (nch!=_EOF_ && nch != '\n') { _UNGETC_(nch, file); } diff --git a/dlls/msvcrt/tests/scanf.c b/dlls/msvcrt/tests/scanf.c index 6d38e438c8..c0d5aa66d1 100644 --- a/dlls/msvcrt/tests/scanf.c +++ b/dlls/msvcrt/tests/scanf.c @@ -22,6 +22,35 @@ #include "wine/test.h" +static void test_fscanf( void ) +{ + static const char file_name[] = "fprintf.tst"; + static const char contents[] = + "line1\n" + "line2\n" + ; + char buf[1024]; + FILE *fp; + int ret; + + fp = fopen(file_name, "wb"); + ret = fprintf(fp, contents); + fclose(fp); + + fp = fopen(file_name, "rb"); + ret = fscanf(fp, "%s", buf); + ok(ret == 1, "ret = %d\n", ret); + ok(strcmp(buf, "line1") == 0, "buf = %s\n", buf); + ret = fscanf(fp, "%s", buf); + ok(ret == 1, "ret = %d\n", ret); + ok(strcmp(buf, "line2") == 0, "buf = %s\n", buf); + ret = fscanf(fp, "%s", buf); + ok(ret == EOF, "ret = %d\n", ret); + fclose(fp); + + unlink(file_name); +} + static void test_sscanf( void ) { /* use function pointers to bypass gcc builtin */ @@ -372,6 +401,7 @@ static void test_swscanf_s(void) START_TEST(scanf) { + test_fscanf(); test_sscanf(); test_sscanf_s(); test_swscanf(); -- 2.17.1