Bug 202240 - [patch] fwscanf return EOF instead of ZERO in the event of matching failure
Summary: [patch] fwscanf return EOF instead of ZERO in the event of matching failure
Status: Closed FIXED
Alias: None
Product: Base System
Classification: Unclassified
Component: kern (show other bugs)
Version: CURRENT
Hardware: Any Any
: --- Affects Many People
Assignee: Stephen J. Kiernan
URL: https://reviews.freebsd.org/D13288
Keywords: patch
Depends on:
Blocks:
 
Reported: 2015-08-11 07:11 UTC by Rajendra
Modified: 2017-12-06 21:26 UTC (History)
3 users (show)

See Also:


Attachments
Source file compares fwscanf & fscanf return values (1.15 KB, text/plain)
2015-08-11 07:11 UTC, Rajendra
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Rajendra 2015-08-11 07:11:24 UTC
Created attachment 159753 [details]
Source file compares fwscanf & fscanf return values

The function fwscanf() return value is wrong incase of invalid input character.

For example:
     int ret = fwscanf(fptr, L"j%s", buf);

The above call should return ZERO for matching failure.

Possible fix:
--- a/lib/libc/stdio/vfwscanf.c
+++ b/lib/libc/stdio/vfwscanf.c
@@ -492,7 +492,7 @@ literal:
                                goto input_failure;
                        if (wi != c) {
                                __ungetwc(wi, fp, locale);
-                               goto input_failure;
+                               goto match_failure;
                        }
                        nread++;
                        continue;

The fscanf() function behaves correctly. Compared the source of both functions confirms the return is value is wrong.

Attached a source file to reproduce the problem.
Comment 1 commit-hook freebsd_committer freebsd_triage 2017-12-06 21:12:49 UTC
A commit references this bug:

Author: stevek
Date: Wed Dec  6 21:12:24 UTC 2017
New revision: 326635
URL: https://svnweb.freebsd.org/changeset/base/326635

Log:
  The function fwscanf() return value is wrong when encountering an early
  matching failure.

  According to the Open Group documentation for fwscanf:
  "Upon successful completion, these functions shall return the number of
  successfully matched and assigned input items; this number can be zero in
  the event of an early matching failure."

  Without this change, fwscanf would return EOF in the case of an early
  matching failure, instead of the proper return value of 0.

  This change aligns fwscanf(3) with the implementation in fscanf(3).

  PR:		202240
  Submitted by:	rajendra.sy@gmail.com
  Reviewed by:	jhb, cem
  Approved by:	sjg (mentor)
  MFC after:	2 weeks
  Differential Revision:	https://reviews.freebsd.org/D13288

Changes:
  head/lib/libc/stdio/vfwscanf.c