Skip to content

Commit

Permalink
Fix possible stack overflow with xml::decode
Browse files Browse the repository at this point in the history
  • Loading branch information
Sainan committed Jan 17, 2025
1 parent 59f84dd commit b644a4a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
10 changes: 6 additions & 4 deletions soup/xml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ NAMESPACE_SOUP
auto i = begin;
do
{
if (auto node = parseImpl(i, end, mode))
if (auto node = parseImpl(i, end, mode, 1000))
{
res.emplace_back(std::move(node));
}
Expand All @@ -77,8 +77,10 @@ NAMESPACE_SOUP
return res;
}

UniquePtr<XmlNode> xml::parseImpl(const char*& i, const char* end, const XmlMode& mode)
UniquePtr<XmlNode> xml::parseImpl(const char*& i, const char* end, const XmlMode& mode, int max_depth)
{
SOUP_ASSERT(max_depth != 0, "Depth limit exceeded");

while (i != end && string::isSpace(*i))
{
++i;
Expand Down Expand Up @@ -393,11 +395,11 @@ NAMESPACE_SOUP
text.clear();
}
#if DEBUG_PARSE
auto child = parseImpl(i, end, mode);
auto child = parseImpl(i, end, mode, max_depth - 1);
std::cout << "Recursed for " << child->encode() << std::endl;
tag->children.emplace_back(std::move(child));
#else
tag->children.emplace_back(parseImpl(i, end, mode));
tag->children.emplace_back(parseImpl(i, end, mode, max_depth - 1));
#endif
if (i == end)
{
Expand Down
2 changes: 1 addition & 1 deletion soup/xml.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ NAMESPACE_SOUP
[[nodiscard]] static std::vector<UniquePtr<XmlNode>> parse(const std::string& xml, const XmlMode& mode = MODE_XML);
[[nodiscard]] static std::vector<UniquePtr<XmlNode>> parse(const char* begin, const char* end, const XmlMode& mode = MODE_XML);
private:
[[nodiscard]] static UniquePtr<XmlNode> parseImpl(const char*& i, const char* end, const XmlMode& mode);
[[nodiscard]] static UniquePtr<XmlNode> parseImpl(const char*& i, const char* end, const XmlMode& mode, int max_depth);
};

struct XmlNode
Expand Down

0 comments on commit b644a4a

Please sign in to comment.