https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115049

--- Comment #3 from Jörn Heusipp <manx-bugzilla at problemloesungsmaschine dot 
de> ---
Created attachment 58613
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=58613&action=edit
updated testcase-v2

Updated testcase that got rid of std::mt19937. It's still somewhat huge because
it still drags in <random>:

file1.cpp
```

#include <random>

void Trigger1();

unsigned int dummy_function();
unsigned int dummy_function() {
        return 42;
}

int main( int /*argc*/ , char * /*argv*/ [] ) {
        Trigger1();
        return 0;
}

static void Trigger2() {
        std::ranlux48 prng{1};
        unsigned short Data1 = std::uniform_int_distribution<unsigned
short>{}(prng);
        unsigned long long Data2 = std::uniform_int_distribution<unsigned long
long>{}(prng);
        static_cast<void>(Data1);
        static_cast<void>(Data2);
}

using my_dummy_function = void (*)();

struct myDummy3 {
        my_dummy_function func{nullptr};
        myDummy3(my_dummy_function f)
                : func(f)
        {
        }
};

myDummy3 dummy3{
        &Trigger2
};

```

file2.cpp:
```

#include <random>

#include <stdio.h>

template <typename T>
const T & my_min(const T & a, const T & b) {
        return (b < a) ? b : a;
}

template <typename T, unsigned long long required_entropy_bits, typename Trng>
inline T my_random(Trng & rng) {
        using unsigned_T = T;
        const unsigned int rng_bits = 48;
        unsigned_T result = 0;
        for (unsigned long long entropy = 0; entropy <
my_min(required_entropy_bits, sizeof(T) * 8); entropy += rng_bits) {
                if constexpr (rng_bits < (sizeof(T) * 8)) {
                        unsigned int shift_bits = rng_bits % (sizeof(T) * 8);
                        //fflush(stdout);  // <--- no crash
                        result = (result << shift_bits) ^
static_cast<unsigned_T>(rng());
                } else {
                        result = static_cast<unsigned_T>(rng());
                }
        }
        if constexpr (required_entropy_bits >= (sizeof(T) * 8)) {
                return static_cast<T>(result);
        } else {
                return static_cast<T>(result & ((static_cast<unsigned_T>(1) <<
required_entropy_bits) - static_cast<unsigned_T>(1)));
        }
}

unsigned int dummy_function();

class myDummy1 {
private:
        std::ranlux48 m_PRNG;
        unsigned short m_Trigger1a;
public:
        myDummy1(unsigned char * &)
                : m_PRNG(dummy_function())
                , m_Trigger1a(m_PRNG())
        {
        }
};

void Trigger1();

void Trigger1() {
        {
                unsigned char * rd = new unsigned char{};
                myDummy1 dummy1(rd);
                delete rd;
        }
        {
                std::ranlux48 * prng = new std::ranlux48(1);
                const unsigned int trigger1b_size = 16;
                unsigned char trigger1b[trigger1b_size];
                for (unsigned int i = 0; i < trigger1b_size; i++) {
                        trigger1b[i] = my_random<unsigned char, 8>(*prng);
                }
                static_cast<void>(trigger1b);
                delete prng;
        }
        {
                std::ranlux48 prng{1};
                printf("%llu\n", my_random<unsigned long long, 53>(prng));
fflush(stdout);
                printf("%llu\n", my_random<unsigned long long, 53>(prng));
fflush(stdout);
                printf("%llu\n", my_random<unsigned long long, 53>(prng));
fflush(stdout);  // <--- crash
                printf("%llu\n", my_random<unsigned long long, 53>(prng));
fflush(stdout);
        }
}

```

attached as testcase-v2.7z
  • [Bug tree-opt... jason at gcc dot gnu.org via Gcc-bugs
    • [Bug tre... sjames at gcc dot gnu.org via Gcc-bugs
    • [Bug tre... manx-bugzilla at problemloesungsmaschine dot de via Gcc-bugs
    • [Bug tre... manx-bugzilla at problemloesungsmaschine dot de via Gcc-bugs
    • [Bug tre... jason at gcc dot gnu.org via Gcc-bugs
    • [Bug tre... jason at gcc dot gnu.org via Gcc-bugs

Reply via email to