From d394042694b3461efd771f0586c5c95b0c56f79d Mon Sep 17 00:00:00 2001 From: Marvin Kopf Date: Tue, 6 Dec 2016 10:40:34 +0100 Subject: Fix comparator segfaults The handler would get called for any BlockEntity, but not every BlockEntity is a BlockEntityWithItems. Downcasting with static_cast is UB on fail. --- .../IncrementalRedstoneSimulator/RedstoneComparatorHandler.h | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/Simulator/IncrementalRedstoneSimulator/RedstoneComparatorHandler.h b/src/Simulator/IncrementalRedstoneSimulator/RedstoneComparatorHandler.h index debfd6330..34af3ccbc 100644 --- a/src/Simulator/IncrementalRedstoneSimulator/RedstoneComparatorHandler.h +++ b/src/Simulator/IncrementalRedstoneSimulator/RedstoneComparatorHandler.h @@ -54,7 +54,14 @@ public: virtual bool Item(cBlockEntity * a_BlockEntity) override { - auto & Contents = static_cast(a_BlockEntity)->GetContents(); + // Skip BlockEntities that don't have slots + auto BlockEntityWithItems = dynamic_cast(a_BlockEntity); + if (BlockEntityWithItems == nullptr) + { + return false; + } + + auto & Contents = BlockEntityWithItems->GetContents(); float Fullness = 0; // Is a floating-point type to allow later calculation to produce a non-truncated value for (int Slot = 0; Slot != Contents.GetNumSlots(); ++Slot) -- cgit v1.2.3