From a0d81f8764127380e1c722f5150b1f1709b15f44 Mon Sep 17 00:00:00 2001 From: Denis Vaksman Date: Fri, 22 Sep 2023 12:14:19 +0300 Subject: [PATCH] [tlo-parsing] dynamically allocate buffer for tlo file (#905) --- common/tlo-parsing/tlo-parser.cpp | 18 +++++++++++------- common/tlo-parsing/tlo-parser.h | 8 +++----- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/common/tlo-parsing/tlo-parser.cpp b/common/tlo-parsing/tlo-parser.cpp index 791bbce995..8758b98fbb 100644 --- a/common/tlo-parsing/tlo-parser.cpp +++ b/common/tlo-parsing/tlo-parser.cpp @@ -24,31 +24,35 @@ tlo_parser::tlo_parser(const char *tlo_path) : error("%s", strerror(errno)); return; } - len = fread(data, 1, sizeof(data), f); + + std::fseek(f, 0, SEEK_END); + auto file_size = std::ftell(f); + std::rewind(f); + data.resize(file_size); + + len = fread(data.data(), 1, file_size, f); std::fclose(f); - if (!(len > 0 && len < MAX_SCHEMA_LEN)) { + if (!(len > 0 && len == file_size)) { error("Error while reading file %s", tlo_path); return; } tl_sch = std::make_unique(); } -tlo_parser::~tlo_parser() = default; - std::string tlo_parser::get_string() { check_pos(4); - size_t len = *reinterpret_cast(data + pos); + size_t len = *reinterpret_cast(data.data() + pos); if (len < 254) { pos += 1; } else { - len = *reinterpret_cast(data + pos); + len = *reinterpret_cast(data.data() + pos); pos += 4; } check_pos(len); size_t old_pos = pos; pos += len; pos += (-pos & 3); - return {data + old_pos, len}; + return {data.data() + old_pos, len}; } void tlo_parser::check_pos(size_t size) { diff --git a/common/tlo-parsing/tlo-parser.h b/common/tlo-parsing/tlo-parser.h index 09b0432cee..ae15f59bed 100644 --- a/common/tlo-parsing/tlo-parser.h +++ b/common/tlo-parsing/tlo-parser.h @@ -7,6 +7,7 @@ #include #include #include +#include namespace vk { namespace tlo_parsing { @@ -17,17 +18,14 @@ struct expr_base; struct tl_scheme; struct tlo_parser { - static constexpr unsigned int MAX_SCHEMA_LEN = 4 * 1024 * 1024; - tlo_parser() = default; explicit tlo_parser(const char *tlo_path); - ~tlo_parser(); template T get_value() { check_pos(sizeof(T)); T res{}; - memcpy(&res, data + pos, sizeof(T)); + memcpy(&res, data.data() + pos, sizeof(T)); pos += sizeof(T); return res; } @@ -49,7 +47,7 @@ struct tlo_parser { size_t len; size_t pos; std::unique_ptr tl_sch; - char data[MAX_SCHEMA_LEN + 1]; + std::vector data; }; } // namespace tlo_parsing