Bug 202240

Summary: [patch] fwscanf return EOF instead of ZERO in the event of matching failure
Product: Base System Reporter: Rajendra <rajendra.sy>
Component: kernAssignee: Stephen J. Kiernan <stevek>
Status: Closed FIXED    
Severity: Affects Many People CC: cem, rajendra.sy, stevek
Priority: --- Keywords: patch
Version: CURRENT   
Hardware: Any   
OS: Any   
URL: https://reviews.freebsd.org/D13288
Attachments:
Description Flags
Source file compares fwscanf & fscanf return values none

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