Add bounds checks to the parsing utilities. (#5421)

* Add bounds checks to the parsing utilities.

* Fix merge conflicts in ACLoader.

* Fix loaders

* Fix unittest of AC-Loader.

* Remove dead code.

* Md5Parser fixes

* Fix md5-parsing

* Fix Merge conflict

* Fix merge conflicts.

* Md5: Fix warning: missing return statement.
This commit is contained in:
Kim Kulling
2024-01-30 14:32:41 +01:00
committed by GitHub
parent d5f35582d4
commit c08e3b4abb
39 changed files with 853 additions and 782 deletions

View File

@@ -123,12 +123,12 @@ bool Q3Shader::LoadShader(ShaderData &fill, const std::string &pFile, IOSystem *
// remove comments from it (C++ style)
CommentRemover::RemoveLineComments("//", &_buff[0]);
const char *buff = &_buff[0];
const char *end = buff + _buff.size();
Q3Shader::ShaderDataBlock *curData = nullptr;
Q3Shader::ShaderMapBlock *curMap = nullptr;
// read line per line
for (; SkipSpacesAndLineEnd(&buff); SkipLine(&buff)) {
for (; SkipSpacesAndLineEnd(&buff, end); SkipLine(&buff, end)) {
if (*buff == '{') {
++buff;
@@ -140,21 +140,21 @@ bool Q3Shader::LoadShader(ShaderData &fill, const std::string &pFile, IOSystem *
}
// read this data section
for (; SkipSpacesAndLineEnd(&buff); SkipLine(&buff)) {
for (; SkipSpacesAndLineEnd(&buff, end); SkipLine(&buff, end)) {
if (*buff == '{') {
++buff;
// add new map section
curData->maps.emplace_back();
curMap = &curData->maps.back();
for (; SkipSpacesAndLineEnd(&buff); SkipLine(&buff)) {
for (; SkipSpacesAndLineEnd(&buff, end); SkipLine(&buff, end)) {
// 'map' - Specifies texture file name
if (TokenMatchI(buff, "map", 3) || TokenMatchI(buff, "clampmap", 8)) {
curMap->name = GetNextToken(buff);
curMap->name = GetNextToken(buff, end);
}
// 'blendfunc' - Alpha blending mode
else if (TokenMatchI(buff, "blendfunc", 9)) {
const std::string blend_src = GetNextToken(buff);
const std::string blend_src = GetNextToken(buff, end);
if (blend_src == "add") {
curMap->blend_src = Q3Shader::BLEND_GL_ONE;
curMap->blend_dest = Q3Shader::BLEND_GL_ONE;
@@ -166,12 +166,12 @@ bool Q3Shader::LoadShader(ShaderData &fill, const std::string &pFile, IOSystem *
curMap->blend_dest = Q3Shader::BLEND_GL_ONE_MINUS_SRC_ALPHA;
} else {
curMap->blend_src = StringToBlendFunc(blend_src);
curMap->blend_dest = StringToBlendFunc(GetNextToken(buff));
curMap->blend_dest = StringToBlendFunc(GetNextToken(buff, end));
}
}
// 'alphafunc' - Alpha testing mode
else if (TokenMatchI(buff, "alphafunc", 9)) {
const std::string at = GetNextToken(buff);
const std::string at = GetNextToken(buff, end);
if (at == "GT0") {
curMap->alpha_test = Q3Shader::AT_GT0;
} else if (at == "LT128") {
@@ -186,7 +186,6 @@ bool Q3Shader::LoadShader(ShaderData &fill, const std::string &pFile, IOSystem *
break;
}
}
} else if (*buff == '}') {
++buff;
curData = nullptr;
@@ -195,7 +194,7 @@ bool Q3Shader::LoadShader(ShaderData &fill, const std::string &pFile, IOSystem *
// 'cull' specifies culling behaviour for the model
else if (TokenMatchI(buff, "cull", 4)) {
SkipSpaces(&buff);
SkipSpaces(&buff, end);
if (!ASSIMP_strincmp(buff, "back", 4)) { // render face's backside, does not function in Q3 engine (bug)
curData->cull = Q3Shader::CULL_CCW;
} else if (!ASSIMP_strincmp(buff, "front", 5)) { // is not valid keyword in Q3, but occurs in shaders
@@ -213,9 +212,10 @@ bool Q3Shader::LoadShader(ShaderData &fill, const std::string &pFile, IOSystem *
curData = &fill.blocks.back();
// get the name of this section
curData->name = GetNextToken(buff);
curData->name = GetNextToken(buff, end);
}
}
return true;
}
@@ -232,6 +232,7 @@ bool Q3Shader::LoadSkin(SkinData &fill, const std::string &pFile, IOSystem *io)
const size_t s = file->FileSize();
std::vector<char> _buff(s + 1);
const char *buff = &_buff[0];
const char *end = buff + _buff.size();
file->Read(&_buff[0], s, 1);
_buff[s] = 0;
@@ -240,10 +241,10 @@ bool Q3Shader::LoadSkin(SkinData &fill, const std::string &pFile, IOSystem *io)
// read token by token and fill output table
for (; *buff;) {
SkipSpacesAndLineEnd(&buff);
SkipSpacesAndLineEnd(&buff, end);
// get first identifier
std::string ss = GetNextToken(buff);
std::string ss = GetNextToken(buff, end);
// ignore tokens starting with tag_
if (!::strncmp(&ss[0], "tag_", std::min((size_t)4, ss.length())))
@@ -253,8 +254,9 @@ bool Q3Shader::LoadSkin(SkinData &fill, const std::string &pFile, IOSystem *io)
SkinData::TextureEntry &entry = fill.textures.back();
entry.first = ss;
entry.second = GetNextToken(buff);
entry.second = GetNextToken(buff, end);
}
return true;
}
@@ -293,7 +295,7 @@ void Q3Shader::ConvertShaderToMaterial(aiMaterial *out, const ShaderDataBlock &s
// - in any case: set it as diffuse texture
//
// If the texture is using 'filter' blending
// - take as lightmap
// - take as light-map
//
// Textures with alpha funcs
// - aiTextureFlags_UseAlpha is set (otherwise aiTextureFlags_NoAlpha is explicitly set)