svn commit: r366144 - head/usr.sbin/pwm
Andriy Gapon
avg at FreeBSD.org
Fri Sep 25 07:55:09 UTC 2020
Author: avg
Date: Fri Sep 25 07:55:08 2020
New Revision: 366144
URL: https://svnweb.freebsd.org/changeset/base/366144
Log:
pwm(8): fix potential duty overflow, use unsigneds for period and duty
For a long period value and the duty specified as a percentage,
there could be an overflow.
Using unsigned integers aligns the code with struct pwm_state and allows
to safely use periods up to 4 seconds where supported by drivers.
MFC after: 2 weeks
Modified:
head/usr.sbin/pwm/pwm.c
Modified: head/usr.sbin/pwm/pwm.c
==============================================================================
--- head/usr.sbin/pwm/pwm.c Fri Sep 25 07:54:38 2020 (r366143)
+++ head/usr.sbin/pwm/pwm.c Fri Sep 25 07:55:08 2020 (r366144)
@@ -75,7 +75,7 @@ main(int argc, char *argv[])
{
struct pwm_state state;
int fd;
- int period, duty;
+ u_int period, duty;
int action, ch;
cap_rights_t right_ioctl;
const unsigned long pwm_ioctls[] = {PWMGETSTATE, PWMSETSTATE};
@@ -108,16 +108,16 @@ main(int argc, char *argv[])
if (action & PWM_SHOW_CONFIG)
usage();
action |= PWM_PERIOD;
- period = strtol(optarg, NULL, 10);
+ period = strtoul(optarg, NULL, 10);
break;
case 'd':
if (action & PWM_SHOW_CONFIG)
usage();
action |= PWM_DUTY;
- duty = strtol(optarg, &percent, 10);
+ duty = strtoul(optarg, &percent, 10);
if (*percent == '%') {
- if (duty < 0 || duty > 100) {
- fprintf(stderr,
+ if (duty > 100) {
+ fprintf(stderr,
"Invalid duty percentage\n");
usage();
}
@@ -185,11 +185,11 @@ main(int argc, char *argv[])
state.period = period;
if (action & PWM_DUTY) {
if (*percent != '\0')
- state.duty = state.period * duty / 100;
+ state.duty = (uint64_t)state.period * duty / 100;
else
state.duty = duty;
}
-
+
if (ioctl(fd, PWMSETSTATE, &state) == -1) {
fprintf(stderr,
"Cannot configure the pwm controller\n");
More information about the svn-src-head
mailing list