From: "Erich E. Hoover" Subject: Re: [PATCH] msvcrt: Fix fscanf return when EOF is immediately after an end of line. Message-Id: Date: Thu, 17 Oct 2019 09:41:32 -0600 In-Reply-To: <5bcb4ab2-17a0-0d14-4ca0-73c4b7094991@gmail.com> References: <5bcb4ab2-17a0-0d14-4ca0-73c4b7094991@gmail.com> Thanks Piotr! I'm happy to change that, I was just using the fprintf tests as a template. Does the attached look better to you? Best, Erich On Thu, Oct 17, 2019 at 5:23 AM Piotr Caban wrote: > > Hi Erich, > > The implementation part of the patch doesn't look right. I think that it > should be fixed by %s format reporting error when there's no characters > to read. > > I'm attaching a diff (generated on top of your patch) that shows the > problems. > > Also please use only tests on strings (sscanf) or handle file creation > failure. > > Thanks, > Piotr From ce70582b693cb1e7e94e0b248cac79319fe06510 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 an empty string precedes an EOF. Signed-off-by: Erich E. Hoover --- dlls/msvcrt/scanf.h | 4 ++++ dlls/msvcrt/tests/scanf.c | 44 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/dlls/msvcrt/scanf.h b/dlls/msvcrt/scanf.h index 1058a7a911..bdb18bad0e 100644 --- a/dlls/msvcrt/scanf.h +++ b/dlls/msvcrt/scanf.h @@ -487,6 +487,10 @@ _FUNCTION_ { nch = _GETC_(file); if (width>0) width--; } + /* if we have reached the EOF and output nothing then report EOF */ + if (nch==_EOF_ && rd==0 && st==0) { + return _EOF_RET; + } /* terminate */ if (st && !suppress) *sptr = 0; } diff --git a/dlls/msvcrt/tests/scanf.c b/dlls/msvcrt/tests/scanf.c index 6d38e438c8..c4ede53eb5 100644 --- a/dlls/msvcrt/tests/scanf.c +++ b/dlls/msvcrt/tests/scanf.c @@ -22,6 +22,41 @@ #include "wine/test.h" +static void test_fscanf( void ) +{ + static const char file_name[] = "fscanf.tst"; + static const char contents[] = + "line1\n" + "line2 " + ; + char buf[1024]; + FILE *fp; + int ret; + + fp = fopen(file_name, "wb"); + ok(fp, "fp = %p\n", fp); + if(!fp) { + skip("failed to create temporary test file\n"); + return; + } + + 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 */ @@ -48,6 +83,14 @@ static void test_sscanf( void ) ret = p_sscanf(buffer, "%d", &result); ok( ret == EOF,"sscanf returns %x instead of %x\n", ret, EOF ); + ret = p_sscanf(" \t\n\n", "%s", buffer); + ok( ret == EOF, "ret = %d\n", ret ); + + buffer1[0] = 'a'; + ret = p_sscanf("test\n", "%s%c", buffer, buffer1); + ok( ret == 2, "ret = %d\n", ret ); + ok( buffer1[0] == '\n', "buffer1[0] = %d\n", buffer1[0] ); + /* check %p */ ok( p_sscanf("000000000046F170", "%p", &ptr) == 1, "sscanf failed\n" ); ok( ptr == (void *)0x46F170,"sscanf reads %p instead of %x\n", ptr, 0x46F170 ); @@ -372,6 +415,7 @@ static void test_swscanf_s(void) START_TEST(scanf) { + test_fscanf(); test_sscanf(); test_sscanf_s(); test_swscanf(); -- 2.17.1