From patchwork Fri Aug 11 14:03:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 134526 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b824:0:b0:3f2:4152:657d with SMTP id z4csp1119340vqi; Fri, 11 Aug 2023 07:05:35 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHn3nIcioc8p1IfLWP4T2vclM/3Fb9XFC5khugg8KqZCbVzyiHsN+/QbY6fNy9Xbq3YJo+U X-Received: by 2002:a05:6402:88e:b0:523:102f:3ce1 with SMTP id e14-20020a056402088e00b00523102f3ce1mr1741268edy.10.1691762735257; Fri, 11 Aug 2023 07:05:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691762735; cv=none; d=google.com; s=arc-20160816; b=mB2+Bc5KTrwno2o5VdN34+azhQIR7RfU11/Q4ZlzDP+BQb/bAvo4FBdhF/P5FWo9UO rX8FMMUa5h9gsxT9PnnLxkVSGYX1lNjyyDJekzr0+k1Rjs7VZsZ1qFQ4nRjJHFjW9Drf qSHaT9rOI0Ym87y7nUpnO2soHeJrWDlJbpWp+AibJWTwsuDPs8P6MF9yKBzwicMt946h o/ntGqi3/lLcXac0QngGrW8r/GPXyUjjE67J/e0NyDcbQ2DPrsm42S3n+To4SOcv/F7L J8jv0/eVjr8jo+f2pfECNoggHVxfxUP0nMdPyne2mWcWHTWltW+FO1ljLzk3qz6VKjOy qNhg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:to :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=K2bhtZ/x1v/0O9qQ1oV8oIUaH1wkxk5jC6BRLGItnc4=; fh=sC8lnOfyH6gL5FYlMbZ1VDz6qVPMj/3ViLJWKNUCUgQ=; b=Fr8rGgi9VgWVUxo/uksNnT4h52coOzbq4JlcXJo/e7K1i92BgkeJNeSvZpY4r8Cl33 59W+hYBvrgtpxgS1KsW1xLLEL0WIUsaydCOWsnsK0GGbcsKjwHRLx+0AvuxEQDm1XdHw Xu0vmsjyNiR5tP65pxg0wnaLyDDG+cOK+xmpmYHPmZYfUkTM8/D1Gjx4idqj3ZISW9In BBLIewLTw7nyLYMJUvHMvSeg50fQTyx1OnFmwxwqOl188K6pR0wzyK7l4gqrFjHhyZbh 4a9bz91xGLnQe1EarfAP02K4ZyM2OYpgRVR8OCzQBx2rwYf2U1OMstRDAYEJh/ZN2DmG FPVA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=gAhG+iPu; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id w18-20020a056402129200b00522bbb9c8ffsi3421182edv.195.2023.08.11.07.05.34 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Aug 2023 07:05:35 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=gAhG+iPu; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 397773858C2F for ; Fri, 11 Aug 2023 14:04:20 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 397773858C2F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1691762660; bh=K2bhtZ/x1v/0O9qQ1oV8oIUaH1wkxk5jC6BRLGItnc4=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=gAhG+iPu/l3vx0BFuYIE3wne+Ow+QCIQAvlQPdTL8waoPPKHaNUk8GEoF6uUricIo DzvCiy3h4/U6TpzlKlNNQBlvkZ4DcOAhslxKcTbj0gOuDxLw+eqRZZzdSmU8o51NZ0 I1wna7dRBnhFIlTuyMFqIUXUjDFA8zviCC5vlUAg= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org 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 468D8385C6E0 for ; Fri, 11 Aug 2023 14:03:36 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 468D8385C6E0 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) 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 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=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jonathan Wakely via Gcc-patches From: Jonathan Wakely Reply-To: Jonathan Wakely Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1773941801768499149 X-GMAIL-MSGID: 1773941801768499149 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} } }