Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix segfault in deprecated newPhase factory function #1600

Merged
merged 1 commit into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion src/thermo/ThermoFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,27 @@
warn_deprecated("newPhase",
"To be removed after Cantera 3.0; superseded by\n"
"newThermo(const string&, const string&).");
return newThermo(infile, id).get();
size_t dot = infile.find_last_of(".");
if (dot == npos) {
newThermoModel(infile);
}
string extension;
extension = toLowerCopy(infile.substr(dot+1));
string id_ = id;
if (id == "-") {
id_ = "";
}
if (extension == "cti" || extension == "xml") {
throw CanteraError("newPhase",
"The CTI and XML formats are no longer supported.");

Check warning on line 217 in src/thermo/ThermoFactory.cpp

View check run for this annotation

Codecov / codecov/patch

src/thermo/ThermoFactory.cpp#L217

Added line #L217 was not covered by tests
}

AnyMap root = AnyMap::fromYamlFile(infile);
AnyMap& phase = root["phases"].getMapWhere("name", id_);
string model = phase["thermo"].asString();
unique_ptr<ThermoPhase> t(ThermoFactory::factory()->create(model));
setupPhase(*t, phase, root);
return t.release();
}

void addDefaultElements(ThermoPhase& thermo, const vector<string>& element_names) {
Expand Down
11 changes: 11 additions & 0 deletions test/thermo/ThermoPhase_Test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,17 @@ TEST_F(TestThermoMethods, setConcentrations)
EXPECT_NEAR(thermo->moleFraction(2), -1e-8 / ctot, 1e-16);
}

TEST(ThermoConstructors, newPhase)
{
suppress_deprecation_warnings();
// Test deprecated newPhase(infile, phasename) factory function
unique_ptr<ThermoPhase> gas(newPhase("h2o2.yaml", ""));
gas->setState_TPX(400, 2 * OneAtm, "H2:1.0, O2:1.0");
EXPECT_NEAR(gas->moleFraction("H2"), 0.5, 1e-8);
EXPECT_NEAR(gas->pressure(), 2 * OneAtm, 1e-5);
make_deprecation_warnings_fatal();
}

class EquilRatio_MixFrac_Test : public testing::Test
{
public:
Expand Down
Loading