diff --git a/lib/libc/stdio/vfscanf.c b/lib/libc/stdio/vfscanf.c --- a/lib/libc/stdio/vfscanf.c +++ b/lib/libc/stdio/vfscanf.c @@ -452,7 +452,7 @@ } else if (state == haveprefix) { p--; (void) __ungetc(c, fp); - } else if (c != EOF) { + } else if (width && c != EOF) { (void) __ungetc(c, fp); } return (p - buf); diff --git a/lib/libc/stdio/vfwscanf.c b/lib/libc/stdio/vfwscanf.c --- a/lib/libc/stdio/vfwscanf.c +++ b/lib/libc/stdio/vfwscanf.c @@ -435,7 +435,7 @@ } else if (state == haveprefix) { wcp--; __ungetwc(c, fp, locale); - } else if (c != WEOF) { + } else if (width && c != WEOF) { __ungetwc(c, fp, locale); } return (wcp - buf); diff --git a/lib/libc/tests/stdio/sscanf_test.c b/lib/libc/tests/stdio/sscanf_test.c --- a/lib/libc/tests/stdio/sscanf_test.c +++ b/lib/libc/tests/stdio/sscanf_test.c @@ -237,6 +237,22 @@ } } +/* + * Test termination cases: non-numeric character, fixed width, EOF + */ +ATF_TC_WITHOUT_HEAD(sscanf_termination); +ATF_TC_BODY(sscanf_termination, tc) +{ + int a = 0, b = 0, c = 0; + char d = 0; + + ATF_CHECK_EQ(4, sscanf("3.1415", "%d%c%2d%d", &a, &d, &b, &c)); + ATF_CHECK_EQ(3, a); + ATF_CHECK_EQ(14, b); + ATF_CHECK_EQ(15, c); + ATF_CHECK_EQ('.', d); +} + ATF_TP_ADD_TCS(tp) { setlocale(LC_NUMERIC, "en_US.UTF-8"); @@ -245,5 +261,6 @@ ATF_TP_ADD_TC(tp, sscanf_d); ATF_TP_ADD_TC(tp, sscanf_x); ATF_TP_ADD_TC(tp, sscanf_i); + ATF_TP_ADD_TC(tp, sscanf_termination); return (atf_no_error()); } diff --git a/lib/libc/tests/stdio/swscanf_test.c b/lib/libc/tests/stdio/swscanf_test.c --- a/lib/libc/tests/stdio/swscanf_test.c +++ b/lib/libc/tests/stdio/swscanf_test.c @@ -238,6 +238,22 @@ } } +/* + * Test termination cases: non-numeric character, fixed width, EOF + */ +ATF_TC_WITHOUT_HEAD(swscanf_termination); +ATF_TC_BODY(swscanf_termination, tc) +{ + int a = 0, b = 0, c = 0; + char d = 0; + + ATF_CHECK_EQ(4, swscanf(L"3.1415", L"%d%c%2d%d", &a, &d, &b, &c)); + ATF_CHECK_EQ(3, a); + ATF_CHECK_EQ(14, b); + ATF_CHECK_EQ(15, c); + ATF_CHECK_EQ(L'.', d); +} + ATF_TP_ADD_TCS(tp) { setlocale(LC_NUMERIC, "en_US.UTF-8"); @@ -246,5 +262,6 @@ ATF_TP_ADD_TC(tp, swscanf_d); ATF_TP_ADD_TC(tp, swscanf_x); ATF_TP_ADD_TC(tp, swscanf_i); + ATF_TP_ADD_TC(tp, swscanf_termination); return (atf_no_error()); }