-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Closed
Labels
bugSomething isn't workingSomething isn't workingfixedSomething works now, yay!Something works now, yay!
Description
Describe the bug
geometric_distribution, poisson_distribution, binomial_distribution, and gamma_distribution could cast potentially huge floating point values to an integer type, resulting in UB.
Command-line test case
#includes tests/std/tests/GH_001017_discrete_distribution_out_of_range/bad_random_engine.hpp
C:\Users\He\source\test>type bad_random.cpp
#include <cassert>
#include <cstdint>
#include <random>
// tests/std/tests/GH_001017_discrete_distribution_out_of_range/bad_random_engine.hpp
#include "bad_random_engine.hpp"
using namespace std;
template <class Distribution>
void test(Distribution&& distribution) {
for (bad_random_generator rng; !rng.has_cycled_through();) {
(void) distribution(rng);
}
}
int main() {
test(geometric_distribution<>{});
test(poisson_distribution<>(100));
test(binomial_distribution<>(100, 0.5));
test(gamma_distribution<>(1e+10, 1));
}
C:\Users\He\source\test>clang++ -O2 -Wall -Wextra -Werror -fsanitize=undefined bad_random.cpp -o bad_random.exe
正在创建库 bad_random.lib 和对象 bad_random.exp
C:\Users\He\source\test>.\bad_random.exe
C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Tools\MSVC\14.27.29109\include\random:2148:33: runtime error: inf is outside the range of representable values of type 'int'
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Tools\MSVC\14.27.29109\include\random:2148:33 in
C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Tools\MSVC\14.27.29109\include\random:2317:41: runtime error: 2.30959e+17 is outside the range of representable values of type 'int'
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Tools\MSVC\14.27.29109\include\random:2317:41 in
C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Tools\MSVC\14.27.29109\include\random:2500:45: runtime error: 1.15479e+17 is outside the range of representable values of type 'int'
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Tools\MSVC\14.27.29109\include\random:2500:45 in
C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Tools\MSVC\14.27.29109\include\random:3149:40: runtime error: 1e+10 is outside the range of representable values of type 'int'
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior C:\Program Files (x86)\Microsoft Visual Studio\2019\Preview\VC\Tools\MSVC\14.27.29109\include\random:3149:40 in
STL version
Microsoft Visual Studio Community 2019 Preview
版本 16.7.0 Preview 5.0
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't workingfixedSomething works now, yay!Something works now, yay!