This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Silently passing wrong input for good one


Paolo Carlini wrote:
Hi Martin,


FWIW, the scanf() behavior isn't quite so rosy :)

The AIX implementation of sscanf() fails on "-3e-abcd" (although
scanf(), at least in unbuffered mode, behaves close to what you
describe).

Some of the other implementations of scanf() I tried (e.g., Solaris)
extract "-3" but leave the "e-abcd" part on the stream. Others (such
as HP-UX or Linux), extract all of "-3e-" with no error and assign
-3.


Well, thanks for the plethora of details. A couple of punctualizations:

1- As you may easily imagine, for us is an high priority consistency
with glibc's "interpretation" of scanf, more than other libc
implementations. Which, anyway, I consider pretty right at least as far
as the C++ "as if" behavior is concerned: it returns 1 and assigns the
value. All the implementations you mentioned agree about that.

2- On the C++ front of the issue, there are the specific constraints due
to the fact that the facet gets an input_iterator. I think this easily
rules out many forms of "backtracking" (i.e., the returned iterator
pointing to 'e').

I suspect it wouldn't be easy in the default case (istreambuf_iterator) although the charT* specializations might be able to be more robust. If the default implementation can't backtrack it should probably set failbit since it consumed a sequence that would (or should) cause scanf to report an input failure (I'm assuming you agree that "-3e-" is not a valid subject sequence).

The point I'm trying to make is that even if glibc swallows the extra
characters (and even if the C standard required it to do so, which I
don't believe is the case(*)), I wouldn't consider such behavior (or
requirement) desirable or reasonable, and thus wouldn't expect a C++
implementation to try to provide compatibility with it.

Btw., I also checked the behavior of strtod() in whose terms scanf()
is described and it is more reasonable and consistent (i.e., strtod()
extracts "-3" leaving the "e-abcd" alone). It might be worthwhile to
bring this inconsistency to the attention of the C committee.

Martin

(*) See EXAMPLE 3 on page 287 where the string "100e" is explicitly
mentioned as failing to match "%f".


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]