parser: fix reading past end of buffer

This commit is contained in:
Eric Wasylishen 2022-01-15 18:31:35 -07:00
parent 8d31f12c8b
commit f02bbca0c7
4 changed files with 11 additions and 6 deletions

View File

@ -117,7 +117,7 @@ keyvalues_t::iterator entdict_t::end()
std::vector<entdict_t> EntData_Parse(const std::string &entdata)
{
std::vector<entdict_t> result;
parser_t parser(entdata.data());
parser_t parser(entdata.data(), entdata.data() + entdata.size());
/* go through all the entities */
while (1) {

View File

@ -37,8 +37,8 @@ bool parser_t::parse_token(parseflags flags)
skipspace:
/* skip space */
while (*pos <= 32) {
if (!*pos) {
while (at_end() || *pos <= 32) {
if (at_end() || !*pos) {
if (flags & PARSE_OPTIONAL)
return false;
if (flags & PARSE_SAMELINE)

View File

@ -43,12 +43,15 @@ inline auto untie(const std::tuple<T...> &tuple)
struct parser_t
{
const char *pos;
const char *end;
uint32_t linenum = 1;
std::string token;
parser_t(const char *data) : pos(data) { }
parser_t(const char *data, const char *in_end) : pos(data), end(in_end) { }
bool parse_token(parseflags flags = PARSE_NORMAL);
auto state() { return std::tie(pos, linenum); }
bool at_end() const { return pos >= end; };
};

View File

@ -1847,7 +1847,8 @@ mapentity_t LoadExternalMap(const char *filename)
FError("Couldn't load external map file \"{}.\"\n", filename);
}
parser_t parser(reinterpret_cast<const char *>(file->data()));
const char *begin = reinterpret_cast<const char *>(file->data());
parser_t parser(begin, begin + file->size());
// parse the worldspawn
if (!ParseEntity(parser, &dest)) {
@ -1895,7 +1896,8 @@ void LoadMapFile(void)
return;
}
parser_t parser(reinterpret_cast<const char *>(file->data()));
const char *begin = reinterpret_cast<const char *>(file->data());
parser_t parser(begin, begin + file->size());
for (int i = 0;; i++) {
mapentity_t &entity = map.entities.emplace_back();