From 582169e0a5bdcb5bceb60563ddca435d5a51095b Mon Sep 17 00:00:00 2001 From: lethosor Date: Sat, 5 Oct 2019 21:05:26 -0400 Subject: [PATCH] eventExample: make unitAttack a bit safer --- plugins/devel/eventExample.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/plugins/devel/eventExample.cpp b/plugins/devel/eventExample.cpp index b5f9d34d4..ba9c2a00d 100644 --- a/plugins/devel/eventExample.cpp +++ b/plugins/devel/eventExample.cpp @@ -175,8 +175,15 @@ void unitAttack(color_ostream& out, void* ptr) { EventManager::UnitAttackData* data = (EventManager::UnitAttackData*)ptr; out.print("unit %d attacks unit %d\n", data->attacker, data->defender); df::unit* defender = df::unit::find(data->defender); + if (!defender) { + out.printerr("defender %d does not exist\n", data->defender); + return; + } int32_t woundIndex = df::unit_wound::binsearch_index(defender->body.wounds, data->wound); - df::unit_wound* wound = defender->body.wounds[woundIndex]; + df::unit_wound* wound = vector_get(defender->body.wounds, woundIndex); + if (!wound) { + return; + } set parts; for ( auto a = wound->parts.begin(); a != wound->parts.end(); a++ ) { parts.insert((*a)->body_part_id);