diff options
-rw-r--r-- | core/load-git.c | 12 | ||||
-rw-r--r-- | core/parse.c | 12 |
2 files changed, 20 insertions, 4 deletions
diff --git a/core/load-git.c b/core/load-git.c index 9753e0586..c44a880ae 100644 --- a/core/load-git.c +++ b/core/load-git.c @@ -642,6 +642,14 @@ static char *parse_sample_unit(struct sample *sample, double val, char *unit) } /* + * If the given cylinder doesn't exist, return NO_SENSOR. + */ +static uint8_t sanitize_sensor_id(const struct dive *d, int nr) +{ + return d && nr >= 0 && nr < d->cylinders.nr ? nr : NO_SENSOR; +} + +/* * By default the sample data does not change unless the * save-file gives an explicit new value. So we copy the * data from the previous sample if one exists, and then @@ -667,8 +675,8 @@ static struct sample *new_sample(struct git_parser_state *state) sample->pressure[0].mbar = 0; sample->pressure[1].mbar = 0; } else { - sample->sensor[0] = !state->o2pressure_sensor; - sample->sensor[1] = state->o2pressure_sensor; + sample->sensor[0] = sanitize_sensor_id(state->active_dive, !state->o2pressure_sensor); + sample->sensor[1] = sanitize_sensor_id(state->active_dive, state->o2pressure_sensor); } return sample; } diff --git a/core/parse.c b/core/parse.c index 459108a26..241d5763f 100644 --- a/core/parse.c +++ b/core/parse.c @@ -365,6 +365,14 @@ void ws_end(struct parser_state *state) } /* + * If the given cylinder doesn't exist, return NO_SENSOR. + */ +static uint8_t sanitize_sensor_id(const struct dive *d, int nr) +{ + return d && nr >= 0 && nr < d->cylinders.nr ? nr : NO_SENSOR; +} + +/* * By default the sample data does not change unless the * save-file gives an explicit new value. So we copy the * data from the previous sample if one exists, and then @@ -392,8 +400,8 @@ void sample_start(struct parser_state *state) sample->pressure[0].mbar = 0; sample->pressure[1].mbar = 0; } else { - sample->sensor[0] = !state->o2pressure_sensor; - sample->sensor[1] = state->o2pressure_sensor; + sample->sensor[0] = sanitize_sensor_id(state->cur_dive, !state->o2pressure_sensor); + sample->sensor[1] = sanitize_sensor_id(state->cur_dive, state->o2pressure_sensor); } state->cur_sample = sample; state->next_o2_sensor = 0; |