From 9a7bb1fac7fc3ef94ea5ad064e811f06ab05dfc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xenxo=20Espasand=C3=ADn?= Date: Sun, 12 Feb 2023 15:58:24 +0100 Subject: [PATCH] fix(gsc): use vector for rawfile (#64) --- src/gsc/context.cpp | 15 +++++++-------- src/gsc/context.hpp | 2 +- src/tool/main.cpp | 7 +------ 3 files changed, 9 insertions(+), 15 deletions(-) diff --git a/src/gsc/context.cpp b/src/gsc/context.cpp index 55cd931b..80581118 100644 --- a/src/gsc/context.cpp +++ b/src/gsc/context.cpp @@ -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 { - // todo: remove cache to prevent use after free if files are read from fs by the game auto const itr = header_files_.find(name); if (itr != header_files_.end()) { - return { &itr->first, reinterpret_cast(itr->second.data), itr->second.size }; + return { &itr->first, reinterpret_cast(itr->second.data()), itr->second.size() }; } 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) { - return { &res.first->first, reinterpret_cast(res.first->second.data), res.first->second.size }; + return { &res.first->first, reinterpret_cast(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); - 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"); - if (file.second.size() == 0) + if (file.first.data == nullptr && file.first.size == 0 && !file.second.empty()) { // process RawFile - auto prog = source_.parse_program(name, file.first); + auto prog = source_.parse_program(name, file.second); auto funcs = std::vector{}; diff --git a/src/gsc/context.hpp b/src/gsc/context.hpp index 38c86a3d..43dd3470 100644 --- a/src/gsc/context.hpp +++ b/src/gsc/context.hpp @@ -126,7 +126,7 @@ protected: std::unordered_map token_map_rev_; std::unordered_map path_map_; std::unordered_map hash_map_; - std::unordered_map header_files_; + std::unordered_map> header_files_; std::unordered_set includes_; std::unordered_map> include_cache_; std::unordered_set new_func_map_; diff --git a/src/tool/main.cpp b/src/tool/main.cpp index f8b9bf9c..5b6f0b9e 100644 --- a/src/tool/main.cpp +++ b/src/tool/main.cpp @@ -457,12 +457,7 @@ auto fs_callback(std::string const& name) -> std::pair> } else { - auto res = files.insert({ name, std::move(data) }); - - if(res.second) - { - return { {res.first->second.data(), res.first->second.size() }, {} }; - } + return { {}, std::move(data) }; } throw std::runtime_error("file read error");