From patchwork Tue Feb 27 11:42:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 86432 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 2ACCE3858034 for ; Tue, 27 Feb 2024 11:46:40 +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 C9A263858413 for ; Tue, 27 Feb 2024 11:45:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C9A263858413 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 C9A263858413 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=1709034338; cv=none; b=OFer2XQVvJlxsD75J/P2OBjVqNjt2IT9wKrxjHnFE9P2wIvfRgD1Ckb02cdFXiYlS/CXVd7NiG1OBx0HxddZbipybo/x7NOj6WVOzAQrNX3ZRFmFvAN7mo3usELr23vGo3Oa3S95CVFtWSztDY7bvZZOnMyS1ej79w8757wzA5o= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1709034338; c=relaxed/simple; bh=zV0IucB9ylBK1JxbWol7kRf3g6WdtTe64i4tD9xytMg=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=uE1t9O5lVyFmwDmf7mhJWYJmINa01nYjNvemLA9qWeEvRFeMQlNvM9RMsnLXR/NK86CxpF2oHZHd0jnzGY7Y+BLRThNLHaxW/jvBnAMXU4sGIeU1TDPN0/4J+q6npjWlWJrGHD5rtVIMXYB2osAcgWWABFpdNuD2ShIi+1RC0Mk= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1709034334; 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=5z9kdMFuG0Zcxy8i2riDPwaeUfqe4VV2RY+I8wD39BU=; b=HRiBa0YZRuAKkM7ShJY/0sbOTNcNDtyuVjY03ED7pjh46aOChPh4Hnq/QVKFLZ+zIQ5r1g /6Sbs/hHcuIX2AI7vtHrOQgLuD/BSGxF0QSi4WXjhLo9Yq5X12CP+E/MjY9WledchVdOxb rTFfgcnh7yJ9NBtrvgSLtl/V5bXrExI= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-613-_UJHOeY5PMyJz7sFM_NGUQ-1; Tue, 27 Feb 2024 06:45:32 -0500 X-MC-Unique: _UJHOeY5PMyJz7sFM_NGUQ-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 13A64185A785; Tue, 27 Feb 2024 11:45:32 +0000 (UTC) Received: from localhost (unknown [10.42.28.181]) by smtp.corp.redhat.com (Postfix) with ESMTP id D16B3C01644; Tue, 27 Feb 2024 11:45:31 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [PATCH 4/8] libstdc++: Fix error handling in std::print Date: Tue, 27 Feb 2024 11:42:01 +0000 Message-ID: <20240227114528.1350601-4-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.8 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, 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+patchwork=sourceware.org@gcc.gnu.org Tested x86_64-linux. Reviews invited. -- >8 -- The standard requires an exception if std::print fails to write to a std::ostream. libstdc++-v3/ChangeLog: * include/std/ostream (vprint_nonunicode): Throw if stream state indicates writing failed. * testsuite/27_io/basic_ostream/print/1.cc: Check for exception. * testsuite/27_io/print/1.cc: Likewise. --- libstdc++-v3/include/std/ostream | 5 +++++ .../testsuite/27_io/basic_ostream/print/1.cc | 17 +++++++++++++++++ libstdc++-v3/testsuite/27_io/print/1.cc | 16 ++++++++++++++++ 3 files changed, 38 insertions(+) diff --git a/libstdc++-v3/include/std/ostream b/libstdc++-v3/include/std/ostream index a136399ad0b..3740ad6edfa 100644 --- a/libstdc++-v3/include/std/ostream +++ b/libstdc++-v3/include/std/ostream @@ -901,6 +901,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION __catch(...) { __os._M_setstate(ios_base::badbit); } } + + if (!__os) + __throw_system_error(EIO); } inline void @@ -974,6 +977,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION __catch(...) { __os._M_setstate(ios_base::badbit); } } + if (!__os) + __throw_system_error(EIO); #endif // _WIN32 } 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..14bfb14d556 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,22 @@ test_locale() } } +void +test_errors() +{ +#ifdef __cpp_exceptions + std::stringstream in(std::ios::in); + try + { + std::print(in, "{}", "nope"); + VERIFY(false); + } + catch (const std::system_error&) + { + } +#endif +} + int main() { test_print_ostream(); @@ -111,4 +127,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(); }