[poincare] Fix checking for special tree identifier value

Unsigned types are never negative, so the existant check does not make
sense. Check correctly using the newly introduced
TreeNode::IsValidIdentifier method.
This commit is contained in:
Neven Sajko
2020-02-21 20:34:27 +00:00
committed by LeaNumworks
parent f12c53b3f2
commit 4da9f34993
4 changed files with 6 additions and 4 deletions

View File

@@ -128,7 +128,7 @@ protected:
void setIdentifierAndRetain(uint16_t newId);
void setTo(const TreeHandle & tr);
static bool hasNode(uint16_t identifier) { return identifier < TreeNode::NoNodeIdentifier; }
static bool hasNode(uint16_t identifier) { return TreeNode::IsValidIdentifier(identifier); }
/* Hierarchy operations */
// Add

View File

@@ -172,6 +172,8 @@ public:
void log(std::ostream & stream, bool recursive = true);
#endif
static bool IsValidIdentifier(uint16_t id) { return id < NoNodeIdentifier; }
protected:
TreeNode() :
m_identifier(NoNodeIdentifier),

View File

@@ -27,7 +27,7 @@ public:
// Node
TreeNode * node(uint16_t identifier) const {
assert(identifier >= 0 && identifier < MaxNumberOfNodes);
assert(TreeNode::IsValidIdentifier(identifier) && identifier < MaxNumberOfNodes);
if (m_nodeForIdentifierOffset[identifier] != UINT16_MAX) {
return const_cast<TreeNode *>(reinterpret_cast<const TreeNode *>(m_alignedBuffer + m_nodeForIdentifierOffset[identifier]));
}
@@ -125,7 +125,7 @@ private:
}
}
void push(uint16_t i) {
assert(m_currentIndex >= 0 && m_currentIndex < MaxNumberOfNodes);
assert(TreeNode::IsValidIdentifier(m_currentIndex) && m_currentIndex < MaxNumberOfNodes);
m_availableIdentifiers[m_currentIndex++] = i;
}
uint16_t pop() {

View File

@@ -14,7 +14,7 @@ namespace Poincare {
TreePool * TreePool::SharedStaticPool = nullptr;
void TreePool::freeIdentifier(uint16_t identifier) {
if (identifier >= 0 && identifier < MaxNumberOfNodes) {
if (TreeNode::IsValidIdentifier(identifier) && identifier < MaxNumberOfNodes) {
m_nodeForIdentifierOffset[identifier] = UINT16_MAX;
m_identifiers.push(identifier);
}