Zero-length mChildren arrays should be nullptr (#5749)

- Children: Don't allocate a zero-length array
- aiNode::mChildren should be left nullptr instead.
pull/5752/head
RichardTea 2024-09-03 09:26:27 +01:00 committed by GitHub
parent c35200e38e
commit c1ffbfec06
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 27 additions and 12 deletions

View File

@ -643,11 +643,17 @@ void Discreet3DSImporter::AddNodeToGraph(aiScene *pcSOut, aiNode *pcOut,
} }
// Allocate storage for children // Allocate storage for children
pcOut->mNumChildren = (unsigned int)pcIn->mChildren.size(); const unsigned int size = static_cast<unsigned int>(pcIn->mChildren.size());
pcOut->mNumChildren = size;
if (size == 0) {
return;
}
pcOut->mChildren = new aiNode *[pcIn->mChildren.size()]; pcOut->mChildren = new aiNode *[pcIn->mChildren.size()];
// Recursively process all children // Recursively process all children
const unsigned int size = static_cast<unsigned int>(pcIn->mChildren.size());
for (unsigned int i = 0; i < size; ++i) { for (unsigned int i = 0; i < size; ++i) {
pcOut->mChildren[i] = new aiNode(); pcOut->mChildren[i] = new aiNode();
pcOut->mChildren[i]->mParent = pcOut; pcOut->mChildren[i]->mParent = pcOut;

View File

@ -372,10 +372,12 @@ aiNode *COBImporter::BuildNodes(const Node &root, const Scene &scin, aiScene *fi
} }
// add children recursively // add children recursively
if (!root.temp_children.empty()) {
nd->mChildren = new aiNode *[root.temp_children.size()](); nd->mChildren = new aiNode *[root.temp_children.size()]();
for (const Node *n : root.temp_children) { for (const Node *n : root.temp_children) {
(nd->mChildren[nd->mNumChildren++] = BuildNodes(*n, scin, fill))->mParent = nd; (nd->mChildren[nd->mNumChildren++] = BuildNodes(*n, scin, fill))->mParent = nd;
} }
}
return nd; return nd;
} }

View File

@ -357,14 +357,14 @@ void FBXConverter::ConvertNodes(uint64_t id, aiNode *parent, aiNode *root_node)
if (nodes.empty()) { if (nodes.empty()) {
parent->mNumChildren = 0; parent->mNumChildren = 0;
parent->mChildren = nullptr; parent->mChildren = nullptr;
} } else {
parent->mChildren = new aiNode *[nodes.size()](); parent->mChildren = new aiNode *[nodes.size()]();
parent->mNumChildren = static_cast<unsigned int>(nodes.size()); parent->mNumChildren = static_cast<unsigned int>(nodes.size());
for (unsigned int i = 0; i < nodes.size(); ++i) { for (unsigned int i = 0; i < nodes.size(); ++i) {
parent->mChildren[i] = nodes[i].mOwnership.release(); parent->mChildren[i] = nodes[i].mOwnership.release();
} }
} }
}
void FBXConverter::ConvertLights(const Model &model, const std::string &orig_name) { void FBXConverter::ConvertLights(const Model &model, const std::string &orig_name) {
const std::vector<const NodeAttribute *> &node_attrs = model.GetAttributes(); const std::vector<const NodeAttribute *> &node_attrs = model.GetAttributes();

View File

@ -400,6 +400,10 @@ void SMDImporter::AddBoneChildren(aiNode* pcNode, uint32_t iParent) {
} }
} }
// nothing to do
if (pcNode->mNumChildren == 0)
return;
// now allocate the output array // now allocate the output array
pcNode->mChildren = new aiNode *[pcNode->mNumChildren]; pcNode->mChildren = new aiNode *[pcNode->mNumChildren];

View File

@ -891,6 +891,9 @@ void ValidateDSProcess::Validate(const aiNode *pNode) {
ReportError("aiNode \"%s\" child %i \"%s\" parent is someone else: \"%s\"", pNode->mName.C_Str(), i, pChild->mName.C_Str(), parentName); ReportError("aiNode \"%s\" child %i \"%s\" parent is someone else: \"%s\"", pNode->mName.C_Str(), i, pChild->mName.C_Str(), parentName);
} }
} }
} else if (pNode->mChildren) {
ReportError("aiNode::mChildren is not nullptr for empty node %s (aiNode::mNumChildren is %i)",
nodeName, pNode->mNumChildren);
} }
} }