Skip to content

Commit

Permalink
[tlo-parsing] dynamically allocate buffer for tlo file (#905)
Browse files Browse the repository at this point in the history
  • Loading branch information
DrDet authored Sep 22, 2023
1 parent b29ef7b commit a0d81f8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
18 changes: 11 additions & 7 deletions common/tlo-parsing/tlo-parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<tl_scheme>();
}

tlo_parser::~tlo_parser() = default;

std::string tlo_parser::get_string() {
check_pos(4);
size_t len = *reinterpret_cast<unsigned char *>(data + pos);
size_t len = *reinterpret_cast<unsigned char *>(data.data() + pos);
if (len < 254) {
pos += 1;
} else {
len = *reinterpret_cast<unsigned int *>(data + pos);
len = *reinterpret_cast<unsigned int *>(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) {
Expand Down
8 changes: 3 additions & 5 deletions common/tlo-parsing/tlo-parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <cstring>
#include <memory>
#include <string>
#include <vector>

namespace vk {
namespace tlo_parsing {
Expand All @@ -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<typename T>
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;
}
Expand All @@ -49,7 +47,7 @@ struct tlo_parser {
size_t len;
size_t pos;
std::unique_ptr<tl_scheme> tl_sch;
char data[MAX_SCHEMA_LEN + 1];
std::vector<char> data;
};

} // namespace tlo_parsing
Expand Down

0 comments on commit a0d81f8

Please sign in to comment.