diff options
author | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2021-07-18 12:33:31 +0200 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2021-07-19 10:13:51 -0700 |
commit | 361678dcbea78d5d4155439eb90936e3f0f36114 (patch) | |
tree | 0f423b30bc64c5242cb0b17a5ed56ae8cce0c897 | |
parent | 7e11a3537102697604dbb971a0f3800b25db5ffe (diff) | |
download | subsurface-361678dcbea78d5d4155439eb90936e3f0f36114.tar.gz |
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>
-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; |