PR libstdc++/59568 fix error handling for std::complex stream extraction
[gcc.git] / libstdc++-v3 / include / std / complex
index bd8b09d84f0d99b1225e72bcf5639f4478f640ed..bfe10347bd3788b12e701fc68b3a6216a0cc7925 100644 (file)
@@ -492,31 +492,52 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     basic_istream<_CharT, _Traits>&
     operator>>(basic_istream<_CharT, _Traits>& __is, complex<_Tp>& __x)
     {
-      _Tp __re_x, __im_x;
+      bool __fail = true;
       _CharT __ch;
-      __is >> __ch;
-      if (__ch == '(')
+      if (__is >> __ch)
        {
-         __is >> __re_x >> __ch;
-         if (__ch == ',')
+         if (_Traits::eq(__ch, __is.widen('(')))
            {
-             __is >> __im_x >> __ch;
-             if (__ch == ')')
-               __x = complex<_Tp>(__re_x, __im_x);
-             else
-               __is.setstate(ios_base::failbit);
+             _Tp __u;
+             if (__is >> __u >> __ch)
+               {
+                 const _CharT __rparen = __is.widen(')');
+                 if (_Traits::eq(__ch, __rparen))
+                   {
+                     __x = __u;
+                     __fail = false;
+                   }
+                 else if (_Traits::eq(__ch, __is.widen(',')))
+                   {
+                     _Tp __v;
+                     if (__is >> __v >> __ch)
+                       {
+                         if (_Traits::eq(__ch, __rparen))
+                           {
+                             __x = complex<_Tp>(__u, __v);
+                             __fail = false;
+                           }
+                         else
+                           __is.putback(__ch);
+                       }
+                   }
+                 else
+                   __is.putback(__ch);
+               }
            }
-         else if (__ch == ')')
-           __x = __re_x;
          else
-           __is.setstate(ios_base::failbit);
-       }
-      else
-       {
-         __is.putback(__ch);
-         __is >> __re_x;
-         __x = __re_x;
+           {
+             __is.putback(__ch);
+             _Tp __u;
+             if (__is >> __u)
+               {
+                 __x = __u;
+                 __fail = false;
+               }
+           }
        }
+      if (__fail)
+       __is.setstate(ios_base::failbit);
       return __is;
     }
 
@@ -1941,6 +1962,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
 inline namespace literals {
 inline namespace complex_literals {
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wliteral-suffix"
 #define __cpp_lib_complex_udls 201309
 
   constexpr std::complex<float>
@@ -1967,6 +1990,7 @@ inline namespace complex_literals {
   operator""il(unsigned long long __num)
   { return std::complex<long double>{0.0L, static_cast<long double>(__num)}; }
 
+#pragma GCC diagnostic pop
 } // inline namespace complex_literals
 } // inline namespace literals