Silently passing wrong input for good one

Martin Sebor sebor@roguewave.com
Fri Oct 21 22:10:00 GMT 2005


Paolo Carlini wrote:
> Hi Roberto,
> 
> 
>>the program below, when compiled with GCC 4.0.2 and
>>given the input "-3e-abcd", prints
>>
>>input = `-3e-abcd'
>>good
>>f = -3
>>left = `abcd'
> 
> 
> I changed this behavior during the 3.4.x development cycle (3.3.x did
> behave like Comeau), see testcase num_get/get/char/10.cc. Basically, my
> rationale was that of matching as closely as possible the usual usage of
> scanf in C: if '-3e-' is handed to scanf, it definitely assigns -3.0 and
> the user happily proceeds.

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. Tru64 UNIX successfully extracts "-3e" and assigns -3, and IRIX
(in unbuffered mode) silently extracts the whole thing! (sscanf()
behaves more reasonably and extracts "-3e-").

I think all but the Solaris implementation are buggy. The C standard
says the the subject sequence (recognized by strtod()) is the longest
sequence of the expected form, which of the ones we saw is only "-3".

In any case, for any input function to extract "-3e-" and swallow
the "e-" part after it with no indication of an error doesn't sound
right, even if the standard required it :)

Martin



More information about the Libstdc++ mailing list