1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2024-12-13 11:40:29 +00:00

Fix misplaced calls to IArguments.escapes

- Fix mainThread=true methods calling IArguments.escapes too late. This
   should be done before scheduling on the main thread, not on the main
   thread itself!

 - Fix VarargsArguments.escapes not checking that the argument haven't
   been closed. This is slightly prone to race conditions, but I don't
   think it's worth the overhead of tracking the owning thread.

   Maybe when panama and its resource scopes are released.

Thanks Sara for pointing this out!

Slightly irked that none of our tests caught this. Alas.

Also fix a typo in AddressPredicate. Yes, no commit discipline.
This commit is contained in:
Jonathan Coates 2023-06-27 18:25:34 +01:00
parent 910a63214e
commit 7eb3b691da
No known key found for this signature in database
GPG Key ID: B9E431FF07C98D06
6 changed files with 21 additions and 4 deletions

View File

@ -479,9 +479,11 @@ public interface IArguments {
* yourself.
*
* @return An {@link IArguments} instance which can escape the current scope. May be {@code this}.
* @throws LuaException For the same reasons as {@link #get(int)}.
* @throws LuaException For the same reasons as {@link #get(int)}.
* @throws IllegalStateException If marking these arguments as escaping outside the scope of the original function.
*/
default IArguments escapes() throws LuaException {
// TODO(1.21.0): Make this return void, require that it mutates this.
return this;
}
}

View File

@ -70,7 +70,7 @@ interface AddressPredicate {
} catch (IllegalArgumentException e) {
LOG.error(
"Malformed http whitelist/blacklist entry '{}': Cannot extract IP address from '{}'.",
addressStr + '/' + prefixSizeStr, prefixSizeStr
addressStr + '/' + prefixSizeStr, addressStr
);
return null;
}

View File

@ -21,7 +21,10 @@ import java.util.Objects;
*/
public final class LuaMethodSupplier {
private static final Generator<LuaMethod> GENERATOR = new Generator<>(LuaMethod.class, List.of(ILuaContext.class),
m -> (target, context, args) -> context.executeMainThreadTask(() -> ResultHelpers.checkNormalResult(m.apply(target, context, args.escapes())))
m -> (target, context, args) -> {
var escArgs = args.escapes();
return context.executeMainThreadTask(() -> ResultHelpers.checkNormalResult(m.apply(target, context, escArgs)));
}
);
private static final IntCache<LuaMethod> DYNAMIC = new IntCache<>(
method -> (instance, context, args) -> ((IDynamicLuaObject) instance).callMethod(context, method, args)

View File

@ -22,7 +22,10 @@ import java.util.Objects;
*/
public class PeripheralMethodSupplier {
private static final Generator<PeripheralMethod> GENERATOR = new Generator<>(PeripheralMethod.class, List.of(ILuaContext.class, IComputerAccess.class),
m -> (target, context, computer, args) -> context.executeMainThreadTask(() -> ResultHelpers.checkNormalResult(m.apply(target, context, computer, args.escapes())))
m -> (target, context, computer, args) -> {
var escArgs = args.escapes();
return context.executeMainThreadTask(() -> ResultHelpers.checkNormalResult(m.apply(target, context, computer, escArgs)));
}
);
private static final IntCache<PeripheralMethod> DYNAMIC = new IntCache<>(
method -> (instance, context, computer, args) -> ((IDynamicPeripheral) instance).callMethod(computer, context, method, args)

View File

@ -191,6 +191,7 @@ final class VarargArguments implements IArguments {
@Override
public IArguments escapes() {
if (escapes) return this;
if (isClosed()) throw new IllegalStateException("Cannot call escapes after IArguments has been closed.");
var cache = this.cache;
var typeNames = this.typeNames;

View File

@ -60,4 +60,12 @@ class VarargArgumentsTest {
assertThrows(IllegalStateException.class, () -> args.get(0));
assertThrows(IllegalStateException.class, () -> args.getType(0));
}
@Test
public void testEscapeAfterClose() {
var args = VarargArguments.of(tableWithCustomType());
args.close();
assertThrows(IllegalStateException.class, args::escapes);
}
}