parser: don't create samples with invalid cylinder ids

By default, the parser would create samples with cylinder
ids 0 and 1. This creates out-of-bound accesses for the
common one-cylinder (or even no-cylinder) dives. These
were harmless when the cylinder-table was of a fixed size.
Since changing to a dynamic cylinder-table, these became
actual out-of-bound accesses. Don't create such samples
in the parser.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2021-07-18 12:33:31 +02:00 committed by Dirk Hohndel
parent 7e11a35371
commit 361678dcbe
2 changed files with 20 additions and 4 deletions

View file

@ -641,6 +641,14 @@ static char *parse_sample_unit(struct sample *sample, double val, char *unit)
return end;
}
/*
* 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
@ -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;
}

View file

@ -364,6 +364,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
@ -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;