]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Make chrono::parse fail for bad %z [PR125369]
authorJonathan Wakely <jwakely@redhat.com>
Mon, 18 May 2026 14:44:21 +0000 (15:44 +0100)
committerJonathan Wakely <redi@gcc.gnu.org>
Tue, 19 May 2026 08:42:35 +0000 (09:42 +0100)
The chrono parsing code failed to check for errors when parsing input to
match %z. The expected input is [+-]hh[mm] but if we read less than two
valid digits for the hh or mm parts we didn't set failbit in the stream,
and used the -1 error values returned for each bad digit in the offset
value. This resulted in a "successful" parse that produced a value like
-11h or -11min for the time zone offset.

libstdc++-v3/ChangeLog:

PR libstdc++/125369
* include/bits/chrono_io.h (__detail::_Parser::operator()):
Check for errors when parsing digits for a %z format.
* testsuite/std/time/parse/125369.cc: New test.

Reviewed-by: Tomasz KamiƄski <tkaminsk@redhat.com>
libstdc++-v3/include/bits/chrono_io.h
libstdc++-v3/testsuite/std/time/parse/125369.cc [new file with mode: 0644]

index 1fe50ad79e0543469520f3705236b5c54afdbdf7..a8b73f1569e63136176f4bc5bf689224455d98da 100644 (file)
@@ -5234,8 +5234,12 @@ namespace __detail
                      else
                        {
                          // Read hh
-                         __hh = 10 * _S_try_read_digit(__is, __err);
-                         __hh += _S_try_read_digit(__is, __err);
+                         auto __d1 = _S_try_read_digit(__is, __err);
+                         auto __d2 = _S_try_read_digit(__is, __err);
+                         if (__d1 >= 0 && __d2 >= 0) [[likely]]
+                           __hh = 10 * __d1 + __d2;
+                         else
+                           __err |= ios_base::failbit;
                        }
 
                      if (__is_failed(__err))
@@ -5269,8 +5273,12 @@ namespace __detail
                      int_least32_t __mm = 0;
                      if (__read_mm)
                        {
-                         __mm = 10 * _S_try_read_digit(__is, __err);
-                         __mm += _S_try_read_digit(__is, __err);
+                         auto __d1 = _S_try_read_digit(__is, __err);
+                         auto __d2 = _S_try_read_digit(__is, __err);
+                         if (__d1 >= 0 && __d2 >= 0) [[likely]]
+                           __mm = 10 * __d1 + __d2;
+                         else
+                           __err |= ios_base::failbit;
                        }
 
                      if (!__is_failed(__err))
diff --git a/libstdc++-v3/testsuite/std/time/parse/125369.cc b/libstdc++-v3/testsuite/std/time/parse/125369.cc
new file mode 100644 (file)
index 0000000..719c56d
--- /dev/null
@@ -0,0 +1,65 @@
+// { dg-do run { target c++20 } }
+
+#include <chrono>
+#include <sstream>
+#include <testsuite_hooks.h>
+
+using namespace std::chrono;
+
+const std::string input = "2026-05-18 15:26:48 ";
+const std::string fmt = "%F %T %";
+sys_seconds ss;
+minutes offset{};
+std::string abbrev;
+
+void
+test_parse_z()
+{
+  for (auto suffix : {"Z", "+", "+Z", "-A", "+1Z", "+010", "+010Z"})
+  {
+    std::istringstream in{input + suffix};
+    from_stream(in, (fmt + "z").c_str(), ss, &abbrev, &offset);
+
+    VERIFY( in.fail() );
+    VERIFY( ss == sys_seconds(0s) );
+    VERIFY( offset == 0min );
+    VERIFY( abbrev.empty() );
+  }
+}
+
+void
+test_parse_Ez()
+{
+  for (auto suffix : {"Z", "+", "-", "+01:", "+01:X"})
+  {
+    std::istringstream in{input + suffix};
+    from_stream(in, (fmt + "Ez").c_str(), ss, &abbrev, &offset);
+
+    VERIFY( in.fail() );
+    VERIFY( ss == sys_seconds(0s) );
+    VERIFY( offset == 0min );
+    VERIFY( abbrev.empty() );
+  }
+}
+
+void
+test_parse_Oz()
+{
+  for (auto suffix : {"Z", "+", "-", "+01:", "+01:X"})
+  {
+    std::istringstream in{input + suffix};
+    from_stream(in, (fmt + "Oz").c_str(), ss, &abbrev, &offset);
+
+    VERIFY( in.fail() );
+    VERIFY( ss == sys_seconds(0s) );
+    VERIFY( offset == 0min );
+    VERIFY( abbrev.empty() );
+  }
+}
+
+int main()
+{
+  test_parse_z();
+  test_parse_Ez();
+  test_parse_Oz();
+}