hw/misc: Fix arith overflow in NPCM7XX PWM module
commit1e5ce6e10a1deaec6044ba8ad75431e0424dbce9
authorHao Wu <wuhaotsh@google.com>
Wed, 27 Jan 2021 01:11:42 +0000 (26 17:11 -0800)
committerPeter Maydell <peter.maydell@linaro.org>
Fri, 29 Jan 2021 10:47:28 +0000 (29 10:47 +0000)
tree6f2110eeb591c05111c4c95886f6d185956b99c0
parentdaa726d92604b72650d86ada01935e03a909d4dd
hw/misc: Fix arith overflow in NPCM7XX PWM module

Fix potential overflow problem when calculating pwm_duty.
1. Ensure p->cmr and p->cnr to be from [0,65535], according to the
   hardware specification.
2. Changed duty to uint32_t. However, since MAX_DUTY * (p->cmr+1)
   can excceed UINT32_MAX, we convert them to uint64_t in computation
   and converted them back to uint32_t.
   (duty is guaranteed to be <= MAX_DUTY so it won't overflow.)

Fixes: CID 1442342
Suggested-by: Peter Maydell <peter.maydell@linaro.org>
Reviewed-by: Doug Evans <dje@google.com>
Signed-off-by: Hao Wu <wuhaotsh@google.com>
Message-id: 20210127011142.2122790-1-wuhaotsh@google.com
Reviewed-by: Peter Maydell <peter.maydell@linaro.org>
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
hw/misc/npcm7xx_pwm.c
tests/qtest/npcm7xx_pwm-test.c