Subject: lib/14432: Torek stdio and gnu/lib/libstdc++ clear() method problem
To: None <gnats-bugs@gnats.netbsd.org>
From: Mark Davies <mark@mcs.vuw.ac.nz>
List: netbsd-bugs
Date: 11/02/2001 14:06:39
>Number:         14432
>Category:       lib
>Synopsis:       Torek stdio and gnu/lib/libstdc++ clear() method problem
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    lib-bug-people
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Thu Nov 01 17:07:00 PST 2001
>Closed-Date:
>Last-Modified:
>Originator:     Mark Davies
>Release:        NetBSD 1.5Y 29/10/2001
>Organization:
Dept. of Comp. Sci., Victoria Uni. of Wellington, New Zealand.
>Environment:
	
	
System: NetBSD turakirae.mcs.vuw.ac.nz 1.5Y NetBSD 1.5Y (MCS_WORKSTATION) #1: Tue Oct 30 22:54:21 NZDT 2001 mark@turakirae.mcs.vuw.ac.nz:/src/work/src/sys/arch/i386/compile/MCS_WORKSTATION i386
Architecture: i386
Machine: i386
>Description:
	
>How-To-Repeat:
	
	<how to correct or work around the problem, if known (multiple lines)>



The following little c++ program demonstrates a problem with the iostream clear
method on at least the i386 port.

#include <iostream.h>

main()
{
        char ans;

        cout << "Hello World: ";
        cin >> ans;
        cin.clear();
        cout << "Hi again: ";
        cin >> ans;
}

Compiling and running this on 1.5X, if you type CNTL-D at the first prompt,
it drops through the second and exits with the cin.clear() appearing not to
clear the EOF status on standard in. This is with the standard system g++
and the same thing happens with the 2.95.2 from pkgsrc and with the new
toolchain on 1.5Y.

This same code, compiled with various versions of g++, on Tru64 and
Solaris8 sits at the second prompt waiting for more input so its not a
general g++/libstdc++ problem but specific to NetBSD (or maybe Torek stdio
based systems).

The following little C program behaves the same as the above C++ one
(with NetBSD's behaviour being different from Solaris and Tru64)

#include <stdio.h>

main ()
{
  char ans;
  
  printf("Hello World: ");
  ans= getc(stdin);
  /* clearerr(stdin); */
  printf("Hi World: ");
  ans= getc(stdin);
  
}

If you enable the commented out clearerr(stdin) then you get consistent 
behaviour across the three UNIX systems.

Similarly the following change to the C++ program gets consistent behaviour 
across the systems:

#include <iostream.h>
#include <stdio.h>

main()
{
        char ans;

        cout << "Hello World: ";
        cin >> ans;
        cin.clear();
	clearerr(stdin);
        cout << "Hi again: ";
        cin >> ans;
}


So it appears to me that Torek stdio requires the clearerr() to clear the EOF 
status while the other stdio implementation lets you get away without doing it 
(and I have no problem with the Torek behaviour) but it also seems that 
libstdc++, when dealing with streams that are associated with stdio files is 
assuming the non Torek behaviour :-(

Interestingly the orginal little sample program works correctly on MacOS X.1 buit I couldn't see any obvious difference in the Darwin gcc source.

>Fix:

I have no idea how to fix it.  Well presumably the implementation of the clear 
method needs to work out if its dealing with a stdiostream and if so call 
clearerr() but that doesn't look at all easy to me.

cheers
mark
>Release-Note:
>Audit-Trail:
>Unformatted: