From 4aebcdc021391fcbdee089b5e14c11ebab517885 Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Mon, 20 Apr 2020 21:28:17 +0200 Subject: core: return -1 from explicit_first_cylinder() if dive has no cylinders Arguably, returning 0 for a dive with no cylinders is wrong, since the 0 is a valid cylinder id, however that cylinder doesn't exist. Instead, return -1. All callers of explicit_first_cylinder() return early anyway for dives with no cylinders. Signed-off-by: Berthold Stoeger --- core/dive.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'core') diff --git a/core/dive.c b/core/dive.c index 1658d3bdd..89e090165 100644 --- a/core/dive.c +++ b/core/dive.c @@ -961,10 +961,13 @@ static int same_rounded_pressure(pressure_t a, pressure_t b) * 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. - * We likewise return 0 if the event concerns a cylinder that doesn't exist. */ + * We likewise return 0 if the event concerns a cylinder that doesn't exist. + * If the dive has no cylinders, -1 is returned. */ int explicit_first_cylinder(const struct dive *dive, const struct divecomputer *dc) { int res = 0; + if (!dive->cylinders.nr) + return -1; 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)) -- cgit v1.2.3-70-g09d2