Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

<random>: UB in random number distributions: type casting out of range #1123

Closed
statementreply opened this issue Jul 31, 2020 · 1 comment · Fixed by #1159
Closed

<random>: UB in random number distributions: type casting out of range #1123

statementreply opened this issue Jul 31, 2020 · 1 comment · Fixed by #1159
Labels
bug Something isn't working fixed Something works now, yay!

Comments

@statementreply
Copy link
Contributor

statementreply commented Jul 31, 2020

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
@CaseyCarter CaseyCarter added the bug Something isn't working label Jul 31, 2020
@MattStephanson
Copy link
Contributor

I can combine this with my work on #1001 (which incidentally should fix binomial_distribution).

MattStephanson added a commit to MattStephanson/STL that referenced this issue Aug 17, 2020
@StephanTLavavej StephanTLavavej added the fixed Something works now, yay! label Oct 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed Something works now, yay!
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants