mirror of
https://github.com/danieldemus/openhab-core.git
synced 2026-07-29 12:34:22 +02:00
Prevent infinite loop in CronAdjuster with invalid date combinations (#4548)
Signed-off-by: Jimmy Tanagra <jcode@tanagra.id.au>
This commit is contained in:
@@ -71,6 +71,7 @@ public class CronAdjuster implements SchedulerTemporalAdjuster {
|
||||
.collect(Collectors.toMap(Entry::getKey, Entry::getValue));
|
||||
|
||||
private final List<Field> fields = new ArrayList<>(7);
|
||||
private String cronExpression;
|
||||
private final Map<String, String> environmentMap;
|
||||
private final boolean reboot;
|
||||
|
||||
@@ -82,6 +83,7 @@ public class CronAdjuster implements SchedulerTemporalAdjuster {
|
||||
environmentMap = parseEnvironment(entries);
|
||||
|
||||
String cronExpression = entries[entries.length - 1].trim();
|
||||
this.cronExpression = cronExpression;
|
||||
|
||||
reboot = "@reboot".equals(cronExpression);
|
||||
|
||||
@@ -515,6 +517,7 @@ public class CronAdjuster implements SchedulerTemporalAdjuster {
|
||||
// we start over with this new time.
|
||||
|
||||
int index = 0;
|
||||
int restarts = 0;
|
||||
final int length = fields.size();
|
||||
|
||||
while (index < length) {
|
||||
@@ -525,6 +528,10 @@ public class CronAdjuster implements SchedulerTemporalAdjuster {
|
||||
if (out == null) {
|
||||
index++;
|
||||
} else {
|
||||
if (restarts++ > 1000) {
|
||||
throw new IllegalArgumentException(
|
||||
String.format("Cron expression '%s' is not valid, too many restarts", cronExpression));
|
||||
}
|
||||
ret = out;
|
||||
index = 0;
|
||||
}
|
||||
|
||||
+11
@@ -14,6 +14,7 @@ package org.openhab.core.internal.scheduler;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.temporal.Temporal;
|
||||
@@ -25,6 +26,7 @@ import org.eclipse.jdt.annotation.NonNullByDefault;
|
||||
import org.junit.jupiter.api.Timeout;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
import org.openhab.core.scheduler.CronAdjuster;
|
||||
|
||||
/**
|
||||
@@ -197,4 +199,13 @@ public class CronAdjusterTest {
|
||||
equalTo(out));
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = { "0 0 0 31 2 *", "* * *", "80 * * * * *" })
|
||||
public void testInvalidCronExpression(String cron) {
|
||||
assertThrows(IllegalArgumentException.class, () -> {
|
||||
final CronAdjuster cronAdjuster = new CronAdjuster(cron);
|
||||
cronAdjuster.adjustInto(java.time.ZonedDateTime.now());
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user