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: [committed] libstdc++: Make std::random_device throw more std::system_error [PR105081]
Date: Fri, 30 Jun 2023 15:09:23 +0100	[thread overview]
Message-ID: <20230630141024.716797-1-jwakely@redhat.com> (raw)

Tested x86_64-linux. Pushed to trunk.

-- >8 --

In r14-289-gf9412cedd6c0e7 I made the std::random_device constructor
throw std::system_error for unrecognized tokens. But it still throws
std::runtime_error for a token such as "rdseed" that is recognized but
not supported at runtime by the CPU the program is running on.

With this change we throw std::system_error for those cases too. This
fixes the following failures on Intel CPUs withour rdseed support:

FAIL: 26_numerics/random/random_device/94087.cc execution test
FAIL: 26_numerics/random/random_device/cons/token.cc execution test
FAIL: 26_numerics/random/random_device/entropy.cc execution test

libstdc++-v3/ChangeLog:

	PR libstdc++/105081
	* src/c++11/random.cc (random_device::_M_init): Throw
	std::system_error when the requested device is a valid token but
	not available at runtime.
---
 libstdc++-v3/src/c++11/random.cc | 23 ++++++++++++++++++++---
 1 file changed, 20 insertions(+), 3 deletions(-)

diff --git a/libstdc++-v3/src/c++11/random.cc b/libstdc++-v3/src/c++11/random.cc
index 6ecdc7169ab..cece6edbfc7 100644
--- a/libstdc++-v3/src/c++11/random.cc
+++ b/libstdc++-v3/src/c++11/random.cc
@@ -373,6 +373,15 @@ namespace std _GLIBCXX_VISIBILITY(default)
 				      "(const std::string&):"
 				      " unsupported token"));
 
+#if defined ENOSYS
+    const int unsupported = ENOSYS;
+#elif defined ENOTSUP
+    const int unsupported = ENOTSUP;
+#else
+    const int unsupported = 0;
+#endif
+    int err = 0;
+
 #ifdef _GLIBCXX_USE_CRT_RAND_S
     if (which & rand_s)
     {
@@ -407,6 +416,7 @@ namespace std _GLIBCXX_VISIBILITY(default)
 	      return;
 	    }
 	}
+      err = unsupported;
     }
 #endif // USE_RDSEED
 
@@ -427,6 +437,7 @@ namespace std _GLIBCXX_VISIBILITY(default)
 	      return;
 	    }
 	}
+      err = unsupported;
     }
 #endif // USE_RDRAND
 
@@ -438,6 +449,7 @@ namespace std _GLIBCXX_VISIBILITY(default)
 	    _M_func = &__ppc_darn;
 	    return;
 	  }
+	err = unsupported;
       }
 #endif // USE_DARN
 
@@ -458,6 +470,7 @@ namespace std _GLIBCXX_VISIBILITY(default)
 	    _M_func = &__libc_getentropy;
 	    return;
 	  }
+	err = unsupported;
       }
 #endif // _GLIBCXX_HAVE_GETENTROPY
 
@@ -477,6 +490,7 @@ namespace std _GLIBCXX_VISIBILITY(default)
       if (_M_file)
 	return;
 #endif // USE_POSIX_FILE_IO
+      err = errno;
     }
 #endif // _GLIBCXX_USE_DEV_RANDOM
 
@@ -493,9 +507,12 @@ namespace std _GLIBCXX_VISIBILITY(default)
     }
 #endif
 
-    std::__throw_runtime_error(
-	__N("random_device::random_device(const std::string&):"
-	    " device not available"));
+    auto msg = __N("random_device::random_device(const std::string&):"
+		   " device not available");
+    if (err)
+      std::__throw_syserr(err, msg);
+    else
+      std::__throw_runtime_error(msg);
 #endif // USE_MT19937
   }
 
-- 
2.41.0


                 reply	other threads:[~2023-06-30 14:10 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20230630141024.716797-1-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).