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: 207152 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:a81b:b0:108:e6aa:91d0 with SMTP id bq27csp2639689dyb; Tue, 27 Feb 2024 03:47:04 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWrv8FfcdQMusjEurljKbCdXmtikle2Qy5yPPYM4Q4U6Q+PQxDRWUl8mjp010m6RBf+qQEcwma09+gnk0HhaYqN9GlY2w== X-Google-Smtp-Source: AGHT+IEWFvLLFsCok7K0vtoty901X0tSkaWp1gUwTxKtO6C25aOJ2L4sFOEsO6yZ8iRumBbFY657 X-Received: by 2002:ad4:5dcb:0:b0:68f:5546:c7e8 with SMTP id m11-20020ad45dcb000000b0068f5546c7e8mr2366529qvh.48.1709034424180; Tue, 27 Feb 2024 03:47:04 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709034424; cv=pass; d=google.com; s=arc-20160816; b=0B/x0UAI0X6V4vhlbGTosVUAt7JDQS+Y2WafwcO64c1XPickCjqHQPvvme4/F42bSs Q/A/WY6eVoThjtCj0ApQ2eEGR5UJk9nzjuzOCpUqlHfuLRwZacMlyy40GlZWIfWJFo0I CcLpucGTRAVoUcW4xonq0JNr5s9Ur1I+cpV+ZpZ8X8J9BTqvS0R+5/pi6ea+TS3Rj9/5 isd57mmHQm2+2X4Th30JbFvrcswMV4nmY3YH6padPz37Nkk/K7LXSq1ehBmhhsdnpTrI Zmw+iM3C8SWotys5AUAf9qQM+PfUflmsKHEOx5PFxJLlUz1vpXF/bBa8ulh/q6Belkcn 441g== 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:content-transfer-encoding :mime-version:message-id:date:subject:to:from:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=5z9kdMFuG0Zcxy8i2riDPwaeUfqe4VV2RY+I8wD39BU=; fh=sJ+2/4g29YdyXkoRrFZSpsL2zxijepB7X/1rB0LDDh8=; b=Vr/CadMFngO0MMJb2HzSogi+SQ1q8sORxmSr06Ff/8QIJcy7cQocdsKROi7yLXt6WN xgHiWAd/5MGr+6FkHb/YovO9m1TfMxkWxn9UR/+PEX6WF250/wjid2qOr7yHYtl0FXXJ PChYwep5tooPNSC58HLua8nE3QLzLkdAcJhXqWtsMrxOzMzzQtgz8Jq++2oTXF+nlDuM mQ0YR7KAAWSKBCqjUD4skB0qM4dVrZPcHeG5KhbbqS0qdpiOtL6a2nyKrtAqSPL6gg8c AvkgcYH4xYZycZokveC4ZtahI+Ut+FdGifsjdPgu2fCAURtft4hKGbPXLOpKjP0pMz1G /EYg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=HRiBa0YZ; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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. [8.43.85.97]) by mx.google.com with ESMTPS id im1-20020a056214246100b0068fe58389dbsi7470989qvb.536.2024.02.27.03.47.04 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 27 Feb 2024 03:47:04 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=HRiBa0YZ; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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 A136C38582AC for ; Tue, 27 Feb 2024 11:47:03 +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+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1792052480194195933 X-GMAIL-MSGID: 1792052480194195933 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(); }