From patchwork Mon Aug 7 21:12:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 132387 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c44e:0:b0:3f2:4152:657d with SMTP id w14csp1714151vqr; Mon, 7 Aug 2023 14:13:38 -0700 (PDT) X-Google-Smtp-Source: AGHT+IE4ZTyM2SBkmCzO5Hw+zr6GLLpLf/qRf3ZO25I7O22doQ0krYax1/dZyRQWGnp8rJDQEG3X X-Received: by 2002:adf:de06:0:b0:317:5f04:bc00 with SMTP id b6-20020adfde06000000b003175f04bc00mr6899561wrm.27.1691442818576; Mon, 07 Aug 2023 14:13:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691442818; cv=none; d=google.com; s=arc-20160816; b=s73mxWCWjje78ZfSsoPtANLMR/Kyz9Oa3K3rJTsAst4t5qAXmVdaLTcKGrZNWMZZOS 1rqF8/gLlMpQ03/W88vTFSI27nrOXBhpE8jyDAjE7slQDXduZjVKAUswVNtVcoeJq5y9 pdIhFYDugVgPDyMZ1hvqq8cOf9D3UG459z7z/mQpsnhAHIZu707TMqgkXqcgRMyUj+0Q c5Jub1SNcIBliyDgusHgnEnHJmjqj+Vf4U1AQZBBsP2GCk/IpOXRgcj77B+i5sdDmGDA 4qlR9Q6/g9iXBl3bPGLd4HMH1tFYfDuaTnqEbXrp4mLI8jmJ+92Hj+Yxmp6MEgkhKb5L P33A== 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=Qi1f/Pf4b8fuUeuHGZzu3JP67IUO3ICU0ScrhO1LqIE=; fh=sC8lnOfyH6gL5FYlMbZ1VDz6qVPMj/3ViLJWKNUCUgQ=; b=qay/NuPj1gN8GGT3P0StCdhJ8IUqZFiKAvUkrs0gAXvcNbCQB1WXDrus7GxdNRU57C aEFBcFOps1ZiAdpvaMQ+i5BmVMXGHWEX/tHKB3/9XjWGNrmIDlPd+8XkNWfOt2Ji++XN 8pGp/EaqaAnkrmaVGJerDQg/5ENhtsxZ+DFV6wzbiAdNcefjYqdvfYzTuu58QoOsRrvE UtC/t7q4gfIeVawhjGbh3+CZpZ9aX1OzF/vrzh6D6PbEjWcY7kpqMMx6Zeg6VKMBVXnt 3NZ6FTf2dgY7JzHx1roIu7NC6UqL9gLmKVEmihnEMdoYhoHxnfCwjXDe60K2GNoEVRBX w0eg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=vdhGfkSi; 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 z12-20020aa7d40c000000b005233567744fsi2840298edq.154.2023.08.07.14.13.38 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Aug 2023 14:13:38 -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=vdhGfkSi; 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 C8B1E3857C43 for ; Mon, 7 Aug 2023 21:13:20 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C8B1E3857C43 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1691442800; bh=Qi1f/Pf4b8fuUeuHGZzu3JP67IUO3ICU0ScrhO1LqIE=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=vdhGfkSi+Hje2GVsGo6B3WPnqXHrCHG/n/lpBaceWdW1pw4VCfhWnxA6ytxNoU/an F5H7S0lT+5YiZ3ZILoODiiEFPIO8IIH4Pk65edy4Vmqh2zoZ1IBcQ7B1SDGM+TFvYj F2s3sXst7R0fJqV+XWKpcKsnQiD/R6LyRWBHFf9A= 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 3D8E03857C45 for ; Mon, 7 Aug 2023 21:12:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 3D8E03857C45 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-355-xG2TD4yIOeW1B8KC9Cwy5w-1; Mon, 07 Aug 2023 17:12:36 -0400 X-MC-Unique: xG2TD4yIOeW1B8KC9Cwy5w-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 82FF9101A59A; Mon, 7 Aug 2023 21:12:35 +0000 (UTC) Received: from localhost (unknown [10.42.28.127]) by smtp.corp.redhat.com (Postfix) with ESMTP id 48171403166; Mon, 7 Aug 2023 21:12:35 +0000 (UTC) To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Fix past-the-end increment in std::format [PR110862] Date: Mon, 7 Aug 2023 22:12:11 +0100 Message-ID: <20230807211234.701538-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.10 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.8 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 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: 1773606345033716297 X-GMAIL-MSGID: 1773606345033716297 Tested x86_64-linux. Pushed to trunk. gcc-13 backport to follow. -- >8 -- At the end of a replacement field we should check that the closing brace is actually present before incrementing past it. libstdc++-v3/ChangeLog: PR libstdc++/110862 * include/std/format (_Scanner::_M_on_replacement_field): Check for expected '}' before incrementing iterator. * testsuite/std/format/string.cc: Check "{0:{0}" format string. --- libstdc++-v3/include/std/format | 4 +++- libstdc++-v3/testsuite/std/format/string.cc | 13 +++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/libstdc++-v3/include/std/format b/libstdc++-v3/include/std/format index 1e0ef612ddd..0debbae5ba8 100644 --- a/libstdc++-v3/include/std/format +++ b/libstdc++-v3/include/std/format @@ -3534,7 +3534,9 @@ namespace __format _M_pc.advance_to(__ptr); } _M_format_arg(__id); - _M_pc.advance_to(_M_pc.begin() + 1); // Move past '}' + if (begin() == end() || *begin() != '}') + __format::__unmatched_left_brace_in_format_string(); + _M_pc.advance_to(begin() + 1); // Move past '}' } constexpr virtual void _M_format_arg(size_t __id) = 0; diff --git a/libstdc++-v3/testsuite/std/format/string.cc b/libstdc++-v3/testsuite/std/format/string.cc index d28135ec260..6a45237b8c4 100644 --- a/libstdc++-v3/testsuite/std/format/string.cc +++ b/libstdc++-v3/testsuite/std/format/string.cc @@ -128,6 +128,19 @@ test_format_spec() VERIFY( ! is_format_string_for("{:9999999}", 1) ); } +void +test_pr110862() +{ + try { + // PR libstdc++/110862 out-of-bounds read on invalid format string + (void) std::vformat("{0:{0}", std::make_format_args(1)); + VERIFY( false ); + } catch (const std::format_error& e) { + std::string_view what = e.what(); + VERIFY( what.find("unmatched left brace") != what.npos ); + } +} + int main() { test_no_args();