summaryrefslogtreecommitdiff
path: root/src/croniter
diff options
context:
space:
mode:
authorPēteris Caune <cuu508@gmail.com>2021-04-26 14:26:35 +0300
committerkiorky <kiorky@cryptelium.net>2021-05-06 11:11:04 +0200
commit903c78599dc2aa7b0d57afe34b6d2575afb7ac40 (patch)
tree7ba8e520d6008a51048d2b09771ab8c88f67fd83 /src/croniter
parentbaacc7eb345e9fd8baf5d55b269858572a864c0b (diff)
downloadcroniter-903c78599dc2aa7b0d57afe34b6d2575afb7ac40.tar.gz
Fix ZeroDivisionError with "* * R/0 * *"
Diffstat (limited to 'src/croniter')
-rw-r--r--src/croniter/croniter.py7
-rw-r--r--src/croniter/tests/test_croniter_hash.py7
2 files changed, 12 insertions, 2 deletions
diff --git a/src/croniter/croniter.py b/src/croniter/croniter.py
index 3890c4e..0d3324e 100644
--- a/src/croniter/croniter.py
+++ b/src/croniter/croniter.py
@@ -851,7 +851,12 @@ class HashExpander:
crc = random.randint(0, 0xFFFFFFFF)
else:
crc = binascii.crc32(hash_id) & 0xFFFFFFFF
- return ((crc >> idx) % (range_end - range_begin + 1)) + range_begin
+
+ rng = range_end - range_begin + 1
+ if rng == 0:
+ raise CroniterBadCronError("Bad range")
+
+ return ((crc >> idx) % rng) + range_begin
def match(self, efl, idx, expr, hash_id=None, **kw):
return hash_expression_re.match(expr)
diff --git a/src/croniter/tests/test_croniter_hash.py b/src/croniter/tests/test_croniter_hash.py
index 88f228e..084a3af 100644
--- a/src/croniter/tests/test_croniter_hash.py
+++ b/src/croniter/tests/test_croniter_hash.py
@@ -1,6 +1,6 @@
from datetime import datetime, timedelta
-from croniter import croniter, CroniterNotAlphaError
+from croniter import croniter, CroniterNotAlphaError, CroniterBadCronError
from croniter.tests import base
@@ -122,6 +122,11 @@ class CroniterHashTest(CroniterHashBase):
with self.assertRaises(TypeError):
croniter('H H * * *', self.epoch, hash_id={1: 2})
+ def test_invalid_divisor(self):
+ """Test an invalid divisor type raises CroniterBadCronError"""
+ with self.assertRaises(CroniterBadCronError):
+ croniter('* * H/0 * *', self.epoch, hash_id=self.hash_id)
+
class CroniterWordAliasTest(CroniterHashBase):
def test_hash_word_midnight(self):