From 62adc24d150b0566b9cc9ab449de4ed30ac2ab62 Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Mon, 23 Dec 2019 22:02:10 +0100 Subject: [PATCH] Core: don't return invalid cylinders from explicit_first_cylinder() For reasons which I don't yet understand, when plotting a dive whose first cylinder is not cylinder 0 and then plotting a dive with only one cylinder, it can happen that for the latter explicit_first_cylinder() returns an erroneous value. This is due to the way in which we copy the dive to be plotted to displayed_dive. For now, make sure that no invalid cylinder is returned to avoid crashes. This will have to be changed anyway, since this is very fundamentally not thread-safe and inefficient. Signed-off-by: Berthold Stoeger --- core/dive.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/core/dive.c b/core/dive.c index ca4b0b705..71b1038ec 100644 --- a/core/dive.c +++ b/core/dive.c @@ -865,17 +865,19 @@ static int same_rounded_pressure(pressure_t a, pressure_t b) /* Some dive computers (Cobalt) don't start the dive with cylinder 0 but explicitly * tell us what the first gas is with a gas change event in the first sample. * Sneakily we'll use a return value of 0 (or FALSE) when there is no explicit - * first cylinder - in which case cylinder 0 is indeed the first cylinder */ + * first cylinder - in which case cylinder 0 is indeed the first cylinder. + * We likewise return 0 if the event concerns a cylinder that doesn't exist. */ int explicit_first_cylinder(const struct dive *dive, const struct divecomputer *dc) { + int res = 0; if (dc) { const struct event *ev = get_next_event(dc->events, "gaschange"); if (ev && ((dc->sample && ev->time.seconds == dc->sample[0].time.seconds) || ev->time.seconds <= 1)) - return get_cylinder_index(dive, ev); + res = get_cylinder_index(dive, ev); else if (dc->divemode == CCR) - return MAX(get_cylinder_idx_by_use(dive, DILUENT), 0); + res = MAX(get_cylinder_idx_by_use(dive, DILUENT), 0); } - return 0; + return res < dive->cylinders.nr ? res : 0; } /* this gets called when the dive mode has changed (so OC vs. CC)