diff options
author | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2019-12-23 22:02:10 +0100 |
---|---|---|
committer | bstoeger <32835590+bstoeger@users.noreply.github.com> | 2020-03-06 10:00:13 +0100 |
commit | 62adc24d150b0566b9cc9ab449de4ed30ac2ab62 (patch) | |
tree | 293e64c0295368110bc5f858be9d75bd2a062962 /core | |
parent | 763dbf256a7e2ee0b9a15829135f6862cd8039f5 (diff) | |
download | subsurface-62adc24d150b0566b9cc9ab449de4ed30ac2ab62.tar.gz |
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 <bstoeger@mail.tuwien.ac.at>
Diffstat (limited to 'core')
-rw-r--r-- | core/dive.c | 10 |
1 files 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) |