From 8f5bd0edda294aa53db4fcf017062798bab17502 Mon Sep 17 00:00:00 2001 From: Richard Cochran Date: Tue, 4 Sep 2012 07:51:59 +0200 Subject: [PATCH] Guard against divide by zero. If a buggy driver or hardware delivers bogus time stamps, then we might crash with a divide by zero exception. Signed-off-by: Richard Cochran --- clock.c | 4 ++++ port.c | 4 ++++ tmv.h | 5 +++++ 3 files changed, 13 insertions(+) diff --git a/clock.c b/clock.c index ab30d4f..6136050 100644 --- a/clock.c +++ b/clock.c @@ -209,6 +209,10 @@ static enum servo_state clock_no_adjust(struct clock *c) if (f->count < f->max_count) { return state; } + if (tmv_eq(c->t2, f->ingress1)) { + pr_warning("bad timestamps in rate ratio calculation"); + return state; + } /* * origin2 = c->t1 (+c->path_delay) + c->c1 + c->c2; */ diff --git a/port.c b/port.c index 74efadd..df91fe1 100644 --- a/port.c +++ b/port.c @@ -407,6 +407,10 @@ static void port_nrate_calculate(struct port *p, tmv_t t3, tmv_t t4, tmv_t c) return; } origin2 = tmv_add(t3, c); + if (tmv_eq(t4, n->ingress1)) { + pr_warning("bad timestamps in nrate calculation"); + return; + } n->ratio = tmv_dbl(tmv_sub(origin2, n->origin1)) / tmv_dbl(tmv_sub(t4, n->ingress1)); diff --git a/tmv.h b/tmv.h index 2c560a0..ef15f10 100644 --- a/tmv.h +++ b/tmv.h @@ -51,6 +51,11 @@ static inline tmv_t tmv_div(tmv_t a, int divisor) return a / divisor; } +static inline int tmv_eq(tmv_t a, tmv_t b) +{ + return a == b ? 1 : 0; +} + static inline int tmv_is_zero(tmv_t x) { return x == ((tmv_t) 0) ? 1 : 0;