Fix stack overflow (#807)

Fix stack overflow in HandleNode by explicitly limiting the depth of recursion.
This commit is contained in:
Alan Griffiths
2020-04-09 19:02:10 +01:00
committed by GitHub
parent 70205f5d88
commit 4edff1fa5d
5 changed files with 140 additions and 0 deletions

10
src/depthguard.cpp Normal file
View File

@@ -0,0 +1,10 @@
#include "yaml-cpp/depthguard.h"
namespace YAML {
DeepRecursion::DeepRecursion(int depth, const Mark& mark_, const std::string& msg_)
: ParserException(mark_, msg_),
m_depth(depth) {
}
} // namespace YAML

View File

@@ -7,6 +7,7 @@
#include "singledocparser.h"
#include "tag.h"
#include "token.h"
#include "yaml-cpp/depthguard.h"
#include "yaml-cpp/emitterstyle.h"
#include "yaml-cpp/eventhandler.h"
#include "yaml-cpp/exceptions.h" // IWYU pragma: keep
@@ -47,6 +48,8 @@ void SingleDocParser::HandleDocument(EventHandler& eventHandler) {
}
void SingleDocParser::HandleNode(EventHandler& eventHandler) {
DepthGuard<2000> depthguard(depth, m_scanner.mark(), ErrorMsg::BAD_FILE);
// an empty node *is* a possibility
if (m_scanner.empty()) {
eventHandler.OnNull(m_scanner.mark(), NullAnchor);

View File

@@ -15,6 +15,7 @@
namespace YAML {
class CollectionStack;
template <int> class DepthGuard; // depthguard.h
class EventHandler;
class Node;
class Scanner;
@@ -55,6 +56,7 @@ class SingleDocParser {
anchor_t LookupAnchor(const Mark& mark, const std::string& name) const;
private:
int depth = 0;
Scanner& m_scanner;
const Directives& m_directives;
std::unique_ptr<CollectionStack> m_pCollectionStack;