From 4a6dd84f81c77789a073f9ed8776f79be60345ef Mon Sep 17 00:00:00 2001 From: Ray Speth Date: Wed, 2 Apr 2014 15:26:20 +0000 Subject: [PATCH] [Thermo] Check composition strings for invalid float literals --- interfaces/cython/cantera/test/test_thermo.py | 10 ++++++++++ src/base/stringUtils.cpp | 4 ++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/interfaces/cython/cantera/test/test_thermo.py b/interfaces/cython/cantera/test/test_thermo.py index 732831fdf..5c101a9ff 100644 --- a/interfaces/cython/cantera/test/test_thermo.py +++ b/interfaces/cython/cantera/test/test_thermo.py @@ -69,6 +69,16 @@ class TestThermoPhase(utilities.CanteraTest): self.assertRaises(Exception, set_bad) + def test_setCompositionStringBad(self): + X0 = self.phase.X + with self.assertRaises(Exception): + self.phase.X = 'H2:1.0, O2:asdf' + self.assertArrayNear(X0, self.phase.X) + + with self.assertRaises(Exception): + self.phase.X = 'H2:1e-x4' + self.assertArrayNear(X0, self.phase.X) + def test_report(self): report = self.phase.report() self.assertTrue(self.phase.name in report) diff --git a/src/base/stringUtils.cpp b/src/base/stringUtils.cpp index ab4e92349..f014eecbd 100644 --- a/src/base/stringUtils.cpp +++ b/src/base/stringUtils.cpp @@ -136,7 +136,7 @@ compositionMap parseCompString(const std::string& ss, if (icolon != std::string::npos) { std::string name = stripws(s.substr(0, icolon)); if (iend != std::string::npos) { - num = s.substr(icolon+1, iend-icolon); + num = s.substr(icolon+1, iend-icolon-1); s = s.substr(iend+1, s.size()); } else { num = s.substr(icolon+1, s.size()); @@ -146,7 +146,7 @@ compositionMap parseCompString(const std::string& ss, throw CanteraError("parseCompString", "unknown species " + name); } - x[name] = fpValue(num); + x[name] = fpValueCheck(num); } else { s = ""; }