Skip to content

Commit 40dbde4

Browse files
committed
Check file is not empty to prevent core dump
1 parent b49f0d1 commit 40dbde4

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

src/utilities/data_io.cxx

+13
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ toml::value read_data_from_toml(const ghc::filesystem::path file_path){
2323
throw toml_error("File '" + file_path.string() +
2424
"' could not be opened as it does not exist");
2525
}
26+
if (ghc::filesystem::is_empty(file_path)) {
27+
throw toml_error("File '" + file_path.string() +
28+
"' appears to be empty");
29+
}
2630

2731
auto toml_data_ = toml::parse(file_path.string());
2832
return toml_data_;
@@ -95,6 +99,10 @@ std::string get_first_component(const ghc::filesystem::path &file_path){
9599
throw toml_error("File '" + file_path.string() +
96100
"' could not be opened as it does not exist");
97101
}
102+
if (ghc::filesystem::is_empty(file_path)) {
103+
throw toml_error("File '" + file_path.string() +
104+
"' appears to be empty");
105+
}
98106
const auto toml_data_ = toml::parse(file_path.string());
99107

100108
return toml_data_.as_table().begin()->first;
@@ -105,6 +113,10 @@ bool component_exists(const ghc::filesystem::path &file_path, const std::string
105113
{
106114
return false;
107115
}
116+
if (ghc::filesystem::is_empty(file_path)) {
117+
throw toml_error("File '" + file_path.string() +
118+
"' appears to be empty");
119+
}
108120
const auto toml_data_ = toml::parse(file_path.string());
109121
if(!toml_data_.contains(component)){
110122
return false;
@@ -115,4 +127,5 @@ bool component_exists(const ghc::filesystem::path &file_path, const std::string
115127
}
116128

117129
}
130+
118131
}; // namespace FairDataPipeline

test/test_io.cxx

+3-5
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,8 @@ TEST_F(IOTest, TestInvalidToml) {
184184
}
185185

186186
try{
187-
ghc::filesystem::path empty_file( ghc::filesystem::path(TESTDIR) / "data" / "temp" / "empty_file.toml");
188-
std::ofstream output_file;
189-
output_file.open(empty_file.string());
190-
output_file.close();
187+
ghc::filesystem::path empty_file = ghc::filesystem::path(TESTDIR) / "data" / "temp" / "empty_file.toml";
188+
std::ofstream output_file(empty_file.string());
191189
read_point_estimate_from_toml(empty_file);
192190
FAIL();
193191
}
@@ -199,6 +197,6 @@ TEST_F(IOTest, TestInvalidToml) {
199197
}
200198
catch(...){
201199
FAIL() << "Exception Thrown" << std::endl;
202-
}
200+
}
203201

204202
}

0 commit comments

Comments
 (0)