public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
* [committed] libstdc++: Handle invalid values in std::chrono pretty printers
@ 2023-08-11 14:03 Jonathan Wakely
  0 siblings, 0 replies; only message in thread
From: Jonathan Wakely @ 2023-08-11 14:03 UTC (permalink / raw)
  To: libstdc++, gcc-patches

Tested x86_64-linux, Pushed to trunk. I'll backport this to gcc-13 too.

-- >8 --

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              | 7 ++++++-
 libstdc++-v3/testsuite/libstdc++-prettyprinters/chrono.cc | 7 +++++++
 2 files changed, 13 insertions(+), 1 deletion(-)

diff --git a/libstdc++-v3/python/libstdcxx/v6/printers.py b/libstdc++-v3/python/libstdcxx/v6/printers.py
index b4c427d487c..0187c4b60e6 100644
--- a/libstdc++-v3/python/libstdcxx/v6/printers.py
+++ b/libstdc++-v3/python/libstdcxx/v6/printers.py
@@ -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':
diff --git a/libstdc++-v3/testsuite/libstdc++-prettyprinters/chrono.cc b/libstdc++-v3/testsuite/libstdc++-prettyprinters/chrono.cc
index b5314e025cc..9aa284aea2f 100644
--- a/libstdc++-v3/testsuite/libstdc++-prettyprinters/chrono.cc
+++ b/libstdc++-v3/testsuite/libstdc++-prettyprinters/chrono.cc
@@ -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} } }
 
-- 
2.41.0


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-08-11 14:03 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-08-11 14:03 [committed] libstdc++: Handle invalid values in std::chrono pretty printers Jonathan Wakely

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).