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
Unknown Object (File)
Mon, Apr 8, 1:39 AM
Unknown Object (File)
Sun, Apr 7, 11:52 PM
Unknown Object (File)
Feb 10 2024, 6:26 PM
Unknown Object (File)
Jan 21 2024, 2:57 PM
Unknown Object (File)
Jan 16 2024, 2:42 AM
Unknown Object (File)
Dec 23 2023, 10:18 AM
Unknown Object (File)
Nov 27 2023, 7:53 AM
Unknown Object (File)
Nov 26 2023, 6:56 AM

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.