Skip to content

Commit e33e66a

Browse files
pitkajuhpshriwisepaulromano
authored
Fix openmc-dev#2994, non-existent path causes segmentation fault when saving plot (openmc-dev#3038)
Co-authored-by: Patrick Shriwise <pshriwise@gmail.com> Co-authored-by: Paul Romano <paul.k.romano@gmail.com>
1 parent b0732cb commit e33e66a

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

‎src/plot.cpp‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,11 @@ void Plot::set_output_path(pugi::xml_node plot_node)
315315
} else {
316316
filename = fmt::format("plot_{}", id());
317317
}
318+
const std::string dir_if_present =
319+
filename.substr(0, filename.find_last_of("/") + 1);
320+
if (dir_if_present.size() > 0 && !dir_exists(dir_if_present)) {
321+
fatal_error(fmt::format("Directory '{}' does not exist!", dir_if_present));
322+
}
318323
// add appropriate file extension to name
319324
switch (type_) {
320325
case PlotType::slice:

‎tests/unit_tests/test_plots.py‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def myplot():
1111
plot.width = (100., 100.)
1212
plot.origin = (2., 3., -10.)
1313
plot.pixels = (500, 500)
14-
plot.filename = 'myplot'
14+
plot.filename = './not-a-dir/myplot'
1515
plot.type = 'slice'
1616
plot.basis = 'yz'
1717
plot.background = 'black'
@@ -221,3 +221,26 @@ def test_voxel_plot_roundtrip():
221221
assert new_plot.origin == plot.origin
222222
assert new_plot.width == plot.width
223223
assert new_plot.color_by == plot.color_by
224+
225+
226+
def test_plot_directory(run_in_tmpdir):
227+
pwr_pin = openmc.examples.pwr_pin_cell()
228+
229+
# create a standard plot, expected to work
230+
plot = openmc.Plot()
231+
plot.filename = 'plot_1'
232+
plot.type = 'slice'
233+
plot.pixels = (10, 10)
234+
plot.color_by = 'material'
235+
plot.width = (100., 100.)
236+
pwr_pin.plots = [plot]
237+
pwr_pin.plot_geometry()
238+
239+
# use current directory, also expected to work
240+
plot.filename = './plot_1'
241+
pwr_pin.plot_geometry()
242+
243+
# use a non-existent directory, should raise an error
244+
plot.filename = './not-a-dir/plot_1'
245+
with pytest.raises(RuntimeError, match='does not exist'):
246+
pwr_pin.plot_geometry()

0 commit comments

Comments
 (0)