Re: [PATCH] ptp/ptp_clock.c: Correct input parameter range check

From: Richard Cochran
Date: Wed Apr 03 2019 - 23:15:01 EST


On Thu, Apr 04, 2019 at 05:39:58AM +0530, Siddaraju D H wrote:
> From: Siddaraju DH <siddarajudh@xxxxxxxxx>
>
> The ealier implementaion used to return EINVAL for -ve adjustments
> in the range -1ns to -999999999ns as these -ve numbers will fail the
> unsigned comaparison against NSEC_PER_SEC. Since the tv_sec field
> will be ZERO in this range, the user will not be able to specify
> the signedness of adjustment through the tv_sec field.

NAK, the tv_sec field can be set to -1. See the example, below.

Thanks,
Richard

void clockadj_step(clockid_t clkid, int64_t step)
{
struct timex tx;
int sign = 1;
if (step < 0) {
sign = -1;
step *= -1;
}
memset(&tx, 0, sizeof(tx));
tx.modes = ADJ_SETOFFSET | ADJ_NANO;
tx.time.tv_sec = sign * (step / NS_PER_SEC);
tx.time.tv_usec = sign * (step % NS_PER_SEC);
/*
* The value of a timeval is the sum of its fields, but the
* field tv_usec must always be non-negative.
*/
if (tx.time.tv_usec < 0) {
tx.time.tv_sec -= 1;
tx.time.tv_usec += 1000000000;
}
if (clock_adjtime(clkid, &tx) < 0)
pr_err("failed to step clock: %m");
}