Prevent infinite loops in device linking (#16856)

This commit is contained in:
Julian Giebel
2023-05-28 18:14:06 +02:00
committed by GitHub
parent 9c1fe530c1
commit 49cb9d0e1e
11 changed files with 185 additions and 14 deletions

View File

@@ -19,4 +19,20 @@ public sealed class DeviceLinkSinkComponent : Component
/// </summary>
[DataField("links")]
public HashSet<EntityUid> LinkedSources = new();
/// <summary>
/// Counts the amount of times a sink has been invoked for severing the link if this counter gets to high
/// The counter is counted down by one every tick if it's higher than 0
/// This is for preventing infinite loops
/// </summary>
[DataField("invokeCounter")]
public int InvokeCounter;
/// <summary>
/// How high the invoke counter is allowed to get before the links to the sink are removed and the DeviceLinkOverloadedEvent gets raised
/// If the invoke limit is smaller than 1 the sink can't overload
/// </summary>
[ViewVariables(VVAccess.ReadWrite)]
[DataField("invokeLimit")]
public int InvokeLimit = 10;
}

View File

@@ -315,6 +315,20 @@ public abstract class SharedDeviceLinkSystem : EntitySystem
sinkComponent.LinkedSources.Add(sourceUid);
}
/// <summary>
/// Removes every link from the given sink
/// </summary>
public void RemoveAllFromSink(EntityUid sinkUid, DeviceLinkSinkComponent? sinkComponent = null)
{
if (!Resolve(sinkUid, ref sinkComponent))
return;
foreach (var sourceUid in sinkComponent.LinkedSources)
{
RemoveSinkFromSource(sourceUid, sinkUid, null, sinkComponent);
}
}
/// <summary>
/// Removes all links between a source and a sink
/// </summary>
@@ -332,7 +346,7 @@ public abstract class SharedDeviceLinkSystem : EntitySystem
if (sourceComponent == null && sinkComponent == null)
{
// Both were delted?
// Both were deleted?
return;
}