Tidy up SHAMap node handling of invalid wire formats

This commit is contained in:
JoelKatz
2015-06-17 13:31:02 -07:00
committed by Vinnie Falco
parent 2d6af1da1d
commit 749f31f69d
4 changed files with 23 additions and 30 deletions

View File

@@ -239,6 +239,9 @@ public:
Blob (node.nodedata().begin(), node.nodedata().end()),
0, snfWIRE, uZero, false);
if (!newNode)
return;
s.erase();
newNode->addRaw(s, snfPREFIX);

View File

@@ -181,7 +181,8 @@ SHAMap::fetchNodeFromDB (uint256 const& hash) const
{
node = SHAMapAbstractNode::make(obj->getData(),
0, snfPREFIX, hash, true);
canonicalize (hash, node);
if (node)
canonicalize (hash, node);
}
catch (...)
{
@@ -210,9 +211,12 @@ SHAMap::checkFilter(uint256 const& hash, SHAMapNodeID const& id,
if (filter->haveNode (id, hash, nodeData))
{
node = SHAMapAbstractNode::make(nodeData, 0, snfPREFIX, hash, true);
filter->gotNode (true, id, hash, nodeData, node->getType ());
if (backed_)
canonicalize (hash, node);
if (node)
{
filter->gotNode (true, id, hash, nodeData, node->getType ());
if (backed_)
canonicalize (hash, node);
}
}
return node;
}
@@ -383,7 +387,7 @@ SHAMap::descendAsync (SHAMapInnerNode* parent, int branch,
ptr = SHAMapAbstractNode::make(obj->getData(), 0, snfPREFIX, hash, true);
if (backed_)
if (ptr && backed_)
canonicalize (hash, ptr);
}
}

View File

@@ -419,7 +419,7 @@ SHAMapAddNode SHAMap::addRootNode (Blob const& rootNode,
assert (seq_ >= 1);
auto node = SHAMapAbstractNode::make(rootNode, 0, format, uZero, false);
if (!node)
if (!node || !node->isValid ())
return SHAMapAddNode::invalid ();
#ifdef BEAST_DEBUG
@@ -459,7 +459,7 @@ SHAMapAddNode SHAMap::addRootNode (uint256 const& hash, Blob const& rootNode, SH
assert (seq_ >= 1);
auto node = SHAMapAbstractNode::make(rootNode, 0, format, uZero, false);
if (!node || node->getNodeHash () != hash)
if (!node || !node->isValid() || node->getNodeHash () != hash)
return SHAMapAddNode::invalid ();
if (backed_)
@@ -537,6 +537,13 @@ SHAMap::addKnownNode (const SHAMapNodeID& node, Blob const& rawNode,
auto newNode = SHAMapAbstractNode::make(rawNode, 0, snfWIRE, uZero, false);
if (!newNode || !newNode->isValid() || childHash != newNode->getNodeHash ())
{
if (journal_.warning) journal_.warning <<
"Corrupt node received";
return SHAMapAddNode::invalid ();
}
if (!newNode->isInBounds (iNodeID))
{
// Map is provably invalid
@@ -544,13 +551,6 @@ SHAMap::addKnownNode (const SHAMapNodeID& node, Blob const& rawNode,
return SHAMapAddNode::useful ();
}
if (childHash != newNode->getNodeHash ())
{
if (journal_.warning) journal_.warning <<
"Corrupt node received";
return SHAMapAddNode::invalid ();
}
if (backed_)
canonicalize (childHash, newNode);

View File

@@ -79,28 +79,14 @@ SHAMapAbstractNode::make(Blob const& rawNode, std::uint32_t seq, SHANodeFormat f
if (format == snfWIRE)
{
if (rawNode.empty ())
{
#ifdef BEAST_DEBUG
deprecatedLogs().journal("SHAMapTreeNode").fatal <<
"Wire format node is empty";
assert (false);
#endif
throw std::runtime_error ("invalid node AW type");
}
return {};
Serializer s (rawNode.data(), rawNode.size() - 1);
int type = rawNode.back ();
int len = s.getLength ();
if ((type < 0) || (type > 4))
{
#ifdef BEAST_DEBUG
deprecatedLogs().journal("SHAMapTreeNode").fatal <<
"Invalid wire format node" << strHex (rawNode);
assert (false);
#endif
throw std::runtime_error ("invalid node AW type");
}
return {};
if (type == 0)
{