public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jonathan Wakely <jwakely@redhat.com>
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	[thread overview]
Message-ID: <20240227114528.1350601-4-jwakely@redhat.com> (raw)

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();
 }
-- 
2.43.0


             reply	other threads:[~2024-02-27 11:45 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-27 11:42 Jonathan Wakely [this message]
2024-02-27 15:20 ` Tim Song
2024-02-27 18:07   ` [PATCH v2] " Jonathan Wakely

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240227114528.1350601-4-jwakely@redhat.com \
    --to=jwakely@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=libstdc++@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).