[llvm-bugs] [Bug 26395] New: Analyzer says undefined behavior in uniform_int_distribution<size_t>

via llvm-bugs llvm-bugs at lists.llvm.org
Sat Jan 30 10:59:07 PST 2016


https://llvm.org/bugs/show_bug.cgi?id=26395

            Bug ID: 26395
           Summary: Analyzer says undefined behavior in
                    uniform_int_distribution<size_t>
           Product: libc++
           Version: 3.7
          Hardware: Macintosh
                OS: MacOS X
            Status: NEW
          Severity: normal
          Priority: P
         Component: All Bugs
          Assignee: unassignedclangbugs at nondot.org
          Reporter: jared.grubb at gmail.com
                CC: llvm-bugs at lists.llvm.org, mclow.lists at gmail.com
    Classification: Unclassified

The clang static analyzer flags this code as having undefined behavior. Note
that if I change "size_t" to "unsigned" then things look fine. It looks like
the engine is 32-bit but it's going to try a 64-bit shift maybe? I'm totally
guessing here.

.../c++/v1/algorithm:2920
 warning: The result of the '>>' expression is undefined
    __mask0_ = __w0_ > 0 ? _Engine_result_type(~0) >> (_EDt - __w0_) :
                           ~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~

To reproduce:

$ cat x.cpp
#include <random>
#include <cstddef>

size_t genRandom(size_t maxValue) {
        std::minstd_rand engine;
        std::uniform_int_distribution<size_t> dist{0, maxValue};
        return dist(engine);
}

$ clang++ x.cpp -std=c++14 --analyze

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20160130/c058b6bf/attachment.html>


More information about the llvm-bugs mailing list