Skip to content

Commit 359f12f

Browse files
authored
prevent segfault whan empty sequence passet to GasState::mix_rats setter (#308)
1 parent fb11c65 commit 359f12f

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

src/gas_state.hpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,13 @@ struct GasState {
117117
}
118118

119119
static void set_mix_rats(const GasState &self, const nlohmann::json &json) {
120+
if (json.size() == 0)
121+
throw std::runtime_error("Non-empty sequence of mixing ratios expected");
122+
120123
GimmickGuard<InputGimmick> guard(json);
121-
f_gas_state_from_json(self.ptr.f_arg(),
122-
self.gas_data->ptr.f_arg());
124+
f_gas_state_from_json(
125+
self.ptr.f_arg(),
126+
self.gas_data->ptr.f_arg()
127+
);
123128
}
124129
};

tests/test_gas_state.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,3 +130,16 @@ def test_set_mix_rats_from_json():
130130
for i_spec in range(gas_data.n_spec):
131131
if not i_spec in idx_set:
132132
assert sut[i_spec] == 0
133+
134+
@staticmethod
135+
def test_set_mix_rats_empty():
136+
# arrange
137+
gas_data = ppmc.GasData(("SO2",))
138+
sut = ppmc.GasState(gas_data)
139+
140+
# act
141+
with pytest.raises(RuntimeError) as excinfo:
142+
sut.mix_rats = ()
143+
144+
# assert
145+
assert str(excinfo.value) == "Non-empty sequence of mixing ratios expected"

0 commit comments

Comments
 (0)