public inbox for libstdc++-cvs@sourceware.org
help / color / mirror / Atom feed
From: Jonathan Wakely <redi@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org
Subject: [gcc r13-7711] libstdc++: Handle invalid values in std::chrono pretty printers
Date: Fri, 11 Aug 2023 15:36:56 +0000 (GMT)	[thread overview]
Message-ID: <20230811153656.2D06A3858D32@sourceware.org> (raw)

https://gcc.gnu.org/g:5993775fa3a4a1c87d7dd67ae8f77a98cc4ff91e

commit r13-7711-g5993775fa3a4a1c87d7dd67ae8f77a98cc4ff91e
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Fri Aug 11 12:27:58 2023 +0100

    libstdc++: Handle invalid values in std::chrono pretty printers
    
    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.
    
    (cherry picked from commit c19b542a177b7b65b013e535ae9f384352808d11)

Diff:
---
 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 b4c427d487cd..0187c4b60e63 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 b5314e025cce..9aa284aea2f3 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} } }

                 reply	other threads:[~2023-08-11 15:36 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230811153656.2D06A3858D32@sourceware.org \
    --to=redi@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    --cc=libstdc++-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).