[libcxx-commits] [libcxx] [libc++] Fixed uniform_real_distribution.h where it was allowing initialization with non floating point types (PR #70485)

Pranav Tatavarthy via libcxx-commits libcxx-commits at lists.llvm.org
Fri Oct 27 10:57:44 PDT 2023


https://github.com/Pranav10903 created https://github.com/llvm/llvm-project/pull/70485

Included a static_assert to deal with cases where result_types are initialized with non floating point type values which can cause undefined behaviour as mentioned [here](https://en.cppreference.com/w/cpp/numeric/random/uniform_real_distribution)

>From 52cb5d88e3a4bd347f6993405b18480acc0835f1 Mon Sep 17 00:00:00 2001
From: Pranav Tatavarthy <97344709+Pranav10903 at users.noreply.github.com>
Date: Fri, 27 Oct 2023 21:41:41 +0530
Subject: [PATCH 1/2] Update uniform_real_distribution.h

---
 libcxx/include/__random/uniform_real_distribution.h | 1 +
 1 file changed, 1 insertion(+)

diff --git a/libcxx/include/__random/uniform_real_distribution.h b/libcxx/include/__random/uniform_real_distribution.h
index 1388cef95f39414..8268dc4f14327c0 100644
--- a/libcxx/include/__random/uniform_real_distribution.h
+++ b/libcxx/include/__random/uniform_real_distribution.h
@@ -27,6 +27,7 @@ _LIBCPP_BEGIN_NAMESPACE_STD
 template<class _RealType = double>
 class _LIBCPP_TEMPLATE_VIS uniform_real_distribution
 {
+     static_assert(std::is_floating_point<_RealType>::value, "result_type must be a floating point type");
 public:
     // types
     typedef _RealType result_type;

>From c846260ffb00060a4840d07b37937ed4fe14beea Mon Sep 17 00:00:00 2001
From: Pranav Tatavarthy <97344709+Pranav10903 at users.noreply.github.com>
Date: Fri, 27 Oct 2023 23:18:48 +0530
Subject: [PATCH 2/2] Update uniform_real_distribution.h

---
 libcxx/include/__random/uniform_real_distribution.h | 1 +
 1 file changed, 1 insertion(+)

diff --git a/libcxx/include/__random/uniform_real_distribution.h b/libcxx/include/__random/uniform_real_distribution.h
index 8268dc4f14327c0..40a14eff7b3aed4 100644
--- a/libcxx/include/__random/uniform_real_distribution.h
+++ b/libcxx/include/__random/uniform_real_distribution.h
@@ -14,6 +14,7 @@
 #include <__random/is_valid.h>
 #include <iosfwd>
 #include <limits>
+#include <type_traits>
 
 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
 #  pragma GCC system_header



More information about the libcxx-commits mailing list