diff --git a/src/objmem.cpp b/src/objmem.cpp index e8b20e5b199..6d5d6879331 100644 --- a/src/objmem.cpp +++ b/src/objmem.cpp @@ -645,11 +645,27 @@ bool createFlagPosition(FLAG_POSITION **ppsNew, UDWORD player) return true; } +static bool isFlagPositionInList(FLAG_POSITION *psFlagPosToAdd) +{ + ASSERT_OR_RETURN(false, psFlagPosToAdd != nullptr, "Invalid FlagPosition pointer"); + ASSERT_OR_RETURN(false, psFlagPosToAdd->player < MAX_PLAYERS, "Invalid FlagPosition player: %u", psFlagPosToAdd->player); + for (FLAG_POSITION* psCurr = apsFlagPosLists[psFlagPosToAdd->player]; (psCurr != nullptr); psCurr = psCurr->psNext) + { + if (psCurr == psFlagPosToAdd) + { + return true; + } + } + return false; +} + /* add the Flag Position to the Flag Position Lists */ void addFlagPosition(FLAG_POSITION *psFlagPosToAdd) { ASSERT_OR_RETURN(, psFlagPosToAdd != nullptr, "Invalid FlagPosition pointer"); ASSERT_OR_RETURN(, psFlagPosToAdd->coords.x != ~0, "flag has invalid position"); + ASSERT_OR_RETURN(, psFlagPosToAdd->player < MAX_PLAYERS, "Invalid FlagPosition player: %u", psFlagPosToAdd->player); + ASSERT_OR_RETURN(, !isFlagPositionInList(psFlagPosToAdd), "FlagPosition is already in the list!"); psFlagPosToAdd->psNext = apsFlagPosLists[psFlagPosToAdd->player]; apsFlagPosLists[psFlagPosToAdd->player] = psFlagPosToAdd;