Skip to content

Commit

Permalink
Fix for issue #407: Prevent crashes due to unbounded growth. (#721)
Browse files Browse the repository at this point in the history
  • Loading branch information
object-Object authored Aug 20, 2024
2 parents 21a344b + b408b67 commit a763cdf
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
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

0 comments on commit a763cdf

Please sign in to comment.