public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH 4/8] libstdc++: Fix error handling in std::print
@ 2024-02-27 11:42 Jonathan Wakely
  2024-02-27 15:20 ` Tim Song
  0 siblings, 1 reply; 3+ messages in thread
From: Jonathan Wakely @ 2024-02-27 11:42 UTC (permalink / raw)
  To: libstdc++, gcc-patches

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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH 4/8] libstdc++: Fix error handling in std::print
  2024-02-27 11:42 [PATCH 4/8] libstdc++: Fix error handling in std::print Jonathan Wakely
@ 2024-02-27 15:20 ` Tim Song
  2024-02-27 18:07   ` [PATCH v2] " Jonathan Wakely
  0 siblings, 1 reply; 3+ messages in thread
From: Tim Song @ 2024-02-27 15:20 UTC (permalink / raw)
  To: Jonathan Wakely; +Cc: libstdc++, gcc-patches

[-- Attachment #1: Type: text/plain, Size: 3132 bytes --]

[print.fun] requires a system_error, but I don't think
[ostream.formatted.print] does?

On Tue, Feb 27, 2024 at 5:47 AM Jonathan Wakely <jwakely@redhat.com> wrote:

> 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
>
>

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH v2] libstdc++: Fix error handling in std::print
  2024-02-27 15:20 ` Tim Song
@ 2024-02-27 18:07   ` Jonathan Wakely
  0 siblings, 0 replies; 3+ messages in thread
From: Jonathan Wakely @ 2024-02-27 18:07 UTC (permalink / raw)
  To: Tim Song; +Cc: libstdc++, gcc Patches

[-- Attachment #1: Type: text/plain, Size: 328 bytes --]

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.

[-- Attachment #2: patch.txt --]
[-- Type: text/plain, Size: 2489 bytes --]

commit e1c689dbeb5b6364eb2a2f0af20ced07b8096b82
Author: Jonathan Wakely <jwakely@redhat.com>
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();
 }

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-02-27 18:07 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-27 11:42 [PATCH 4/8] libstdc++: Fix error handling in std::print Jonathan Wakely
2024-02-27 15:20 ` Tim Song
2024-02-27 18:07   ` [PATCH v2] " Jonathan Wakely

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).