commit 12c212e50580cbfa6d929ae444c67732af842cb7
Author:     Elie Le Vaillant <eolie...@disroot.org>
AuthorDate: Fri Dec 6 10:37:36 2024 +0100
Commit:     Roberto E. Vargas Caballero <k...@shike2.com>
CommitDate: Thu Dec 19 11:40:06 2024 +0100

    cron: fix parsing and '~' behavior
    
    In parserange(), we tested wether range was null, to test wether or
    not the repeat number was the end of the string (to test if we had
    something like "*/3/34").  But it is str that we should be testing,
    not range, as its value as a pointer doesn't mean anything in the
    current context.
    
    This makes this cron more in line with other interpretations concerning
    '~'.  In other crons, a random number is picked for the starting field,
    and it doesn't change during all of the program's lifetime, whereas this
    one used to change its random number everytime it was matched.

diff --git a/cron.c b/cron.c
index 5d940cd..2f3cedc 100644
--- a/cron.c
+++ b/cron.c
@@ -18,7 +18,7 @@
 #include "util.h"
 
 struct range {
-       long low, high, repeat, random;
+       long low, high, repeat;
        TAILQ_ENTRY(range) entry;
 };
 
@@ -204,11 +204,7 @@ matchentry(struct ctabentry *cte, struct tm *tm)
                found = 0;
                t = matchtbl[i].tm;
                TAILQ_FOREACH(r, matchtbl[i].f, entry) {
-                       if (r->random)
-                               low = r->random;
-                       else
-                               low = r->low;
-                       if (low <= t && r->high >= t && t % r->repeat == 0) {
+                       if (r->low <= t && r->high >= t && t % r->repeat == 0) {
                                found = 1;
                                break;
                        }
@@ -219,13 +215,6 @@ matchentry(struct ctabentry *cte, struct tm *tm)
        if (i != LEN(matchtbl))
                return 0;
 
-       for (i = 0; i < LEN(matchtbl); i++) { /* only if entry is matched */
-               TAILQ_FOREACH(r, matchtbl[i].f, entry) {
-                       if (r->random)
-                               r->random = random_uniform(r->high - r->low) + 
r->low;
-               }
-       }
-
        return 1;
 }
 
@@ -239,8 +228,9 @@ parserange(char *str, long low, long high, struct range *r)
         */
        char *range, *repeat, *strlow, *strhigh;
        char *e;
+       int random;
 
-       r->random = 0;
+       random = 0;
 
        range = strsep(&str, "/");
        repeat = strsep(&str, "/");
@@ -249,7 +239,7 @@ parserange(char *str, long low, long high, struct range *r)
 
        switch (*range) {
        case '~':
-               r->random = 1;
+               random = 1;
        case '*': /* fallthru */
                if (range[1] != '\0')
                        return -1;
@@ -281,7 +271,7 @@ parserange(char *str, long low, long high, struct range *r)
                        if (strhigh) {
                                if (!*strhigh || strlow != NULL) /* i.e. N~ or 
N~M~... */
                                        return -1;
-                               r->random = 1;
+                               random = 1;
 
                                errno = 0;
                                r->high = strtol(strhigh, &e, 10);
@@ -293,7 +283,7 @@ parserange(char *str, long low, long high, struct range *r)
        }
 
        if (repeat) {
-               if (!*repeat || range != NULL)
+               if (!*repeat || str != NULL)
                        return -1;
                errno = 0;
                r->repeat = strtol(repeat, &e, 10);
@@ -303,9 +293,10 @@ parserange(char *str, long low, long high, struct range *r)
                r->repeat = 1;
        }
 
-       if (r->random) {
-               /* random replaces low in matchentry(), if it is >0 */
-               r->random = random_uniform(r->high - r->low) + r->low;
+       if (random) {
+               /* random replaces low in matchentry() */
+               r->repeat = r->low; /* so that it doesn't repeat */
+               r->low = random_uniform(r->high - r->low+1) + r->low;
        }
 
        if (r->low < low || r->low > high || r->high < low || r->high > high || 
r->repeat < low || r->repeat > high) {

Reply via email to