Bug 121151 - sscanf doesn't return EOF when it should
Summary: sscanf doesn't return EOF when it should
Status: CLOSED UPSTREAM
Alias: None
Product: Fedora
Classification: Fedora
Component: glibc   
(Show other bugs)
Version: rawhide
Hardware: i386
OS: Linux
medium
high
Target Milestone: ---
Assignee: Jakub Jelinek
QA Contact:
URL:
Whiteboard:
Keywords:
Depends On:
Blocks:
TreeView+ depends on / blocked
 
Reported: 2004-04-18 06:27 UTC by Alexei Podtelezhnikov
Modified: 2007-11-30 22:10 UTC (History)
1 user (show)

Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of:
Environment:
Last Closed: 2004-04-30 16:06:48 UTC
Type: ---
Regression: ---
Mount Type: ---
Documentation: ---
CRM:
Verified Versions:
Category: ---
oVirt Team: ---
RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: ---


Attachments (Terms of Use)
test.c testcase (411 bytes, text/plain)
2004-04-18 06:35 UTC, Alexei Podtelezhnikov
no flags Details

Description Alexei Podtelezhnikov 2004-04-18 06:27:29 UTC
Description of problem: 
 
sscanf does not always return EOF when the input buffer has been 
completely parsed through and '\0' is reached. I believe that in the 
program below all chk1, chk2, and chk3 should be EOFs. Instead, only 
chk3 is correct. 
 
/* sscanf()'s EOF return value problem demonstration */ 
#include<stdio.h> 
main () 
{ 
        char buff[] = "This could have been fgets()-ed\n"; 
        int chk1, chk2, chk3; 
 
        chk1 = sscanf(buff, "This could have been fgets()-ed\nplus 
some"); 
        chk2 = sscanf(buff, "This could have been fgets()-ed plus 
some"); 
        chk3 = sscanf(buff, "This could have been fgets()-ed %d"); 
 
        printf("EOF checks: %d %d %d\n", chk1, chk2, chk3); 
} 
 
 
 
Version-Release number of selected component (if applicable): 
glibc-2.3.2-27.9 
 
How reproducible: 
always 
 
Steps to Reproduce: 
1. gcc the program above 
2. run it 
3. 
   
Actual results: 
EOF checks: 0 0 -1 
 
Expected results: 
EOF checks: -1 -1 -1 
 
Additional info: 
For comparison I compiled and successfully ran the demo program the 
other OS.

Comment 1 Alexei Podtelezhnikov 2004-04-18 06:35:33 UTC
Created attachment 99512 [details]
test.c testcase

Inlining the program mangled the lines. Attaching

Comment 2 Jakub Jelinek 2004-04-19 17:10:58 UTC
#include <stdio.h>

int
main (void)
{
  int ret1, ret2, ret3, ret4, x;

  ret1 = sscanf ("foo\n", "foo\nbar");
  ret2 = sscanf ("foo\n", "foo bar");
  ret3 = sscanf ("foo\n", "foo %d", &x);
  ret4 = sscanf ("foo\n", "foo\n%d", &x);

  printf ("EOF checks: %d %d %d %d\n", ret1, ret2, ret3, ret4);

  ret1 = sscanf ("foon", "foonbar");
  ret2 = sscanf ("foon", "foo bar");
  ret3 = sscanf ("foon", "foo %d", &x);
  ret4 = sscanf ("foon", "foon%d", &x);

  printf ("EOF checks: %d %d %d %d\n", ret1, ret2, ret3, ret4);

  ret1 = sscanf ("foo ", "foo bar");
  ret2 = sscanf ("foo ", "fooxbar");
  ret3 = sscanf ("foo ", "foox%d", &x);
  ret4 = sscanf ("foo ", "foo %d", &x);

  printf ("EOF checks: %d %d %d %d\n", ret1, ret2, ret3, ret4);

  ret1 = sscanf ("foo\t", "foo\tbar");
  ret2 = sscanf ("foo\t", "foo bar");
  ret3 = sscanf ("foo\t", "foo %d", &x);
  ret4 = sscanf ("foo\t", "foo\t%d", &x);

  printf ("EOF checks: %d %d %d %d\n", ret1, ret2, ret3, ret4);

  return 0;
}

Solaris doesn't seem to be consistent when to return 0 and when EOF
either as it seems.

Comment 3 Alexei Podtelezhnikov 2004-04-19 18:41:01 UTC
SunOS 5.7, IRIX64 6.5.20m, and WinXP SP1 all correctly give:
EOF checks: -1 -1 -1 -1
EOF checks: -1 0 0 -1
EOF checks: -1 0 0 -1
EOF checks: -1 -1 -1 -1

glibc 2.3.2:
EOF checks: 0 0 -1 -1
EOF checks: -1 0 0 -1
EOF checks: 0 0 0 -1
EOF checks: 0 0 -1 -1

old cygwin 1.3.2 is even worse:
EOF checks: 0 0 0 0
EOF checks: -1 0 0 -1
EOF checks: 0 0 0 0
EOF checks: 0 0 0 0



Note You need to log in before you can comment on or make changes to this bug.