From e4a9787c5e6efb408dfb8280f51925e5a512b7d6 Mon Sep 17 00:00:00 2001 From: "Lubomir I. Ivanov" Date: Fri, 20 Oct 2017 04:22:32 +0300 Subject: dive.h: add handling of NULL in get_dive_dc() This line: dc = &dive->dc can SIGSEGV for a NULL 'dive' pointer. return NULL if 'dive' is NULL. Also handle NULL 'dc' in get_gasmix() and set 'ev' to NULL. Signed-off-by: Lubomir I. Ivanov --- core/dive.h | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'core') diff --git a/core/dive.h b/core/dive.h index b02ce5f6c..64dee5196 100644 --- a/core/dive.h +++ b/core/dive.h @@ -608,7 +608,10 @@ static inline unsigned int number_of_computers(struct dive *dive) static inline struct divecomputer *get_dive_dc(struct dive *dive, int nr) { - struct divecomputer *dc = &dive->dc; + struct divecomputer *dc; + if (!dive) + return NULL; + dc = &dive->dc; while (nr-- > 0) { dc = dc->next; @@ -941,7 +944,7 @@ static inline struct gasmix *get_gasmix(struct dive *dive, struct divecomputer * if (!gasmix) { int cyl = explicit_first_cylinder(dive, dc); gasmix = &dive->cylinder[cyl].gasmix; - ev = dc->events; + ev = dc ? dc->events : NULL; } while (ev && ev->time.seconds < (unsigned int)time) { gasmix = get_gasmix_from_event(dive, ev); -- cgit v1.2.3-70-g09d2