[LTP] [PATCH v2] misc: rewrite crash02 test
Martin Doucha
mdoucha@suse.cz
Tue Jul 1 14:15:13 CEST 2025
On 01. 07. 25 13:39, Andrea Cervesato wrote:
>
> On 6/30/25 3:51 PM, Martin Doucha wrote:
>> This algorithm has two problems:
>> 1. Undefined behavior if i == 3 && (rand() & 0xFF) > 0x7f
>> 2. Except as a result of the above undefined behavior, the algorithm
>> will never generate a negative value nor a value larger than INT_MAX.
> I see that using multiple rand() create an issue on the numeric
> dimension. By calling rand() multiple times and move the bytes results
> around, usually generates big numbers values.
> Probably we should just get back to the old algorithm. WDYT?
No, this is how to fix the algorithm:
static long rand_number(void)
{
unsigned long ret = 0;
for (int i = 0; i < sizeof(unsigned long); i++)
num |= ((rand() & 0xffUL) << (i * 8));
return ret;
}
This will generate random numbers in the full range from LONG_MIN to
LONG_MAX and without any undefined behavior.
--
Martin Doucha mdoucha@suse.cz
SW Quality Engineer
SUSE LINUX, s.r.o.
CORSO IIa
Krizikova 148/34
186 00 Prague 8
Czech Republic
More information about the ltp
mailing list