[hackers] [sbase][PATCH] cron: fix parsing and '~' behavior

From: Elie Le Vaillant <eolien55_AT_disroot.org>
Date: Wed, 28 Feb 2024 09:10:38 +0100

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.
---
 cron.c | 31 +++++++++++--------------------
 1 file changed, 11 insertions(+), 20 deletions(-)
diff --git a/cron.c b/cron.c
index 5d940cd..2f3cedc 100644
--- a/cron.c
+++ b/cron.c
_AT_@ -18,7 +18,7 @@
 #include "util.h"
 
 struct range {
-	long low, high, repeat, random;
+	long low, high, repeat;
 	TAILQ_ENTRY(range) entry;
 };
 
_AT_@ -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;
 			}
_AT_@ -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;
 }
 
_AT_@ -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, "/");
_AT_@ -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;
_AT_@ -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);
_AT_@ -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);
_AT_@ -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) {
-- 
2.44.0
Received on Wed Feb 28 2024 - 09:10:38 CET

This archive was generated by hypermail 2.3.0 : Wed Feb 28 2024 - 09:12:34 CET