Page MenuHomeFreeBSD

fwscanf returns EOF instead of ZERO in the event of matching failure

Authored by stevek on Nov 28 2017, 9:27 PM.



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, however it currently returns EOF.

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

Test Plan

See test details attached in Bugzilla.

Diff Detail

rS FreeBSD src repository
Automatic diff as part of commit; lint not applicable.
Automatic diff as part of commit; unit tests not applicable.

Event Timeline

stevek created this revision.Nov 28 2017, 9:27 PM
imp added a comment.Nov 28 2017, 9:35 PM

The Open Group says at


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. If the input ends before the first matching failure or conversion, EOF shall be returned. If a read error occurs, the error indicator for the stream is set, EOF shall be returned, [CX] [Option Start] and errno shall be set to indicate the error. [Option End]

It's unclear which case your use-case falls into. Can you clarify?

jhb added a subscriber: jhb.Nov 28 2017, 9:49 PM

This code is when the first non-matching character in the format string is encountered. It is equivalent to this code in fscanf:

                        if (fp->_r <= 0 && __srefill(fp))
                                goto input_failure;
                        if (*fp->_p != c)
                                goto match_failure;
                        fp->_r--, fp->_p++;

As the PR noted, this change just aligns the code with fscanf(). The difference is probably that for an early matching failure it is returning EOF instead of 0. That is, suppose I do 'scanf("ab%c", &c)' when the input string is '123'. When 'a' doesn't match '1', that is an early input failure and should return 0. However, for fwscanf() it is jumping to input_failure() which will see that 'nassigned' is zero and will return EOF instead.

jhb accepted this revision.Nov 28 2017, 9:51 PM

My only other thought is it would be nice to add a test case for this.

This revision is now accepted and ready to land.Nov 28 2017, 9:51 PM
cem accepted this revision.Nov 28 2017, 10:55 PM
stevek added a comment.Dec 1 2017, 7:32 PM
In D13288#277164, @jhb wrote:

My only other thought is it would be nice to add a test case for this.

Sure, I'll look at adding a test case for this. Good idea.

This revision was automatically updated to reflect the committed changes.