From bbb3c4af851bdc7e76c2104d8f67e2d847a6a4f5 Mon Sep 17 00:00:00 2001 From: Olivier Tilmans Date: Tue, 27 Oct 2015 12:20:03 +0100 Subject: [PATCH] link_sim: avoid dividing by 0 Reported in #5 Signed-off-by: Olivier Tilmans --- link_sim.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/link_sim.c b/link_sim.c index 77af2e4..b1bdd1b 100644 --- a/link_sim.c +++ b/link_sim.c @@ -159,9 +159,11 @@ static inline int simulate_link(char *buf, int len) /* Do we want to simulate delay? */ if (delay) { /* Random delay to add is capped to 10s */ - unsigned int applied_delay = RAND_PERCENT > 49 ? - delay + rand() % jitter : - delay - rand() % jitter; + unsigned int applied_delay = jitter ? + (RAND_PERCENT > 49 ? + delay + rand() % jitter : + delay - rand() % jitter) : + delay; applied_delay %= 10000; LOG_PKT_FMT(buf, "Delayed packet by %u ms\n", applied_delay); /* Create a slot for the packet queue */