@ page 934 line 31304 section fscanf objection [gwc fscanf return]
Problem:
Edition of Specification (Year): 2008
Defect code : 1. Error
In austin-group-l 11809, Vincent Lefèvre identified a conflict with
the C Standard:
---- begin quote ----
I think there is a contradiction between POSIX.1-2008 and the
ISO C standard concerning fscanf when an input failure occurs
after the first conversion.
The ISO C standard (at least N1124 and N1336) says:
7.19.6.2 The fscanf function
4 The fscanf function executes each directive of the format in turn.
If a directive fails, as detailed below, the function returns.
Failures are described as input failures (due to the occurrence
of an encoding error or the unavailability of input characters),
or matching failures (due to inappropriate input).
16 The fscanf function returns the value of the macro EOF if an
input failure occurs before any conversion. Otherwise, the
function returns the number of input items assigned, which can
be fewer than provided for, or even zero, in the event of an
early matching failure.
For instance, let us consider
n = fscanf (stdin, "%d %d", &x, &y);
where an input failure occurs *after* the first %d conversion (and
before the second one). According to the C standard, n should be
equal to 1 because
* the input failure occurred *after* the first conversion,
* 1 input item (exactly) has been assigned.
However POSIX.1-2008 says (page 934):
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 any error occurs, EOF shall be returned, and errno shall be set to
indicate the error. If a read error occurs, the error indicator for
the stream shall be set.
I wonder what POSIX really means by "If any error occurs, EOF shall be
returned [...]". I suppose this means "input failure" in C (as opposed
to "matching failure"). But, as shown above, the C standard does the
difference between these two kinds of failure only when the failure
occurs before the first conversion (indeed one gets EOF in case of an
input failure, and 0 in case of a matching failure).
---- end quote ----
An interpretation should be issued stating that POSIX defers to the
C Standard here, and conforming implementations must behave as
described in the C Standard. The POSIX text should be corrected in
TC1 to remove the conflict.
During discussions it emerged that, by a strict reading, the
phrase "before any conversion" is ambiguous, although the intention
is clear (at least, it is to native English speakers). It was
proposed to use the phrase "before the first successful conversion"
instead, but this has a couple of problems:
1. If the error occurs before the first conversion, then that
conversion never happens and cannot be thought of as "successful".
2. Even if the word "successful" is removed, it is still not clear
whether the error has to occur before reading the first byte of
input that would be used in the conversion, or if the requirement
also applies after at least one such byte has been read but with
more bytes needed to finish the conversion.
I propose to use "without any successful conversions having been
made" instead.
Action:
Change
"If the input ends before the first matching failure or conversion,
EOF shall be returned. If any error occurs, EOF shall be returned,
[CX]and errno shall be set to indicate the error[/CX]. If a read
error occurs, the error indicator for the stream shall be set."
to
"If the input ends without any successful conversions having been
made, and without a matching failure having occurred, EOF shall be
returned. If an error occurs without any successful conversions
having been made, and without a matching failure having occurred,
EOF shall be returned [CX]and errno shall be set to indicate the
error[/CX]. If a read error occurs, the error indicator for the
stream shall be set."
Make the same change to the fwscanf() page (P988 L33162).
|