From d63be36fb37cb95af7bbb152b416b09d25094c64 Mon Sep 17 00:00:00 2001 From: Kp Date: Sun, 22 Mar 2015 22:48:47 +0000 Subject: [PATCH] Fix bogus trap on remote_owner check Some call sites pass a uint8_t to objnum_remote_to_local, which is zero-extended up to an int. The check for owner==-1 then fails, causing the sanity check to trap to the debugger, even though the situation is normal and harmless. Switch the type to int8_t to ensure that the value is not sign/zero-extended. --- common/main/multi.h | 4 ++-- similar/main/multi.cpp | 20 +++++++++++--------- similar/main/multibot.cpp | 6 +++--- 3 files changed, 16 insertions(+), 14 deletions(-) diff --git a/common/main/multi.h b/common/main/multi.h index 3248dc9a5..6c74d3420 100644 --- a/common/main/multi.h +++ b/common/main/multi.h @@ -230,8 +230,8 @@ struct owned_remote_objnum extern int GetMyNetRanking(); extern void ClipRank (ubyte *rank); -objnum_t objnum_remote_to_local(int remote_obj, int owner); -short objnum_local_to_remote(objnum_t local_obj, sbyte *owner); +objnum_t objnum_remote_to_local(uint16_t remote_obj, int8_t owner); +uint16_t objnum_local_to_remote(objnum_t local_obj, int8_t *owner); owned_remote_objnum objnum_local_to_remote(objnum_t local); void map_objnum_local_to_remote(int local, int remote, int owner); void map_objnum_local_to_local(objnum_t objnum); diff --git a/similar/main/multi.cpp b/similar/main/multi.cpp index de18f302c..1075c9609 100644 --- a/similar/main/multi.cpp +++ b/similar/main/multi.cpp @@ -83,6 +83,8 @@ COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED. #include "partial_range.h" #include "highest_valid.h" +constexpr tt::integral_constant owner_none{}; + static void multi_reset_object_texture(const vobjptr_t objp); static void multi_add_lifetime_killed(); static void multi_send_heartbeat(); @@ -257,9 +259,9 @@ void ClipRank (ubyte *rank) // Functions that replace what used to be macros // -objnum_t objnum_remote_to_local(int remote_objnum, int owner) +objnum_t objnum_remote_to_local(uint16_t remote_objnum, int8_t owner) { - if (owner == -1) + if (owner == owner_none) return(remote_objnum); // Map a remote object number from owner to a local object number if ((owner >= N_players) || (owner < -1)) { @@ -267,7 +269,7 @@ objnum_t objnum_remote_to_local(int remote_objnum, int owner) return(remote_objnum); } - if ((remote_objnum < 0) || (remote_objnum >= MAX_OBJECTS)) + if (remote_objnum >= MAX_OBJECTS) return(object_none); auto result = remote_to_local[owner][remote_objnum]; @@ -278,10 +280,10 @@ owned_remote_objnum objnum_local_to_remote(objnum_t local_objnum) { // Map a local object number to a remote + owner if ((local_objnum < 0) || (local_objnum > Highest_object_index)) { - return {-1, -1}; + return {owner_none, -1}; } auto owner = object_owner[local_objnum]; - if (owner == -1) + if (owner == owner_none) return {owner, local_objnum}; if (owner >= N_players || owner < -1) throw std::runtime_error("illegal object owner"); @@ -291,7 +293,7 @@ owned_remote_objnum objnum_local_to_remote(objnum_t local_objnum) return {owner, result}; } -short objnum_local_to_remote(objnum_t local_objnum, sbyte *owner) +uint16_t objnum_local_to_remote(objnum_t local_objnum, int8_t *owner) { auto r = objnum_local_to_remote(local_objnum); *owner = r.owner; @@ -1733,7 +1735,7 @@ static void multi_do_kill(const playernum_t pnum, const ubyte *buf) count += 1; killer = GET_INTEL_SHORT(buf + count); if (killer > 0) - killer = objnum_remote_to_local(killer, (sbyte)buf[count+2]); + killer = objnum_remote_to_local(killer, buf[count+2]); if (!multi_i_am_master()) { Netgame.team_vector = buf[5]; @@ -1805,10 +1807,10 @@ void static multi_do_remobj(const ubyte *buf) { short objnum; // which object to remove - sbyte obj_owner; // which remote list is it entered in objnum = GET_INTEL_SHORT(buf + 1); - obj_owner = buf[3]; + // which remote list is it entered in + auto obj_owner = buf[3]; Assert(objnum >= 0); diff --git a/similar/main/multibot.cpp b/similar/main/multibot.cpp index 650cfc431..06d6bb848 100644 --- a/similar/main/multibot.cpp +++ b/similar/main/multibot.cpp @@ -673,7 +673,7 @@ void multi_do_release_robot(const playernum_t pnum, const ubyte *buf) short remote_botnum; remote_botnum = GET_INTEL_SHORT(buf + 2); - auto botnum = objnum_remote_to_local(remote_botnum, (sbyte)buf[4]); + auto botnum = objnum_remote_to_local(remote_botnum, buf[4]); if ((botnum < 0) || (botnum > Highest_object_index)) { return; @@ -707,7 +707,7 @@ void multi_do_robot_position(const playernum_t pnum, const ubyte *buf) ; loc += 1; remote_botnum = GET_INTEL_SHORT(buf + loc); - auto botnum = objnum_remote_to_local(remote_botnum, (sbyte)buf[loc+2]); loc += 3; + auto botnum = objnum_remote_to_local(remote_botnum, buf[loc+2]); loc += 3; if ((botnum < 0) || (botnum > Highest_object_index)) { return; @@ -767,7 +767,7 @@ multi_do_robot_fire(const ubyte *buf) loc += 1; // pnum remote_botnum = GET_INTEL_SHORT(buf + loc); - auto botnum = objnum_remote_to_local(remote_botnum, (sbyte)buf[loc+2]); loc += 3; + auto botnum = objnum_remote_to_local(remote_botnum, buf[loc+2]); loc += 3; gun_num = (sbyte)buf[loc]; loc += 1; memcpy(&fire, buf+loc, sizeof(vms_vector)); fire.x = (fix)INTEL_INT((int)fire.x);