]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Handle invalid values in std::chrono pretty printers
authorJonathan Wakely <jwakely@redhat.com>
Fri, 11 Aug 2023 11:27:58 +0000 (12:27 +0100)
committerJonathan Wakely <jwakely@redhat.com>
Fri, 11 Aug 2023 13:33:01 +0000 (14:33 +0100)
This avoids an IndexError exception when printing invalid chrono::month
or chrono::weekday values.

libstdc++-v3/ChangeLog:

* python/libstdcxx/v6/printers.py (StdChronoCalendarPrinter):
Check for out-of-range month an weekday indices.
* testsuite/libstdc++-prettyprinters/chrono.cc: Check invalid
month and weekday values.

libstdc++-v3/python/libstdcxx/v6/printers.py
libstdc++-v3/testsuite/libstdc++-prettyprinters/chrono.cc

index b4c427d487cd36726c12b216e11938eb622a11c8..0187c4b60e63f11d364e752f372f49be5161d1f2 100644 (file)
@@ -2021,11 +2021,16 @@ class StdChronoCalendarPrinter:
         if typ == 'std::chrono::day':
             return '{}'.format(int(val['_M_d']))
         if typ == 'std::chrono::month':
+            if m < 1 or m >= len(months):
+                return "%d is not a valid month" % m
             return months[m]
         if typ == 'std::chrono::year':
             return '{}y'.format(y)
         if typ == 'std::chrono::weekday':
-            return '{}'.format(weekdays[val['_M_wd']])
+            wd = val['_M_wd']
+            if wd < 0 or wd >= len(weekdays):
+                return "%d is not a valid weekday" % wd
+            return '{}'.format(weekdays[wd])
         if typ == 'std::chrono::weekday_indexed':
             return '{}[{}]'.format(val['_M_wd'], int(val['_M_index']))
         if typ == 'std::chrono::weekday_last':
index b5314e025ccefa0ef8f781b92680ee8fb10e3348..9aa284aea2f33e80dd594cd9d81e67ecbb11b147 100644 (file)
@@ -75,6 +75,13 @@ main()
   [[maybe_unused]] year_month_weekday_last donnerstag = 2017y/July/Thursday[last];
   // { dg-final { note-test donnerstag {2017y/July/Thursday[last]} } }
 
+  [[maybe_unused]] month nam(13);
+  // { dg-final { note-test nam {13 is not a valid month} } }
+  [[maybe_unused]] month nam0(0);
+  // { dg-final { note-test nam0 {0 is not a valid month} } }
+  [[maybe_unused]] weekday nawd(8);
+  // { dg-final { note-test nawd {8 is not a valid weekday} } }
+  //
   hh_mm_ss<seconds> hms(4h + 3min + 2s);
   // { dg-final { note-test hms {04:03:02} } }