Skip to content

Commit 8eb9a29

Browse files
committed
Fixing a use after free when trying to load a marker in a category that has attributes defined in another file
1 parent 731222b commit 8eb9a29

File tree

3 files changed

+30
-9
lines changed

3 files changed

+30
-9
lines changed

xml_converter/src/packaging_xml.cpp

+27-9
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,23 @@ static vector<Parseable*> parse_pois(rapidxml::xml_node<>* root_node, map<string
239239
return markers;
240240
}
241241

242+
class XMLFileData {
243+
public:
244+
unique_ptr<basic_istream<char>> raw_file;
245+
rapidxml::file<char>* xml_file;
246+
string display_path;
247+
rapidxml::xml_document<char> xml_document;
248+
249+
XMLFileData(unique_ptr<basic_istream<char>> input, const MarkerPackFile& filepath) {
250+
this->raw_file = std::move(input);
251+
this->xml_file = new rapidxml::file<char>(*this->raw_file);
252+
this->display_path = filepath.tmp_get_path();
253+
this->xml_document.parse<rapidxml::parse_non_destructive | rapidxml::parse_no_data_nodes>(this->xml_file->data(), this->display_path.c_str());
254+
}
255+
};
256+
257+
vector<XMLFileData*> xml_files_to_cleanup;
258+
242259
////////////////////////////////////////////////////////////////////////////////
243260
// parse_xml_file
244261
//
@@ -251,16 +268,10 @@ set<string> parse_xml_file(
251268
) {
252269
vector<XMLError*> errors;
253270

254-
unique_ptr<basic_istream<char>> infile = open_file_for_read(xml_filepath);
255-
256-
rapidxml::file<> xml_file(*infile);
257-
258-
string tmp_get_path = xml_filepath.tmp_get_path();
271+
XMLFileData* xml_file_data = new XMLFileData(open_file_for_read(xml_filepath), xml_filepath); // This is a memory leak
272+
xml_files_to_cleanup.push_back(xml_file_data);
259273

260-
rapidxml::xml_document<> doc;
261-
doc.parse<rapidxml::parse_non_destructive | rapidxml::parse_no_data_nodes>(xml_file.data(), tmp_get_path.c_str());
262-
263-
rapidxml::xml_node<>* root_node = doc.first_node();
274+
rapidxml::xml_node<>* root_node = xml_file_data->xml_document.first_node();
264275
// Validate the Root Node
265276
if (get_node_name(root_node) != "OverlayData") {
266277
errors.push_back(new XMLNodeNameError("Root node should be of type OverlayData", root_node));
@@ -295,6 +306,13 @@ set<string> parse_xml_file(
295306
return category_names;
296307
}
297308

309+
void cleanup_xml_files() {
310+
for (size_t i = 0; i < xml_files_to_cleanup.size(); i++) {
311+
delete xml_files_to_cleanup[i]->xml_file;
312+
delete xml_files_to_cleanup[i];
313+
}
314+
}
315+
298316
////////////////////////////////////////////////////////////////////////////////
299317
////////////////////////////////// SERIALIZE ///////////////////////////////////
300318
////////////////////////////////////////////////////////////////////////////////

xml_converter/src/packaging_xml.hpp

+2
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,5 @@ void write_xml_file(
2121
std::map<std::string, Category>* marker_categories,
2222
std::vector<Parseable*>* parsed_pois
2323
);
24+
25+
void cleanup_xml_files();

xml_converter/src/xml_converter.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ map<string, vector<string>> read_taco_directory(
5353
}
5454
}
5555

56+
cleanup_xml_files();
5657
return top_level_category_file_locations;
5758
}
5859

0 commit comments

Comments
 (0)