Bug 181153 - sscanf(3) doesn't skip whitespace properly
Summary: sscanf(3) doesn't skip whitespace properly
Status: Open
Alias: None
Product: Base System
Classification: Unclassified
Component: kern (show other bugs)
Version: Unspecified
Hardware: Any Any
: Normal Affects Only Me
Assignee: freebsd-bugs (Nobody)
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2013-08-09 01:30 UTC by Enji Cooper
Modified: 2017-12-31 22:27 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Enji Cooper freebsd_committer freebsd_triage 2013-08-09 01:30:00 UTC
The following test failure occurred:

    sscanf_whitespace: [0.001968s] Failed: /usr/src/lib/libc/tests/stdio/t_scanf.c:71: c == 'z' not met

The corresponding test code of interest is as follows:

ATF_TC(sscanf_whitespace);
ATF_TC_HEAD(sscanf_whitespace, tc)
{

        atf_tc_set_md_var(tc, "descr", "verify sscanf skips all whitespace");
}

ATF_TC_BODY(sscanf_whitespace, tc)
{
        const char str[] = "\f\n\r\t\v%z";
        char c;

        /* set of "white space" symbols from isspace(3) */
        c = 0;
        (void)sscanf(str, "%%%c", &c);
        ATF_REQUIRE(c == 'z');
}
Comment 1 Andrey A. Chernov freebsd_committer freebsd_triage 2013-08-09 02:24:20 UTC
On 09.08.2013 4:23, Garrett Cooper wrote:
> ATF_TC_BODY(sscanf_whitespace, tc)
> {
>         const char str[] = "\f\n\r\t\v%z";
>         char c;
> 
>         /* set of "white space" symbols from isspace(3) */
>         c = 0;
>         (void)sscanf(str, "%%%c", &c);
>         ATF_REQUIRE(c == 'z');
> }

I doubt this exampe should skip whitespace. POSIX says:
"A directive that is an ordinary character shall be executed as follows:
the next byte shall be read from the input and compared with the byte
that comprises the directive; if the comparison shows that they are not
equivalent, the directive shall fail, and the differing and subsequent
bytes shall remain unread."
%% is the ordinary % character and no isspace(3) skipping mentioned in
this explanation.

-- 
http://ache.vniz.net/
bitcoin:1G6ugdNY6e5jx1GVnAU2ntj2NEfmjKG85r
Comment 2 Enji Cooper freebsd_committer freebsd_triage 2013-08-09 03:03:10 UTC
On Thu, Aug 8, 2013 at 6:24 PM, Andrey Chernov <ache@freebsd.org> wrote:
> On 09.08.2013 4:23, Garrett Cooper wrote:
>> ATF_TC_BODY(sscanf_whitespace, tc)
>> {
>>         const char str[] = "\f\n\r\t\v%z";
>>         char c;
>>
>>         /* set of "white space" symbols from isspace(3) */
>>         c = 0;
>>         (void)sscanf(str, "%%%c", &c);
>>         ATF_REQUIRE(c == 'z');
>> }
>
> I doubt this exampe should skip whitespace. POSIX says:
> "A directive that is an ordinary character shall be executed as follows:
> the next byte shall be read from the input and compared with the byte
> that comprises the directive; if the comparison shows that they are not
> equivalent, the directive shall fail, and the differing and subsequent
> bytes shall remain unread."
> %% is the ordinary % character and no isspace(3) skipping mentioned in
> this explanation.

What about this line [*]?

"
A directive composed of one or more white-space characters is executed
by reading input until no more valid input can be read, or up to the
first byte which is not a white-space character which remains unread.
"

I think this is what the NetBSD test developer was shooting for originally.

FWIW I did print out the value previously and it was (char)0, or '\0'.

Thanks!

* http://pubs.opengroup.org/onlinepubs/7908799/xsh/fscanf.html
Comment 3 Andrey A. Chernov freebsd_committer freebsd_triage 2013-08-09 03:15:55 UTC
On 09.08.2013 6:03, Garrett Cooper wrote:
>> I doubt this exampe should skip whitespace. POSIX says:
>> "A directive that is an ordinary character shall be executed as follows:
>> the next byte shall be read from the input and compared with the byte
>> that comprises the directive; if the comparison shows that they are not
>> equivalent, the directive shall fail, and the differing and subsequent
>> bytes shall remain unread."
>> %% is the ordinary % character and no isspace(3) skipping mentioned in
>> this explanation.
> 
> What about this line [*]?
> 
> "
> A directive composed of one or more white-space characters is executed
> by reading input until no more valid input can be read, or up to the
> first byte which is not a white-space character which remains unread.
> "
> 
> I think this is what the NetBSD test developer was shooting for originally.
> 
> FWIW I did print out the value previously and it was (char)0, or '\0'.
> 
> Thanks!
> 
> * http://pubs.opengroup.org/onlinepubs/7908799/xsh/fscanf.html
> 

I see another line here:
http://pubs.opengroup.org/onlinepubs/9699919799/functions/sscanf.html
(2013 Edition)

"A directive that is a conversion specification defines a set of
matching input sequences, as described below for each conversion
character. A conversion specification shall be executed in the following
steps.
Input white-space characters (as specified by isspace) shall be skipped,
unless the conversion specification includes a [, c, C, or n conversion
specifier."

Which is clearly not our case because %% is not a conversion, just
ordinary character. And don't see there line your mentioned.
Probably there still are skipping bugs with other %-modifiers, I don't
inspect it carefully, but not with %%.

-- 
http://ache.vniz.net/
bitcoin:1G6ugdNY6e5jx1GVnAU2ntj2NEfmjKG85r
Comment 4 Eitan Adler freebsd_committer freebsd_triage 2017-12-31 07:59:43 UTC
For bugs matching the following criteria:

Status: In Progress Changed: (is less than) 2014-06-01

Reset to default assignee and clear in-progress tags.

Mail being skipped