public inbox for glibc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "vincent-srcware at vinc17 dot net" <sourceware-bugzilla@sourceware.org>
To: glibc-bugs@sourceware.org
Subject: [Bug stdio/30647] New: scanf functions wrong on nan()
Date: Mon, 17 Jul 2023 15:30:06 +0000	[thread overview]
Message-ID: <bug-30647-131@http.sourceware.org/bugzilla/> (raw)

https://sourceware.org/bugzilla/show_bug.cgi?id=30647

            Bug ID: 30647
           Summary: scanf functions wrong on nan()
           Product: glibc
           Version: 2.37
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: stdio
          Assignee: unassigned at sourceware dot org
          Reporter: vincent-srcware at vinc17 dot net
  Target Milestone: ---

On a string input containing "nan()" with parentheses (possibly with
n-char-sequence), the scanf functions assume that the subject sequence is just
"nan". Note that strtod is correct, i.e. it takes the parentheses into account.

Consider the following testcase:

#include <stdio.h>
#include <stdlib.h>

static void test_strtod (const char *s)
{
  char *endptr;
  double d;

  printf ("strtod test on %s\n", s);
  d = strtod (s, &endptr);
  printf ("d = %g \"%s\"\n", d, endptr);
}

int main (void)
{
  int r;
  double a, b, c;

  test_strtod ("nan*");
  test_strtod ("nan()*");

  r = sscanf ("nan nan() 1", "%lf%lf%lf", &a, &b, &c);
  printf ("sscanf return value: %d\n", r);
  if (r >= 1)
    printf ("a = %g\n", a);
  if (r >= 2)
    printf ("b = %g\n", b);
  if (r >= 3)
    printf ("c = %g\n", c);

  r = fscanf (stdin, "%lf%lf%lf", &a, &b, &c);
  printf ("fscanf return value: %d\n", r);
  if (r >= 1)
    printf ("a = %g\n", a);
  if (r >= 2)
    printf ("b = %g\n", b);
  if (r >= 3)
    printf ("c = %g\n", c);
  return 0;
}

I get the following output with GNU libc 2.31 and 2.37 on Debian:

$ printf "nan nan() 1" | ./naninput
strtod test on nan*
d = nan "*"
strtod test on nan()*
d = nan "*"
sscanf return value: 2
a = nan
b = nan
fscanf return value: 2
a = nan
b = nan

instead of

strtod test on nan*
d = nan "*"
strtod test on nan()*
d = nan "*"
sscanf return value: 3
a = nan
b = nan
c = 1
fscanf return value: 3
a = nan
b = nan
c = 1

(as obtained with MacOS X 12.6 and Android 13).

-- 
You are receiving this mail because:
You are on the CC list for the bug.

             reply	other threads:[~2023-07-17 15:30 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-17 15:30 vincent-srcware at vinc17 dot net [this message]
2023-07-18 11:55 ` [Bug stdio/30647] " vincent-srcware at vinc17 dot net
2024-04-30 16:42 ` carlos at redhat dot com

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-30647-131@http.sourceware.org/bugzilla/ \
    --to=sourceware-bugzilla@sourceware.org \
    --cc=glibc-bugs@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).