From 08aeb6faeb33b93e378af9ed308cd5acbdaf9efc Mon Sep 17 00:00:00 2001 From: lethosor Date: Sat, 5 Oct 2019 20:26:31 -0400 Subject: [PATCH 1/2] Fix EventManager building ID type The pointer to the building ID was sometimes not dereferenced properly. Addressed by switching to an ID cast to a pointer type for consistency with other types of events. Fixes #1434 Ref #1003 (19695b4ee7e8b370c8ce53aa2dcd399e1e9ca94d) Broken in #1253 (a7dfacd1c57490d08fc92a43eea963cff47c05eb) --- library/modules/Buildings.cpp | 2 +- library/modules/EventManager.cpp | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/library/modules/Buildings.cpp b/library/modules/Buildings.cpp index 732956a3e..607f63a36 100644 --- a/library/modules/Buildings.cpp +++ b/library/modules/Buildings.cpp @@ -1199,7 +1199,7 @@ void Buildings::clearBuildings(color_ostream& out) { void Buildings::updateBuildings(color_ostream& out, void* ptr) { - int32_t id = *((int32_t*)ptr); + int32_t id = (int32_t)(intptr_t)ptr; auto building = df::building::find(id); if (building) diff --git a/library/modules/EventManager.cpp b/library/modules/EventManager.cpp index 404d2342c..7d8b7e6d5 100644 --- a/library/modules/EventManager.cpp +++ b/library/modules/EventManager.cpp @@ -273,7 +273,7 @@ void DFHack::EventManager::onStateChange(color_ostream& out, state_change_event } for ( size_t a = 0; a < df::global::world->buildings.all.size(); a++ ) { df::building* b = df::global::world->buildings.all[a]; - Buildings::updateBuildings(out, (void*)&(b->id)); + Buildings::updateBuildings(out, (void*)intptr_t(b->id)); buildings.insert(b->id); } lastSyndromeTime = -1; @@ -609,7 +609,7 @@ static void manageBuildingEvent(color_ostream& out) { buildings.insert(a); for ( auto b = copy.begin(); b != copy.end(); b++ ) { EventHandler bob = (*b).second; - bob.eventHandler(out, (void*)&a); + bob.eventHandler(out, (void*)intptr_t(a)); } } nextBuilding = *df::global::building_next_id; @@ -625,7 +625,7 @@ static void manageBuildingEvent(color_ostream& out) { for ( auto b = copy.begin(); b != copy.end(); b++ ) { EventHandler bob = (*b).second; - bob.eventHandler(out, (void*)&id); + bob.eventHandler(out, (void*)intptr_t(id)); } a = buildings.erase(a); } From 582169e0a5bdcb5bceb60563ddca435d5a51095b Mon Sep 17 00:00:00 2001 From: lethosor Date: Sat, 5 Oct 2019 21:05:26 -0400 Subject: [PATCH 2/2] 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);