fix(gsc): use vector for rawfile (#64)

This commit is contained in:
Xenxo Espasandín 2023-02-12 15:58:24 +01:00 committed by GitHub
parent 817c07dabf
commit f99a56542a
3 changed files with 9 additions and 15 deletions

View File

@ -605,23 +605,22 @@ auto context::make_token(std::string_view str) const -> std::string
auto context::load_header(std::string const& name) -> std::tuple<std::string const*, char const*, usize> auto context::load_header(std::string const& name) -> std::tuple<std::string const*, char const*, usize>
{ {
// todo: remove cache to prevent use after free if files are read from fs by the game
auto const itr = header_files_.find(name); auto const itr = header_files_.find(name);
if (itr != header_files_.end()) if (itr != header_files_.end())
{ {
return { &itr->first, reinterpret_cast<char const*>(itr->second.data), itr->second.size }; return { &itr->first, reinterpret_cast<char const*>(itr->second.data()), itr->second.size() };
} }
auto data = fs_callback_(name); auto data = fs_callback_(name);
if (data.first.data != nullptr && data.first.size != 0 && data.second.size() == 0) if (data.first.data == nullptr && data.first.size == 0 && !data.second.empty())
{ {
auto const res = header_files_.insert({ name, data.first }); auto const res = header_files_.insert({ name, std::move(data.second) });
if (res.second) if (res.second)
{ {
return { &res.first->first, reinterpret_cast<char const*>(res.first->second.data), res.first->second.size }; return { &res.first->first, reinterpret_cast<char const*>(res.first->second.data()), res.first->second.size() };
} }
} }
@ -644,13 +643,13 @@ auto context::load_include(std::string const& name) -> bool
auto file = fs_callback_(name); auto file = fs_callback_(name);
if (file.first.data == nullptr && file.first.size == 0) if ((file.first.data == nullptr || file.first.size == 0) && file.second.empty())
throw std::runtime_error("empty file"); throw std::runtime_error("empty file");
if (file.second.size() == 0) if (file.first.data == nullptr && file.first.size == 0 && !file.second.empty())
{ {
// process RawFile // process RawFile
auto prog = source_.parse_program(name, file.first); auto prog = source_.parse_program(name, file.second);
auto funcs = std::vector<std::string>{}; auto funcs = std::vector<std::string>{};

View File

@ -126,7 +126,7 @@ protected:
std::unordered_map<std::string_view, u32> token_map_rev_; std::unordered_map<std::string_view, u32> token_map_rev_;
std::unordered_map<u64, std::string_view> path_map_; std::unordered_map<u64, std::string_view> path_map_;
std::unordered_map<u64, std::string_view> hash_map_; std::unordered_map<u64, std::string_view> hash_map_;
std::unordered_map<std::string, buffer> header_files_; std::unordered_map<std::string, std::vector<u8>> header_files_;
std::unordered_set<std::string_view> includes_; std::unordered_set<std::string_view> includes_;
std::unordered_map<std::string, std::vector<std::string>> include_cache_; std::unordered_map<std::string, std::vector<std::string>> include_cache_;
std::unordered_set<std::string> new_func_map_; std::unordered_set<std::string> new_func_map_;

View File

@ -457,12 +457,7 @@ auto fs_callback(std::string const& name) -> std::pair<buffer, std::vector<u8>>
} }
else else
{ {
auto res = files.insert({ name, std::move(data) }); return { {}, std::move(data) };
if(res.second)
{
return { {res.first->second.data(), res.first->second.size() }, {} };
}
} }
throw std::runtime_error("file read error"); throw std::runtime_error("file read error");