From 1a05f34ae89d35f6ece12dba7b6ab5f60df98705 Mon Sep 17 00:00:00 2001 From: Linus Torvalds Date: Sat, 18 Aug 2012 09:02:27 -0700 Subject: [PATCH] Make fixup_divep robust against insane dive times This fixes the case of the dive duration being zero, or being shorter than the assumed ascent/descent time. Reported-by: Lutz Vieweg Signed-off-by: Linus Torvalds --- dive.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/dive.c b/dive.c index f4bf497..aee09d5 100644 --- a/dive.c +++ b/dive.c @@ -472,11 +472,18 @@ struct dive *fixup_dive(struct dive *dive) cyl->sample_end.mbar = 0; } } - if (end < 0) - { + if (end < 0) { /* Assume an ascent/descent rate of 9 m/min */ - int asc_desc_time = dive->maxdepth.mm*60/9000; - dive->meandepth.mm = dive->maxdepth.mm*(dive->duration.seconds-asc_desc_time)/dive->duration.seconds; + int depth = dive->maxdepth.mm; + int asc_desc_time = depth*60/9000; + int duration = dive->duration.seconds; + + /* Protect against insane dives - make mean be half of max */ + if (duration <= asc_desc_time) { + duration = 2; + asc_desc_time = 1; + } + dive->meandepth.mm = depth*(duration-asc_desc_time)/duration; return dive; } -- 2.45.2