mouse07410 / asn1c

The ASN.1 Compiler
http://lionet.info/asn1c/
BSD 2-Clause "Simplified" License
94 stars 70 forks source link

Bug with JUNKTEST with Random values on MSYS2 and Cygwin #135

Closed kentkost closed 1 year ago

kentkost commented 1 year ago

This is an issue that is as old as time when it comes to random values and stdlib.

In the converter-example.c I can't use JUNKTEST on windows.

First because the method on windows are called rand and srand and not random and srandom

An easy fix for this to just add further macros to converter-example.c

#if defined(__WIN32__) && defined(JUNKTEST)
#define random rand
#define srandom srand
#endif

The second issue is that in stdlib.h on windows the RAND_MAX is a 16 bit number and is defined as 0x7fff But for example on openbsd it is defined to be as large as 0x7fffffff

Which means in the file asn_random_fill.c line 47 it will always fail on Windows.

I have made a fix for this that looks like this:

intmax_t
asn_random_between(intmax_t lb, intmax_t rb) {
        ...
        int max= 0xffffff;

#ifdef __WIN32__
        max= RAND_MAX-1;
#endif

        assert(RAND_MAX > max);    /* Seen 7ffffffd! */
        assert(range < intmax_max);

        for(; got_entropy < range;) {
            got_entropy = (got_entropy << 24) | max;
#ifdef HAVE_RANDOM
            value = (value << 24) | (random() % max);
#else
            value = (value << 24) | (rand() % max);
#endif
        }
        ...

Basically it if runs Cygwin or MSYS2 then the max value it can generate is 16bit. I explored options online about redefining RAND_MAX but the general consensus was that it was a bad idea.

Should I make a pull request on this?

mouse07410 commented 1 year ago

Thank you, and yes please.