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: 1905365 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=OSO3msFe; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4TklpS5wC3z1yX4 for ; Wed, 28 Feb 2024 05:08:16 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id BF7833858C3A for ; Tue, 27 Feb 2024 18:08:13 +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-Google-Smtp-Source: AGHT+IEkQ0b+1librMgZmPMPUm/L/z/a4dWmnnSqSSZ/9FIhNvW2j791D5ucSbmJbI8Wv5nKpvd1Xqmg+I2utEUbYsQ= 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+incoming=patchwork.ozlabs.org@gcc.gnu.org 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(); }