Skip to content

fix: possible infinite loop and memory leak in metadata reader #1749

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 1 commit into from
Feb 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions test-app/runtime/src/main/cpp/MetadataReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ MetadataReader::MetadataReader(uint32_t nodesLength, uint8_t* nodeData, uint32_t
m_root = BuildTree();
}

// helper debug function when need to convert a metadata node to its full name
//std::string toFullName(MetadataTreeNode* p) {
// std::string final = p->name;
// while((p = p->parent) && !p->name.empty()) {
// final.insert(0,p->name + ".");
// };
// return final;
//}

MetadataTreeNode* MetadataReader::BuildTree() {
MetadataTreeNodeRawData* rootNodeData = reinterpret_cast<MetadataTreeNodeRawData*>(m_nodeData);

Expand All @@ -43,21 +52,22 @@ MetadataTreeNode* MetadataReader::BuildTree() {
node->children = new vector<MetadataTreeNode*>;
MetadataTreeNodeRawData* childNodeData = rootNodeData + curNodeData->firstChildId;
while (true) {

uint16_t childNodeDataId = childNodeData - rootNodeData;
// node (and its next siblings) already visited, so we don't need to visit it again
if (m_v[childNodeDataId] != emptyNode) {
break;
}

MetadataTreeNode* childNode = new MetadataTreeNode;
childNode->parent = node;
childNode->name = ReadName(childNodeData->offsetName);
childNode->offsetValue = childNodeData->offsetValue;

node->children->push_back(childNode);

uint16_t childNodeDataId = childNodeData - rootNodeData;

m_v[childNodeDataId] = childNode;

if (childNodeDataId == childNodeData->nextSiblingId) {
break;
}

childNodeData = rootNodeData + childNodeData->nextSiblingId;
}
}
Expand Down