From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id B9C3E385C8B0 for ; Fri, 11 Aug 2023 14:03:37 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B9C3E385C8B0 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1691762617; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=K2bhtZ/x1v/0O9qQ1oV8oIUaH1wkxk5jC6BRLGItnc4=; b=DJdMOhVr/wuVwCSk0zo8c9sTYHuCJWuazNR1WH5O33UVCyouphl4GBsfJ7yRlysh34uOL2 yeTwpwf/snnck43gnyhQorq/Qjkw8eIpUOPedd5RvejNG1Mk40EKEabF+KayIpQJSu5tOq oR5D5Lyi8p+OsPG+rc+LgIvOcFax5D8= Received: from mimecast-mx02.redhat.com (66.187.233.73 [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-633-g0wuB1cANJq6uCR53ummlg-1; Fri, 11 Aug 2023 10:03:33 -0400 X-MC-Unique: g0wuB1cANJq6uCR53ummlg-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 909523813F45; Fri, 11 Aug 2023 14:03:33 +0000 (UTC) Received: from localhost (unknown [10.42.28.56]) by smtp.corp.redhat.com (Postfix) with ESMTP id 575191121314; Fri, 11 Aug 2023 14:03:33 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Handle invalid values in std::chrono pretty printers Date: Fri, 11 Aug 2023 15:03:12 +0100 Message-ID: <20230811140332.1390523-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-12.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE,TXREP autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: 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 hms(4h + 3min + 2s); // { dg-final { note-test hms {04:03:02} } } -- 2.41.0