summaryrefslogtreecommitdiffstats
path: root/qt-models/cylindermodel.cpp
diff options
context:
space:
mode:
authorGravatar Berthold Stoeger <bstoeger@mail.tuwien.ac.at>2019-08-04 22:13:49 +0200
committerGravatar Dirk Hohndel <dirk@hohndel.org>2019-11-09 19:19:04 +0100
commit794066b2367082851858d4e0da8b6e388d2acabb (patch)
tree81aad4f5e50c096a25d4bf59491a05ec250b6bc9 /qt-models/cylindermodel.cpp
parent52d8d89f73542eb8ab3577bc55b466e7ca73bfc7 (diff)
downloadsubsurface-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 'qt-models/cylindermodel.cpp')
-rw-r--r--qt-models/cylindermodel.cpp16
1 files changed, 8 insertions, 8 deletions
diff --git a/qt-models/cylindermodel.cpp b/qt-models/cylindermodel.cpp
index f64cc213a..22fc5ae8b 100644
--- a/qt-models/cylindermodel.cpp
+++ b/qt-models/cylindermodel.cpp
@@ -134,7 +134,7 @@ QVariant CylindersModel::data(const QModelIndex &index, int role) const
if (!index.isValid() || index.row() >= rows)
return QVariant();
- const cylinder_t *cyl = &displayed_dive.cylinders.cylinders[index.row()];
+ const cylinder_t *cyl = get_cylinder(&displayed_dive, index.row());
switch (role) {
case Qt::BackgroundRole: {
@@ -259,7 +259,7 @@ QVariant CylindersModel::data(const QModelIndex &index, int role) const
cylinder_t *CylindersModel::cylinderAt(const QModelIndex &index)
{
- return &displayed_dive.cylinders.cylinders[index.row()];
+ return get_cylinder(&displayed_dive, index.row());
}
// this is our magic 'pass data in' function that allows the delegate to get
@@ -449,7 +449,7 @@ static bool show_cylinder(struct dive *dive, int i)
if (is_cylinder_used(dive, i))
return true;
- cylinder_t *cyl = dive->cylinders.cylinders + i;
+ cylinder_t *cyl = get_cylinder(dive, i);
if (cyl->start.mbar || cyl->sample_start.mbar ||
cyl->end.mbar || cyl->sample_end.mbar)
return true;
@@ -533,10 +533,10 @@ void CylindersModel::moveAtFirst(int cylid)
cylinder_t temp_cyl;
beginMoveRows(QModelIndex(), cylid, cylid, QModelIndex(), 0);
- memmove(&temp_cyl, &displayed_dive.cylinders.cylinders[cylid], sizeof(temp_cyl));
+ memmove(&temp_cyl, get_cylinder(&displayed_dive, cylid), sizeof(temp_cyl));
for (int i = cylid - 1; i >= 0; i--)
- memmove(&displayed_dive.cylinders.cylinders[i + 1], &displayed_dive.cylinders.cylinders[i], sizeof(temp_cyl));
- memmove(&displayed_dive.cylinders.cylinders[0], &temp_cyl, sizeof(temp_cyl));
+ memmove(get_cylinder(&displayed_dive, i + 1), get_cylinder(&displayed_dive, i), sizeof(temp_cyl));
+ memmove(get_cylinder(&displayed_dive, 0), &temp_cyl, sizeof(temp_cyl));
// Create a mapping of cylinder indexes:
// 1) Fill mapping[0]..mapping[cyl] with 0..index
@@ -558,7 +558,7 @@ void CylindersModel::updateDecoDepths(pressure_t olddecopo2)
pressure_t decopo2;
decopo2.mbar = prefs.decopo2;
for (int i = 0; i < displayed_dive.cylinders.nr; i++) {
- cylinder_t *cyl = &displayed_dive.cylinders.cylinders[i];
+ cylinder_t *cyl = get_cylinder(&displayed_dive, i);
/* If the gas's deco MOD matches the old pO2, it will have been automatically calculated and should be updated.
* If they don't match, we should leave the user entered depth as it is */
if (cyl->depth.mm == gas_mod(cyl->gasmix, olddecopo2, &displayed_dive, M_OR_FT(3, 10)).mm) {
@@ -578,7 +578,7 @@ bool CylindersModel::updateBestMixes()
// Check if any of the cylinders are best mixes, update if needed
bool gasUpdated = false;
for (int i = 0; i < displayed_dive.cylinders.nr; i++) {
- cylinder_t *cyl = &displayed_dive.cylinders.cylinders[i];
+ cylinder_t *cyl = get_cylinder(&displayed_dive, i);
if (cyl->bestmix_o2) {
cyl->gasmix.o2 = best_o2(displayed_dive.maxdepth, &displayed_dive);
// fO2 + fHe must not be greater than 1