Fix potential crash with old XML files

A file with no divecomputer section in a dive can trigger a SEGV as cur_dc
could be NULL.

Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Dirk Hohndel 2013-01-18 10:39:25 -08:00
parent edfdef1bbc
commit 62c4ec20c7

View file

@ -648,6 +648,15 @@ static void try_to_fill_event(const char *name, char *buf)
nonmatch("event", name, buf);
}
/*
* If we don't have an explicit dive computer,
* we use the implicit one that every dive has..
*/
static struct divecomputer *get_dc(void)
{
return cur_dc ? : &cur_dive->dc;
}
/* We're in the top-level dive xml. Try to convert whatever value to a dive value */
static void try_to_fill_dc(struct divecomputer *dc, const char *name, char *buf)
{
@ -690,7 +699,7 @@ static void get_cylinderindex(char *buffer, void *_i)
int *i = _i;
*i = atoi(buffer);
if (lastcylinderindex != *i) {
add_gas_switch_event(cur_dive, cur_dc, cur_sample->time.seconds, *i);
add_gas_switch_event(cur_dive, get_dc(), cur_sample->time.seconds, *i);
lastcylinderindex = *i;
}
}
@ -1159,15 +1168,6 @@ static void event_start(void)
cur_event.active = 1;
}
/*
* If we don't have an explicit dive computer,
* we use the implicit one that every dive has..
*/
static struct divecomputer *get_dc(void)
{
return cur_dc ? : &cur_dive->dc;
}
static void event_end(void)
{
struct divecomputer *dc = get_dc();