Skip to content

Commit

Permalink
UnitSAO: Prevent circular attachments
Browse files Browse the repository at this point in the history
  • Loading branch information
SmallJoker committed May 29, 2021
1 parent ff48619 commit 5bf7246
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
2 changes: 2 additions & 0 deletions doc/lua_api.txt
Expand Up @@ -6314,6 +6314,8 @@ object you are working with still exists.
Default `{x=0, y=0, z=0}`
* `forced_visible`: Boolean to control whether the attached entity
should appear in first person. Default `false`.
* This command may fail silently (do nothing) when it would result
in circular attachments.
* `get_attach()`: returns parent, bone, position, rotation, forced_visible,
or nil if it isn't attached.
* `get_children()`: returns a list of ObjectRefs that are attached to the
Expand Down
13 changes: 13 additions & 0 deletions src/server/unit_sao.cpp
Expand Up @@ -124,6 +124,19 @@ void UnitSAO::sendOutdatedData()
void UnitSAO::setAttachment(int parent_id, const std::string &bone, v3f position,
v3f rotation, bool force_visible)
{
auto *obj = parent_id ? m_env->getActiveObject(parent_id) : nullptr;
if (obj) {
// Do checks to avoid circular references
// The chain of wanted parent must not refer or contain "this"
for (obj = obj->getParent(); obj; obj = obj->getParent()) {
if (obj == this) {
warningstream << "Mod bug: Attempted to attach object " << m_id << " to parent "
<< parent_id << " but former is an (in)direct parent of latter." << std::endl;
return;
}
}
}

// Attachments need to be handled on both the server and client.
// If we just attach on the server, we can only copy the position of the parent.
// Attachments are still sent to clients at an interval so players might see them
Expand Down

0 comments on commit 5bf7246

Please sign in to comment.