Fix stack overflow

pull/5764/head
dataisland 2024-09-07 23:41:38 -05:00
parent 4024726eca
commit cfce93f9cc
2 changed files with 11 additions and 3 deletions

View File

@ -78,7 +78,15 @@ static constexpr aiImporterDesc desc = {
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
// Recursive parsing of LWS files // Recursive parsing of LWS files
void LWS::Element::Parse(const char *&buffer, const char *end) { namespace {
constexpr int MAX_DEPTH = 1000; // Define the maximum depth allowed
}
void LWS::Element::Parse(const char *&buffer, const char *end, int depth) {
if (depth > MAX_DEPTH) {
throw std::runtime_error("Maximum recursion depth exceeded in LWS::Element::Parse");
}
for (; SkipSpacesAndLineEnd(&buffer, end); SkipLine(&buffer, end)) { for (; SkipSpacesAndLineEnd(&buffer, end); SkipLine(&buffer, end)) {
// begin of a new element with children // begin of a new element with children
@ -121,7 +129,7 @@ void LWS::Element::Parse(const char *&buffer, const char *end) {
// parse more elements recursively // parse more elements recursively
if (sub) { if (sub) {
children.back().Parse(buffer, end); children.back().Parse(buffer, end, depth + 1);
} }
} }
} }

View File

@ -76,7 +76,7 @@ public:
std::list<Element> children; std::list<Element> children;
//! Recursive parsing function //! Recursive parsing function
void Parse(const char *&buffer, const char *end); void Parse(const char *&buffer, const char *end, int depth = 0);
}; };
#define AI_LWS_MASK (0xffffffff >> 4u) #define AI_LWS_MASK (0xffffffff >> 4u)