]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Do not allow chrono::parse to overflow for %C [PR111162]
authorJonathan Wakely <jwakely@redhat.com>
Tue, 29 Aug 2023 12:07:21 +0000 (13:07 +0100)
committerJonathan Wakely <jwakely@redhat.com>
Fri, 1 Sep 2023 08:55:48 +0000 (09:55 +0100)
libstdc++-v3/ChangeLog:

PR libstdc++/111162
* include/bits/chrono_io.h (_Parser::Operator()): Check %C
values are in range of year::min() to year::max().
* testsuite/std/time/parse.cc: Check out of range centuries.

libstdc++-v3/include/bits/chrono_io.h
libstdc++-v3/testsuite/std/time/parse.cc

index d558802e7d8662c4cd16da9c407a9f2d854c4d53..f359571b4dba7a08f584136499f4e951f42543bf 100644 (file)
@@ -3171,7 +3171,14 @@ namespace __detail
                    {
                      auto __v = __read_signed(__num ? __num : 2);
                      if (!__is_failed(__err))
-                       __century = __v * 100;
+                       {
+                         int __cmin = (int)year::min() / 100;
+                         int __cmax = (int)year::max() / 100;
+                         if (__cmin <= __v && __v <= __cmax)
+                           __century = __v * 100;
+                         else
+                           __century = -2; // This prevents guessing century.
+                       }
                    }
                  else if (__mod == 'E')
                    {
index 9b36c5d7db4cd40303f56de44475d37529fbe1b3..46eb7f28c85e79b55ff44a1a88c1721aa626c824 100644 (file)
@@ -251,6 +251,18 @@ test_errors()
   is >> parse("%H:%M %3y", y); // 61min is out of range but not needed
   VERIFY( is.eof() && ! is.fail() );
   VERIFY( y == 2010y );
+
+  is.clear();
+  is.str("328 00");
+  is >> parse("%3C %y", y); // 328 is out of range for %C (PR libstdc++/111162)
+  VERIFY( is.fail() );
+  VERIFY( y == 2010y );
+
+  is.clear();
+  is.str("-328 00");
+  is >> parse("%3C %y", y); // -328 is out of range for %C
+  VERIFY( is.fail() );
+  VERIFY( y == 2010y );
 }
 
 void