The following program:

#include <iostream>
#include <sstream>
#include <istream>
#include <ostream>
#include <string>
#include <stdio.h>
#include <errno.h>

int main(int argc, char* argv[]) {
    ++argv;
    for (; *argv != 0; ++argv) {
        std::cout << "Input: |" << *argv << "|\n";
        std::istringstream is(*argv);
        double d = 0;
        is >> d;
        if (!is) {
            if (is.fail()) std::cout << "  fail() after is >> d\n";
            if (is.eof())  std::cout << "  eof()  after is >> d\n";
            if (is.bad())  std::cout << "  bad()  after is >> d\n";
            is.clear();
        }
        std::cout << "  >> d -> " << d << '\n';
        std::string s;
        if (std::getline(is, s)) {
            std::cout << "  still to read: |" << s << "|\n";
        } else {
            if (is.fail()) std::cout << "  fail() after getline(is, s)\n";
            if (is.eof())  std::cout << "  eof()  after getline(is, s)\n";
            if (is.bad())  std::cout << "  bad()  after getline(is, s)\n";
        }
    }
}

compiled with g++ 4.2.4 on Linux (I've tried also various other versions on
Linux and Solaris, they all had the same behavior) has the following result:
> ./a.out "1.e x" "1.e+ x" "1.e.e. x"
Input: |1.e x|
  >> d -> 1
  still to read: | x|
Input: |1.e+ x|
  >> d -> 1
  still to read: | x|
Input: |1.e.e. x|
  >> d -> 1
  still to read: |.e. x|

I'd have expected either a failure and consuming the badly formed double, or a
success and the first "e" remaining in the input.  I've tried the same program
with Sun CC on Linux and Solaris and with IBM xlC, I get the first behavior I
expected:
> ./a.out "1.e x" "1.e+ x" "1.e.e. x"
Input: |1.e x|
  fail() after is >> d
  >> d -> 0
  still to read: | x|
Input: |1.e+ x|
  fail() after is >> d
  >> d -> 0
  still to read: | x|
Input: |1.e.e. x|
  fail() after is >> d
  >> d -> 0
  still to read: |.e. x|


-- 
           Summary: Handling of badly formatted double
           Product: gcc
           Version: 4.2.4
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: jm at bourguet dot org


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=37160

Reply via email to