Page MenuHomeFreeBSD

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

Authored by stevek on Nov 28 2017, 9:27 PM.
Tags
None
Referenced Files
F105878761: D13288.id35935.diff
Sun, Dec 22, 12:45 AM
Unknown Object (File)
Thu, Dec 19, 12:07 PM
Unknown Object (File)
Mon, Nov 25, 12:11 AM
Unknown Object (File)
Sat, Nov 23, 10:55 AM
Unknown Object (File)
Nov 21 2024, 9:15 PM
Unknown Object (File)
Nov 19 2024, 5:19 PM
Unknown Object (File)
Nov 17 2024, 10:30 AM
Unknown Object (File)
Nov 14 2024, 9:00 PM

Details

Summary

https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=202240

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

Repository
rS FreeBSD src repository - subversion
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

The Open Group says at http://pubs.opengroup.org/onlinepubs/009695399/functions/fwscanf.html

RETURN VALUE

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?

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

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

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.

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
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.