From 3fa7da804d1d558fa0a39262a5d13f859b6357fa Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Wed, 13 Jul 2016 14:47:03 -0400 Subject: [PATCH] stepcompress: Check for invalid count in step_factor and step_sqrt Check for an invalid count and report an error if found. This prevents some segfaults when count goes negative. Signed-off-by: Kevin O'Connor --- klippy/stepcompress.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/klippy/stepcompress.c b/klippy/stepcompress.c index 569522a6..34b2a7d0 100644 --- a/klippy/stepcompress.c +++ b/klippy/stepcompress.c @@ -280,6 +280,11 @@ stepcompress_push_factor(struct stepcompress *sc double ceil_steps = ceil(steps - step_offset); double next_step_offset = ceil_steps - (steps - step_offset); int count = ceil_steps; + if (count < 0 || count > 1000000) { + fprintf(stderr, "ERROR: push_factor invalid count %d %f %f %f %f\n" + , sc->oid, steps, step_offset, clock_offset, factor); + return next_step_offset; + } check_expand(sc, count); // Calculate each step time @@ -304,6 +309,12 @@ stepcompress_push_sqrt(struct stepcompress *sc, double steps, double step_offset double ceil_steps = ceil(steps - step_offset); double next_step_offset = ceil_steps - (steps - step_offset); int count = ceil_steps; + if (count < 0 || count > 1000000) { + fprintf(stderr, "ERROR: push_sqrt invalid count %d %f %f %f %f %f\n" + , sc->oid, steps, step_offset, clock_offset, sqrt_offset + , factor); + return next_step_offset; + } check_expand(sc, count); // Calculate each step time @@ -320,7 +331,7 @@ stepcompress_push_sqrt(struct stepcompress *sc, double steps, double step_offset *qn++ = clock_offset - sqrt(pos*factor); pos += 1.0; } - sc->queue_next = end; + sc->queue_next = qn; return next_step_offset; } @@ -405,7 +416,7 @@ struct steppersync { int num_move_clocks; }; -// Allocate a new 'stepperysync' object +// Allocate a new 'steppersync' object struct steppersync * steppersync_alloc(struct serialqueue *sq, struct stepcompress **sc_list , int sc_num, int move_num)