From patchwork Tue Feb 27 18:07:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 207368 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:a81b:b0:108:e6aa:91d0 with SMTP id bq27csp2872730dyb; Tue, 27 Feb 2024 10:08:41 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCVyaX9gX0zS8gxACfV+GGATIMxOoPKIGxNGL/zQWdx9mwiMgamrzGE75wgz/vibtJnfTCsj9DHrtCnB8Jkip1pYnI4h9g== X-Google-Smtp-Source: AGHT+IHbNYk63eL3HtKWRTO7iAB2VBPDhoT/8d6m7uWeCPc0e+b6b6/D+Z4V/wIPSEIbwrhvYn87 X-Received: by 2002:a05:620a:3903:b0:787:8ed8:f2aa with SMTP id qr3-20020a05620a390300b007878ed8f2aamr3116144qkn.35.1709057321644; Tue, 27 Feb 2024 10:08:41 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709057321; cv=pass; d=google.com; s=arc-20160816; b=ICmkbPQhdqVDTIQEMAIEf4QQj6wk/9X40aHM780fpwJS5A1gi27BSEQu3vz0DH9Wnl 9rzumaqzARTBWUlEbF7pKlAmVCdqPkfX+RTbsTrUjwlpiHPuy6m8f4+NuapzlLMjI4v0 iYq1dzwlOSMlIFS2wwJ+z7uDEU0mEUIlaV+9QC29yVb6m6zXGjgJOWNkckdS9DtHVwO/ EUNtWITHJJscdWlCfLCO//dhRJZH+qhtrPH3gFHg1QrRM1NRO0IrWvE5CoCA8PWDK3yM 00mZPtu+I+niGKajAtka3mW91W4rb0hCb3aL33BqJbWKHrNBHria/qmTiJU4TD4I5lGD DFNA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:cc:to:subject:message-id:date :from:in-reply-to:references:mime-version:dkim-signature:arc-filter :dmarc-filter:delivered-to; bh=SL6Jo3yMMppZkuPNXXrbtlHJ7znWiGSjoyi7PkpQNAE=; fh=3Na6rliX2r0VLQuY6BDKPale2ZFF7nOu5PveYCJP2ew=; b=Ht7Y3DZCNTYXcQKA+kGKoHEDA6WwX/kE/6YBfKtZUtchHmxVIauMHcfhuOnfHqQu5z 1XF+Dvacqz7NMVOdLqoohntZIA3Opba2v976EqO5VCs7WPtn8GTRvGZP+94zutsqf5A+ /8JUIv5t4I3sjUCQIW2ry5+OsKXqxl328VUht2oHVxpgbXrqN2Ek2nxyakNCF+65E9tk nnttQt8gY63navk88+kyq5Tv42xR61Tj7knhenaOvVGIDu3vhsfb9v2nVTLmp2qWmBqj d5w4nIB3LD799u84FrPWYYnmbNQnbhADqLXWE77Cxz3uRYRr7NwVxRP12nSj6UQO8Uuj 8akw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=OSO3msFe; arc=pass (i=1); 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=redhat.com Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id m3-20020a05620a24c300b007873af4a4e7si9014433qkn.764.2024.02.27.10.08.41 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Feb 2024 10:08:41 -0800 (PST) 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=@redhat.com header.s=mimecast20190719 header.b=OSO3msFe; arc=pass (i=1); 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=redhat.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4AB623858C62 for ; Tue, 27 Feb 2024 18:08:41 +0000 (GMT) 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 35C9B3858D33 for ; Tue, 27 Feb 2024 18:07:48 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 35C9B3858D33 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 35C9B3858D33 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1709057270; cv=none; b=MUUbQLq0WyURD02pa03eeRt+16MEI4PvFNzoIeZYyTghCxdtg5RWiL0xyBGkjoh8jY0r0ctTBRbkldY7/raFO/YxpaKxTSJQxBaQ/GMf54CD9MyXPDDOL+2e+i07upLyVOAPoFiYYnSwCb+M+opOFHAX4QfgcEB1VnewpQJkpnw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1709057270; c=relaxed/simple; bh=SL6Jo3yMMppZkuPNXXrbtlHJ7znWiGSjoyi7PkpQNAE=; h=DKIM-Signature:MIME-Version:From:Date:Message-ID:Subject:To; b=GlSkuwnlpyxwuGerbzV/Mf4vJpbuRet8ZQ23h9dHDM65z6kV/VfGVO5DPhSAcqC39xCcNXbXTsUPyfRHpZ3sULJuFEIAtkkxhTEVvq4K1Sgu8tUw08VwpKhZYjQqrkcur2YU7WKpjJCyzz84Jy01QWDFHC2AnzClGOiTMl0j2HI= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1709057267; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=SL6Jo3yMMppZkuPNXXrbtlHJ7znWiGSjoyi7PkpQNAE=; b=OSO3msFeVbdU/5iT3swINE3s1WAO4T5lUkCm1C/LOGg8gbPpgNZzV6bdtCdfsFH6/hkfac QGpAyurv4tjKaMAOu7VOB33ldbgzQQnOsPnBFbE+I8PN3KBmmP8JNVbe5fqhlCvW/XuMoc SM/PYZhBw9XrlfZdZ3ueNRnfA9vmZzQ= Received: from mail-yb1-f198.google.com (mail-yb1-f198.google.com [209.85.219.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-83-Y9_DyEG1P3SwmwI65hMizg-1; Tue, 27 Feb 2024 13:07:46 -0500 X-MC-Unique: Y9_DyEG1P3SwmwI65hMizg-1 Received: by mail-yb1-f198.google.com with SMTP id 3f1490d57ef6-dbe9e13775aso6447595276.1 for ; Tue, 27 Feb 2024 10:07:46 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709057265; x=1709662065; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=SL6Jo3yMMppZkuPNXXrbtlHJ7znWiGSjoyi7PkpQNAE=; b=f246p8zHxI0IYzP5VwKunsa308CXHTm1786n2u5aiRbj3NFWKkuCnS8LQIyyYU1c57 IuYVy+dGhb6rU+Y+vLD97KWTS0huKB3XRrQ6cktLxLO+hgJmHrx4xKz8iPe7BbFJ2kR/ XspDQBS2TL+mWDKTdxtCoo1srpqI/Mfm1oQsO6GjgoOgLA8qf9w4sh2n1E4cprbiHIl4 vOuksPLt5baSKS4aDC/SWp1101aCfsF+TwPOjQGzEzjTY3X+qSdpC4ZsBzPLQgXO2/5l nSnd/eCQ7wNQGhYtKv86TxveLGG079DYfCplZ3PwLGjIMvWIh+lLAFI/Jlq5JILxyTZv Q1bg== X-Forwarded-Encrypted: i=1; AJvYcCVGesMpT5Jxnfw4VoXWAzOx0dD7x6v0g/sZecU4kjfOax/uTVXiqIVESDWMrvR+atkSvSpWtEtVRSpoQtwocsbslt+jMnsBJg== X-Gm-Message-State: AOJu0Yx4czBj8/rKVRhVJtaCYsIzTeyTsZ+hTZmxpRAtvnKduE7v9BpW dcK/y6p9nulBcGQGO64/jp4GxCIUSFH9SGpg8C/bCEASabzJVfsHFAZCiBBz8OGOktoI1HjdOuN FCYakEKgt7xxbowS6TRQY+odRfzuKtRH7i/z831cQ+bR5RwFRDRYLlkkzi2ScO2XSncZxvkhWSa d7UBsdP82+LbNlH4xe6bVcp7VHkZVSlw== X-Received: by 2002:a25:9909:0:b0:dc7:45d3:ffd0 with SMTP id z9-20020a259909000000b00dc745d3ffd0mr242546ybn.1.1709057265449; Tue, 27 Feb 2024 10:07:45 -0800 (PST) X-Received: by 2002:a25:9909:0:b0:dc7:45d3:ffd0 with SMTP id z9-20020a259909000000b00dc745d3ffd0mr242530ybn.1.1709057265160; Tue, 27 Feb 2024 10:07:45 -0800 (PST) MIME-Version: 1.0 References: <20240227114528.1350601-4-jwakely@redhat.com> In-Reply-To: From: Jonathan Wakely Date: Tue, 27 Feb 2024 18:07:29 +0000 Message-ID: Subject: [PATCH v2] libstdc++: Fix error handling in std::print To: Tim Song Cc: "libstdc++" , gcc Patches X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.9 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, PDS_OTHER_BAD_TLD, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1792052480194195933 X-GMAIL-MSGID: 1792076490448821310 On Tue, 27 Feb 2024 at 15:21, Tim Song wrote: > > [print.fun] requires a system_error, but I don't think [ostream.formatted.print] does? Yeah it looks like I got confused (again) jumping back and forth between [print.fun] and [ostream.formatted.print]. So we're doing the right thing, and should just add tests to verify that. commit e1c689dbeb5b6364eb2a2f0af20ced07b8096b82 Author: Jonathan Wakely Date: Thu Feb 22 13:06:59 2024 libstdc++: Test error handling in std::print The standard requires an exception if std::print fails to write to a FILE*. When writing to a std::ostream, failure to format the arguments doesn't affect the stream state, but failure to write to the streadm sets badbit. libstdc++-v3/ChangeLog: * testsuite/27_io/basic_ostream/print/1.cc: Check error handling. * testsuite/27_io/print/1.cc: Likewise. diff --git a/libstdc++-v3/testsuite/27_io/basic_ostream/print/1.cc b/libstdc++-v3/testsuite/27_io/basic_ostream/print/1.cc index 71a4daa04c9..cd4b116ac1c 100644 --- a/libstdc++-v3/testsuite/27_io/basic_ostream/print/1.cc +++ b/libstdc++-v3/testsuite/27_io/basic_ostream/print/1.cc @@ -103,6 +103,40 @@ test_locale() } } +void +test_errors() +{ + // Failure to generate output is reported by setting badbit. + std::stringstream in(std::ios::in); + std::print(in, "{}", "nope"); // No exception here. + VERIFY(in.bad()); +#ifdef __cpp_exceptions + in.clear(); + in.exceptions(std::ios::badbit); + try + { + std::print(in, "{}", "nope"); // Should throw now. + VERIFY(false); + } + catch (const std::ios::failure&) + { + } + + // An exception thrown when formatting the string is propagated + // without setting badbit. + std::ostringstream out; + try + { + std::vprint_nonunicode(out, "{}", std::make_format_args()); + VERIFY(false); + } + catch (const std::format_error&) + { + } + VERIFY(out.good()); +#endif +} + int main() { test_print_ostream(); @@ -111,4 +145,5 @@ int main() test_print_no_padding(); test_vprint_nonunicode(); test_locale(); + test_errors(); } diff --git a/libstdc++-v3/testsuite/27_io/print/1.cc b/libstdc++-v3/testsuite/27_io/print/1.cc index 6a294e0454b..d570f7938be 100644 --- a/libstdc++-v3/testsuite/27_io/print/1.cc +++ b/libstdc++-v3/testsuite/27_io/print/1.cc @@ -74,6 +74,21 @@ test_vprint_nonunicode() // { dg-output "garbage in . garbage out" } } +void +test_errors() +{ +#ifdef __cpp_exceptions + try + { + std::print(stdin, "{}", "nope"); + VERIFY(false); + } + catch (const std::system_error&) + { + } +#endif +} + int main() { test_print_default(); @@ -82,4 +97,5 @@ int main() test_println_file(); test_print_raw(); test_vprint_nonunicode(); + test_errors(); }