summaryrefslogtreecommitdiffstats
path: root/dive.c
diff options
context:
space:
mode:
authorGravatar Dirk Hohndel <dirk@hohndel.org>2015-06-21 21:38:12 -0700
committerGravatar Dirk Hohndel <dirk@hohndel.org>2015-06-21 21:40:21 -0700
commit18d52ec86efcc4e42f2bad0fc47ac1a3d5b69435 (patch)
treec72fef659ae85c7408b2f520f1bdfb60c7f65227 /dive.c
parent52ee5f28c2f19ea386398436e446b23fb7a080ff (diff)
downloadsubsurface-18d52ec86efcc4e42f2bad0fc47ac1a3d5b69435.tar.gz
Prevent possible null pointer dereference
Neither of these functions should ever be called with dc == NULL. But it's easy to prevent the potential crash. Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Diffstat (limited to 'dive.c')
-rw-r--r--dive.c17
1 files changed, 10 insertions, 7 deletions
diff --git a/dive.c b/dive.c
index f187e13c8..37f99048f 100644
--- a/dive.c
+++ b/dive.c
@@ -728,6 +728,8 @@ void per_cylinder_mean_depth(struct dive *dive, struct divecomputer *dc, int *me
for (i = 0; i < MAX_CYLINDERS; i++)
mean[i] = duration[i] = 0;
+ if (!dc)
+ return;
struct event *ev = get_next_event(dc->events, "gaschange");
if (!ev || (dc && dc->sample && ev->time.seconds == dc->sample[0].time.seconds && get_next_event(ev->next, "gaschange") == NULL)) {
// we have either no gas change or only one gas change and that's setting an explicit first cylinder
@@ -861,13 +863,14 @@ static int same_rounded_pressure(pressure_t a, pressure_t b)
* first cylinder - in which case cylinder 0 is indeed the first cylinder */
int explicit_first_cylinder(struct dive *dive, struct divecomputer *dc)
{
- struct event *ev = get_next_event(dc->events, "gaschange");
- if (ev && dc && dc->sample && ev->time.seconds == dc->sample[0].time.seconds)
- return get_cylinder_index(dive, ev);
- else if (dc->divemode == CCR)
- return MAX(get_cylinder_idx_by_use(dive, DILUENT), 0);
- else
- return 0;
+ if (dc) {
+ struct event *ev = get_next_event(dc->events, "gaschange");
+ if (ev && dc->sample && ev->time.seconds == dc->sample[0].time.seconds)
+ return get_cylinder_index(dive, ev);
+ else if (dc->divemode == CCR)
+ return MAX(get_cylinder_idx_by_use(dive, DILUENT), 0);
+ }
+ return 0;
}
/* this gets called when the dive mode has changed (so OC vs. CC)