A few basic rules for gas validation:
        We can't have <0%, or >100% of either O2 or He
        O2 + He must not be >100%
        Switch depth can't be <0%

This places limits on user-input values

Signed-off-by: Rick Walsh <[email protected]>
---
 core/qthelper.cpp           | 9 +++++++++
 qt-models/cylindermodel.cpp | 6 ++++++
 2 files changed, 15 insertions(+)

diff --git a/core/qthelper.cpp b/core/qthelper.cpp
index 24c51d2..d6a6c25 100644
--- a/core/qthelper.cpp
+++ b/core/qthelper.cpp
@@ -1253,6 +1253,8 @@ depth_t string_to_depth(const char *str)
        QString local_m = QObject::tr("m");
        depth_t depth;
 
+       if (value < 0)
+               value = 0;
        if (rest.startsWith("m") || rest.startsWith(local_m))
                goto m;
        if (rest.startsWith("ft") || rest.startsWith(local_ft))
@@ -1328,6 +1330,13 @@ fraction_t string_to_fraction(const char *str)
        fraction_t fraction;
 
        fraction.permille = rint(value * 10);
+       /*
+        * Don't permit values less than zero or greater than 100%
+        */
+       if (fraction.permille < 0)
+               fraction.permille = 0;
+       else if (fraction.permille > 1000)
+               fraction.permille = 1000;
        return fraction;
 }
 
diff --git a/qt-models/cylindermodel.cpp b/qt-models/cylindermodel.cpp
index 1ad03f9..20b7207 100644
--- a/qt-models/cylindermodel.cpp
+++ b/qt-models/cylindermodel.cpp
@@ -249,6 +249,9 @@ bool CylindersModel::setData(const QModelIndex &index, 
const QVariant &value, in
        case O2:
                if (CHANGED()) {
                        cyl->gasmix.o2 = 
string_to_fraction(vString.toUtf8().data());
+                       // fO2 + fHe must not be greater than 1
+                       if (((cyl->gasmix.o2.permille == 0) ? O2_IN_AIR : 
cyl->gasmix.o2.permille) + cyl->gasmix.he.permille > 1000)
+                               cyl->gasmix.he.permille = 1000 - 
((cyl->gasmix.o2.permille == 0) ? O2_IN_AIR : cyl->gasmix.o2.permille);
                        pressure_t modpO2;
                        if (displayed_dive.dc.divemode == PSCR)
                                modpO2.mbar = prefs.decopo2 + (1000 - 
get_o2(&cyl->gasmix)) * SURFACE_PRESSURE *
@@ -262,6 +265,9 @@ bool CylindersModel::setData(const QModelIndex &index, 
const QVariant &value, in
        case HE:
                if (CHANGED()) {
                        cyl->gasmix.he = 
string_to_fraction(vString.toUtf8().data());
+                       // fO2 + fHe must not be greater than 1
+                       if (((cyl->gasmix.o2.permille == 0) ? O2_IN_AIR : 
cyl->gasmix.o2.permille) + cyl->gasmix.he.permille > 1000)
+                               cyl->gasmix.o2.permille = 1000 - 
cyl->gasmix.he.permille;
                        changed = true;
                }
                break;
-- 
2.5.5

_______________________________________________
subsurface mailing list
[email protected]
http://lists.subsurface-divelog.org/cgi-bin/mailman/listinfo/subsurface

Reply via email to