mcu: Add checks to ensure pin max_duration doesn't overflow 32bit counter

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2021-11-22 10:05:52 -05:00
parent d1e13b7e12
commit faea45e60f
1 changed files with 15 additions and 7 deletions

View File

@ -257,13 +257,15 @@ class MCU_digital_out:
if self._max_duration and self._start_value != self._shutdown_value:
raise pins.error("Pin with max duration must have start"
" value equal to shutdown value")
mdur_ticks = self._mcu.seconds_to_clock(self._max_duration)
if mdur_ticks >= 1<<31:
raise pins.error("Digital pin max duration too large")
self._mcu.request_move_queue_slot()
self._oid = self._mcu.create_oid()
self._mcu.add_config_cmd(
"config_digital_out oid=%d pin=%s value=%d default_value=%d"
" max_duration=%d"
% (self._oid, self._pin, self._start_value, self._shutdown_value,
self._mcu.seconds_to_clock(self._max_duration)))
" max_duration=%d" % (self._oid, self._pin, self._start_value,
self._shutdown_value, mdur_ticks))
self._mcu.add_config_cmd("update_digital_out oid=%d value=%d"
% (self._oid, self._start_value),
on_restart=True)
@ -316,6 +318,9 @@ class MCU_pwm:
printtime = self._mcu.estimated_print_time(curtime)
self._last_clock = self._mcu.print_time_to_clock(printtime + 0.200)
cycle_ticks = self._mcu.seconds_to_clock(self._cycle_time)
mdur_ticks = self._mcu.seconds_to_clock(self._max_duration)
if mdur_ticks >= 1<<31:
raise pins.error("PWM pin max duration too large")
if self._hardware_pwm:
self._pwm_max = self._mcu.get_constant_float("PWM_MAX")
if self._is_static:
@ -331,8 +336,7 @@ class MCU_pwm:
" default_value=%d max_duration=%d"
% (self._oid, self._pin, cycle_ticks,
self._start_value * self._pwm_max,
self._shutdown_value * self._pwm_max,
self._mcu.seconds_to_clock(self._max_duration)))
self._shutdown_value * self._pwm_max, mdur_ticks))
svalue = int(self._start_value * self._pwm_max + 0.5)
self._mcu.add_config_cmd("queue_pwm_out oid=%d clock=%d value=%d"
% (self._oid, self._last_clock, svalue),
@ -347,14 +351,15 @@ class MCU_pwm:
self._mcu.add_config_cmd("set_digital_out pin=%s value=%d"
% (self._pin, self._start_value >= 0.5))
return
if cycle_ticks >= 1<<31:
raise pins.error("PWM pin cycle time too large")
self._mcu.request_move_queue_slot()
self._oid = self._mcu.create_oid()
self._mcu.add_config_cmd(
"config_digital_out oid=%d pin=%s value=%d"
" default_value=%d max_duration=%d"
% (self._oid, self._pin, self._start_value >= 1.0,
self._shutdown_value >= 0.5,
self._mcu.seconds_to_clock(self._max_duration)))
self._shutdown_value >= 0.5, mdur_ticks))
self._mcu.add_config_cmd(
"set_digital_out_pwm_cycle oid=%d cycle_ticks=%d"
% (self._oid, cycle_ticks))
@ -383,6 +388,9 @@ class MCU_pwm:
cycle_time = self._cycle_time
cycle_ticks = self._mcu.seconds_to_clock(cycle_time)
if cycle_ticks != self._last_cycle_ticks:
if cycle_ticks >= 1<<31:
raise self._mcu.get_printer().command_error(
"PWM cycle time too large")
self._set_cycle_ticks.send([self._oid, cycle_ticks],
minclock=minclock, reqclock=clock)
self._last_cycle_ticks = cycle_ticks