From 3824ac481e56c94924e1440109e04885c74fc6f3 Mon Sep 17 00:00:00 2001 From: Jonathan Wakely Date: Thu, 14 Dec 2017 11:53:02 +0000 Subject: [PATCH] PR libstdc++/59568 don't use putback or update value when extraction fails PR libstdc++/59568 * include/std/complex (operator>>): Only use putback if a character was successfully extracted and only set the value if a number was successfully extracted. * testsuite/26_numerics/complex/inserters_extractors/char/59568.cc: New test. From-SVN: r255633 --- libstdc++-v3/ChangeLog | 9 + libstdc++-v3/include/std/complex | 10 +- .../inserters_extractors/char/59568.cc | 191 ++++++++++++++++++ 3 files changed, 206 insertions(+), 4 deletions(-) create mode 100644 libstdc++-v3/testsuite/26_numerics/complex/inserters_extractors/char/59568.cc diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index a02b401aa90..074c93c647b 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,12 @@ +2017-12-14 Jonathan Wakely + + PR libstdc++/59568 + * include/std/complex (operator>>): Only use putback if a character + was successfully extracted and only set the value if a number was + successfully extracted. + * testsuite/26_numerics/complex/inserters_extractors/char/59568.cc: + New test. + 2017-12-12 Jonathan Wakely PR libstdc++/83395 diff --git a/libstdc++-v3/include/std/complex b/libstdc++-v3/include/std/complex index 6342c98e88a..22107cb2264 100644 --- a/libstdc++-v3/include/std/complex +++ b/libstdc++-v3/include/std/complex @@ -493,7 +493,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x) { _Tp __re_x, __im_x; - _CharT __ch; + _CharT __ch = _CharT(); __is >> __ch; if (__ch == '(') { @@ -511,11 +511,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION else __is.setstate(ios_base::failbit); } - else + else if (__is) { __is.putback(__ch); - __is >> __re_x; - __x = __re_x; + if (__is >> __re_x) + __x = __re_x; + else + __is.setstate(ios_base::failbit); } return __is; } diff --git a/libstdc++-v3/testsuite/26_numerics/complex/inserters_extractors/char/59568.cc b/libstdc++-v3/testsuite/26_numerics/complex/inserters_extractors/char/59568.cc new file mode 100644 index 00000000000..e292e13311a --- /dev/null +++ b/libstdc++-v3/testsuite/26_numerics/complex/inserters_extractors/char/59568.cc @@ -0,0 +1,191 @@ +// Copyright (C) 2017 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +#include +#include +#include + +void +test01() +{ + std::istringstream in(" 1 (2) ( 2.0 , 0.5 ) "); + std::complex c1, c2, c3; + in >> c1 >> c2 >> c3; + VERIFY( in.good() ); + VERIFY( c1.real() == 1 && c1.imag() == 0 ); + VERIFY( c2.real() == 2 && c2.imag() == 0 ); + VERIFY( c3.real() == 2 && c3.imag() == 0.5 ); +} + +void +test02() +{ + std::istringstream in; + std::complex c(-1, -1); + const std::complex c0 = c; + + in.str("a"); + in >> c; + VERIFY( in.fail() ); + in.clear(); + VERIFY( in.get() == 'a' ); + VERIFY( c == c0 ); + + in.str(" ( ) "); + in >> c; + VERIFY( in.fail() ); + in.clear(); + VERIFY( in.get() == ')' ); + VERIFY( c == c0 ); + + in.str("(,"); + in >> c; + VERIFY( in.fail() ); + in.clear(); + VERIFY( in.get() == ',' ); + VERIFY( c == c0 ); + + in.str("(b)"); + in >> c; + VERIFY( in.fail() ); + in.clear(); + VERIFY( in.get() == 'b' ); + VERIFY( c == c0 ); + + in.str("( c)"); + in >> c; + VERIFY( in.fail() ); + in.clear(); + VERIFY( in.get() == 'c' ); + VERIFY( c == c0 ); + + in.str("(99d"); + in >> c; + VERIFY( in.fail() ); + in.clear(); + // VERIFY( in.get() == 'd' ); + VERIFY( c == c0 ); + + in.str("(99 e"); + in >> c; + VERIFY( in.fail() ); + in.clear(); + // VERIFY( in.get() == 'e' ); + VERIFY( c == c0 ); + + in.str("(99, f"); + in >> c; + VERIFY( in.fail() ); + in.clear(); + VERIFY( in.get() == 'f' ); + VERIFY( c == c0 ); + + in.str("(99, 88g"); + in >> c; + VERIFY( in.fail() ); + in.clear(); + // VERIFY( in.get() == 'g' ); + VERIFY( c == c0 ); + + in.str("(99, 88 h"); + in >> c; + VERIFY( in.fail() ); + in.clear(); + // VERIFY( in.get() == 'h' ); + VERIFY( c == c0 ); + + in.str("(99, )"); + in >> c; + VERIFY( in.fail() ); + in.clear(); + VERIFY( in.get() == ')' ); + VERIFY( c == c0 ); +} + +void +test03() +{ + // PR libstdc++/59568 + std::istringstream in; + std::complex c; + + in.str(""); + in >> c; + VERIFY( in.fail() ); + VERIFY( in.eof() ); + in.clear(); + + in.str(" "); + in >> c; + VERIFY( in.fail() ); + VERIFY( in.eof() ); + in.clear(); + + in.str("(99"); + in >> c; + VERIFY( in.fail() ); + VERIFY( in.eof() ); + in.clear(); + + in.str("(99,"); + in >> c; + VERIFY( in.fail() ); + VERIFY( in.eof() ); + in.clear(); + + in.str("(99,99"); + in >> c; + VERIFY( in.fail() ); + VERIFY( in.eof() ); + in.clear(); +} + +void +test04() +{ + // Test noskipws handling + std::istringstream in; + const char* bad_inputs[] = { + " 1", " (2)", "( 2)", "(2 )", "(2 ,3)", "(2,3 )", 0 + }; + const std::complex c0(-1, -1); + std::complex c; + for (int i = 0; bad_inputs[i]; ++i) + { + c = c0; + in.clear(); + in.str(bad_inputs[i]); + in >> std::noskipws >> c; + VERIFY( in.fail() ); + VERIFY( c == c0 ); + + in.clear(); + in.str(bad_inputs[i]); + in >> std::skipws >> c; + VERIFY( !in.fail() ); + VERIFY( c != c0 ); + } +} + +int +main() +{ + test01(); + test02(); + test03(); + test04(); +}