LLVM Bugzilla is read-only and represents the historical archive of all LLVM issues filled before November 26, 2021. Use github to submit LLVM bugs

Bug 51520 - std::uniform_int_distribution<__int128_t>{INT64_MIN, INT64_MAX} produces out-of-range values
Summary: std::uniform_int_distribution<__int128_t>{INT64_MIN, INT64_MAX} produces out-...
Status: NEW
Alias: None
Product: libc++
Classification: Unclassified
Component: All Bugs (show other bugs)
Version: 12.0
Hardware: PC Linux
: P normal
Assignee: Unassigned Clang Bugs
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2021-08-18 03:55 PDT by Moritz Klammler
Modified: 2021-08-18 03:55 PDT (History)
2 users (show)

See Also:
Fixed By Commit(s):


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Moritz Klammler 2021-08-18 03:55:49 PDT
The specific std::uniform_int_distribution<__int128_t>{INT64_MIN, INT64_MAX} produces values that are not in range. Changing any of the parameters -- e.g. using std::int64_t as type or making either of the limits smaller or larger -- causes the problem to go away. The incorrect values are completely out-of-range, not marginal off-by-one errors.

I have created the following test program and could reproduce the issue with every version of libc++ and set of compiler options I've tried; the problem never occurs with -stdlib=libstdc++ so it seems to me to be a library rather than a compiler issue.

https://godbolt.org/z/z5WYsKEv6

This is a minimal version (with no output) of the demo linked above to reproduce the problem:

#include <cstdint>
#include <cstdlib>
#include <random>

int main()
{
    auto engine = std::default_random_engine{};
    auto distro = std::uniform_int_distribution<__int128_t>{INT64_MIN, INT64_MAX};
    for (auto i = 0; i < 1000; ++i) {
        const auto n = distro(engine);
        if ((n < distro.min()) || (n > distro.max())) {
            return EXIT_FAILURE;
        }
    }
    return EXIT_SUCCESS;
}