Skip to content

Commit

Permalink
[Thermo] Fix segfault in deprecated newPhase factory function
Browse files Browse the repository at this point in the history
The shared_ptr goes out of scope at the end of the function,
deleting the held ThermoPhase right as it's returned.

Erroneous behavior introduced in f3e840d (#1448).
  • Loading branch information
speth authored and ischoegl committed Aug 22, 2023
1 parent 8e2ab16 commit 217f35b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
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 @@ ThermoPhase* newPhase(const string& infile, string id)
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.");
}

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

0 comments on commit 217f35b

Please sign in to comment.