cronie-crond / cronie

Cronie cron daemon project
Other
467 stars 80 forks source link

RANDOM_DELAY scaling factor is incorrect on Solaris/AIX #98

Closed nilx closed 2 years ago

nilx commented 2 years ago

RANDOM_DELAY is set at process startup, by defining a randomized scaling factor https://github.com/cronie-crond/cronie/blob/75843b4bb1509f815eae3bb014748749a8765aae/src/cron.c#L307-L313

RandomScale = (double)random() / (double)RAND_MAX;

This is incorrect, as per specs random() returns a value within 0 ... 2^31-1. It's the rand() function which returns a value within 0 ... RAND_MAX.

On Linux/glibc and OSX, RAND_MAX is 2^31-1. But on other systems it's not, as observed on

Trivial way to observe it:

#include <stdlib.h>
#include <stdio.h>

int main (int argc, char** argv)
{
    printf("%lu\n", (1lu << 31) - 1);
    printf("%lu\n", RAND_MAX);
    return 0;
}

This results in invalid scale factors, as reported in startup logs:

INFO(54211): RANDOM_DELAY will be scaled with factor 4018247% if used.