From a4e9123bcceea0709d04865751579f994d23f207 Mon Sep 17 00:00:00 2001 From: Alexander Batalov Date: Wed, 5 Oct 2022 17:55:54 +0300 Subject: [PATCH] Fix crash in obj_use_item_on --- src/proto_instance.cc | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/src/proto_instance.cc b/src/proto_instance.cc index ef4d7ae..cd01fc1 100644 --- a/src/proto_instance.cc +++ b/src/proto_instance.cc @@ -1362,26 +1362,29 @@ int _obj_use_item_on(Object* a1, Object* a2, Object* a3) int flags = a3->flags & OBJECT_IN_ANY_HAND; itemRemove(a1, a3, 1); - Object* v7 = itemReplace(a1, a3, flags); + Object* replacedItem = itemReplace(a1, a3, flags); - int leftItemAction; - int rightItemAction; + // CE: Fix rare crash when using uninitialized action variables. The + // following code is on par with |_obj_use_item| which does not + // crash. if (a1 == gDude) { + int leftItemAction; + int rightItemAction; interfaceGetItemActions(&leftItemAction, &rightItemAction); - } - if (v7 == NULL) { - if ((flags & OBJECT_IN_LEFT_HAND) != 0) { - leftItemAction = INTERFACE_ITEM_ACTION_DEFAULT; - } else if ((flags & OBJECT_IN_RIGHT_HAND) != 0) { - rightItemAction = INTERFACE_ITEM_ACTION_DEFAULT; - } else { - leftItemAction = INTERFACE_ITEM_ACTION_DEFAULT; - rightItemAction = INTERFACE_ITEM_ACTION_DEFAULT; + if (replacedItem == NULL) { + if ((flags & OBJECT_IN_LEFT_HAND) != 0) { + leftItemAction = INTERFACE_ITEM_ACTION_DEFAULT; + } else if ((flags & OBJECT_IN_RIGHT_HAND) != 0) { + rightItemAction = INTERFACE_ITEM_ACTION_DEFAULT; + } else { + leftItemAction = INTERFACE_ITEM_ACTION_DEFAULT; + rightItemAction = INTERFACE_ITEM_ACTION_DEFAULT; + } } - } - interfaceUpdateItems(false, leftItemAction, rightItemAction); + interfaceUpdateItems(false, leftItemAction, rightItemAction); + } } _obj_destroy(a3);