Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for issue #407: Prevent crashes due to unbounded growth. #721

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,20 @@ class CastingVM(var image: CastingImage, val env: CastingEnvironment) {
// ...and execute it.
// TODO there used to be error checking code here; I'm pretty sure any and all mishaps should already
// get caught and folded into CastResult by evaluate.
val image2 = next.evaluate(continuation.next, world, this)
val image2 = next.evaluate(continuation.next, world, this).let { result ->
// if stack is unable to be serialized, have the result be an error
if (result.newData != null && IotaType.isTooLargeToSerialize(result.newData.stack)) {
result.copy(
newData = null,
sideEffects = listOf(OperatorSideEffect.DoMishap(MishapStackSize(), Mishap.Context(null, null))),
resolutionType = ResolvedPatternType.ERRORED,
sound = HexEvalSounds.MISHAP,
)
} else {
result
}
}

// Then write all pertinent data back to the harness for the next iteration.
if (image2.newData != null) {
this.image = image2.newData
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package at.petrak.hexcasting.api.casting.mishaps

import at.petrak.hexcasting.api.casting.eval.CastingEnvironment
import at.petrak.hexcasting.api.casting.eval.ResolvedPatternType
import at.petrak.hexcasting.api.casting.iota.GarbageIota
import at.petrak.hexcasting.api.casting.iota.Iota
import at.petrak.hexcasting.api.pigment.FrozenPigment
import at.petrak.hexcasting.common.lib.HexDamageTypes
import net.minecraft.world.item.DyeColor

class MishapStackSize() : Mishap() {
override fun accentColor(ctx: CastingEnvironment, errorCtx: Context): FrozenPigment =
dyeColor(DyeColor.BLACK)

override fun resolutionType(ctx: CastingEnvironment) = ResolvedPatternType.ERRORED

override fun execute(env: CastingEnvironment, errorCtx: Context, stack: MutableList<Iota>) {
stack.clear()
stack.add(GarbageIota())
}

override fun errorMessage(ctx: CastingEnvironment, errorCtx: Context) =
error("stack_size")
}
Original file line number Diff line number Diff line change
Expand Up @@ -901,6 +901,7 @@
invalid_spell_datum_type: "Tried to use a value of invalid type as a SpellDatum: %s (class %s). This is a bug in the mod.",
unknown: "threw an exception (%s). This is a bug in the mod.",
shame: "Shame on you!",
stack_size: "Exceeded the size limit of the stack",

invalid_value: {
"": "expected %s at index %s of the stack, but got %s",
Expand Down
Loading