Skip to content

Commit

Permalink
Add redundant stack overflow safeguard for Exposers
Browse files Browse the repository at this point in the history
  • Loading branch information
NotMyWing committed Jun 18, 2024
1 parent c991997 commit 246de04
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,17 @@ protected T pullStackInternal(T stack, boolean simulate) {
*/
@Nullable
protected T pullStack(T stack, boolean simulate) {
if (this.lockRecursion()) {
return null;
}

this.updateMonitor();

return this.pullStackInternal(stack, simulate);
try {
return this.pullStackInternal(stack, simulate);
} finally {
this.unlockRecursion();
}
}

/**
Expand All @@ -95,20 +103,28 @@ protected T pullStack(T stack, boolean simulate) {
*/
@Nullable
protected T pullStackFromSlot(int slot, long maxAmt, boolean simulate) {
this.updateMonitor();
if (this.lockRecursion()) {
return null;
}

if (slot < this.cache.size()) {
var stack = this.cache.getByIndex(slot);
if (stack != null) {
if (slot > 0 && !simulate) {
this.cache.makeFirst(stack);
}
try {
this.updateMonitor();

if (slot < this.cache.size()) {
var stack = this.cache.getByIndex(slot);
if (stack != null) {
if (slot > 0 && !simulate) {
this.cache.makeFirst(stack);
}

return this.pullStackInternal(stack.copy().setStackSize(maxAmt), simulate);
return this.pullStackInternal(stack.copy().setStackSize(maxAmt), simulate);
}
}
}

return null;
return null;
} finally {
this.unlockRecursion();
}
}

/**
Expand Down Expand Up @@ -165,12 +181,20 @@ protected void onMonitorChange(IMEMonitor<T> oldValue, IMEMonitor<T> newValue) {
*/
@Nullable
protected T getInSlot(int slot) {
this.updateMonitor();

if (!this.cache.isEmpty() && slot < this.cache.size()) {
return this.cache.getByIndex(slot);
if (this.lockRecursion()) {
return null;
}

return null;
try {
this.updateMonitor();

if (!this.cache.isEmpty() && slot < this.cache.size()) {
return this.cache.getByIndex(slot);
}

return null;
} finally {
this.unlockRecursion();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public abstract class ExposerHandler<T extends IAEStack<T>> implements IExposerH
protected final IActionSource mySrc;
protected final AENetworkProxy proxy;
protected IMEMonitor<T> monitor;
protected boolean isRecursionLocked = false;

public ExposerHandler(IExposerHost host) {
this.host = host;
Expand Down Expand Up @@ -136,4 +137,17 @@ public IActionSource getActionSource() {
public AENetworkProxy getProxy() {
return this.proxy;
}

protected boolean lockRecursion() {
if (this.isRecursionLocked) {
return true;
} else {
this.isRecursionLocked = true;
return false;
}
}

protected void unlockRecursion() {
this.isRecursionLocked = false;
}
}

0 comments on commit 246de04

Please sign in to comment.