diff options
author | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2019-08-04 22:13:49 +0200 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2019-11-09 19:19:04 +0100 |
commit | 794066b2367082851858d4e0da8b6e388d2acabb (patch) | |
tree | 81aad4f5e50c096a25d4bf59491a05ec250b6bc9 /core/equipment.c | |
parent | 52d8d89f73542eb8ab3577bc55b466e7ca73bfc7 (diff) | |
download | subsurface-794066b2367082851858d4e0da8b6e388d2acabb.tar.gz |
Cylinders: access cylinders with get_cylinder()
Instead of accessing the cylinder table directly, use the get_cylinder()
function. This gives less unwieldy expressions. But more importantly,
the function does bound checking. This is crucial for now as the code
hasn't be properly audited since the change to arbitrarily sized
cylinder tables. Accesses of invalid cylinder indexes may lead to
silent data-corruption that is sometimes not even noticed by
valgrind. Returning NULL instead of an invalid pointer will make
debugging much easier.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Diffstat (limited to 'core/equipment.c')
-rw-r--r-- | core/equipment.c | 8 |
1 files changed, 4 insertions, 4 deletions
diff --git a/core/equipment.c b/core/equipment.c index 3a2e6db4b..39505a39d 100644 --- a/core/equipment.c +++ b/core/equipment.c @@ -279,7 +279,7 @@ void reset_cylinders(struct dive *dive, bool track_gas) pressure_t decopo2 = {.mbar = prefs.decopo2}; for (int i = 0; i < dive->cylinders.nr; i++) { - cylinder_t *cyl = &dive->cylinders.cylinders[i]; + cylinder_t *cyl = get_cylinder(dive, i); if (cyl->depth.mm == 0) /* if the gas doesn't give a mod, calculate based on prefs */ cyl->depth = gas_mod(cyl->gasmix, decopo2, dive, M_OR_FT(3,10)); if (track_gas) @@ -308,10 +308,10 @@ void copy_cylinder_types(const struct dive *s, struct dive *d) return; for (i = 0; i < s->cylinders.nr && i < d->cylinders.nr; i++) - copy_cylinder_type(s->cylinders.cylinders + i, d->cylinders.cylinders + i); + copy_cylinder_type(get_cylinder(s, i), get_cylinder(d, i)); for ( ; i < s->cylinders.nr; i++) - add_cloned_cylinder(&d->cylinders, s->cylinders.cylinders[i]); + add_cloned_cylinder(&d->cylinders, *get_cylinder(s, i)); } cylinder_t *add_empty_cylinder(struct cylinder_table *t) @@ -355,7 +355,7 @@ void dump_cylinders(struct dive *dive, bool verbose) { printf("Cylinder list:\n"); for (int i = 0; i < dive->cylinders; i++) { - cylinder_t *cyl = &dive->cylinders.cylinders[i]; + cylinder_t *cyl = get_cylinder(dive, i); printf("%02d: Type %s, %3.1fl, %3.0fbar\n", i, cyl->type.description, cyl->type.size.mliter / 1000.0, cyl->type.workingpressure.mbar / 1000.0); printf(" Gasmix O2 %2.0f%% He %2.0f%%\n", cyl->gasmix.o2.permille / 10.0, cyl->gasmix.he.permille / 10.0); |