Ticket #4161: 0004-Prevent-leaking-internal-data-in-cmpPlayer.patch

File 0004-Prevent-leaking-internal-data-in-cmpPlayer.patch, 2.7 KB (added by leper, 8 years ago)
  • binaries/data/mods/public/simulation/components/CeasefireManager.js

    From 3bc268aab08cd3e074e9ee83da3763c12457d6ed Mon Sep 17 00:00:00 2001
    From: leper <leper@wildfiregames.com>
    Date: Mon, 22 Aug 2016 04:52:23 +0200
    Subject: [PATCH 4/5] Prevent leaking internal data in cmpPlayer.
    
    ---
     .../mods/public/simulation/components/CeasefireManager.js     |  3 +--
     binaries/data/mods/public/simulation/components/Player.js     |  4 ++--
     .../mods/public/simulation/components/tests/test_Player.js    | 11 +++++++++++
     3 files changed, 14 insertions(+), 4 deletions(-)
    
    diff --git a/binaries/data/mods/public/simulation/components/CeasefireManager.js b/binaries/data/mods/public/simulation/components/CeasefireManager.js
    index 2f2e37f..3418569 100644
    a b CeasefireManager.prototype.StartCeasefire = function(ceasefireTime)  
    7171        // Save diplomacy
    7272        let numPlayers = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager).GetNumPlayers();
    7373        for (let i = 1; i < numPlayers; ++i)
    74             // Copy array with slice(), otherwise it will change
    75             this.diplomacyBeforeCeasefire.push(QueryPlayerIDInterface(i).GetDiplomacy().slice());
     74            this.diplomacyBeforeCeasefire.push(QueryPlayerIDInterface(i).GetDiplomacy());
    7675
    7776        // Set every enemy (except gaia) to neutral
    7877        for (let i = 1; i < numPlayers; ++i)
  • binaries/data/mods/public/simulation/components/Player.js

    diff --git a/binaries/data/mods/public/simulation/components/Player.js b/binaries/data/mods/public/simulation/components/Player.js
    index cb137dd..7959dd3 100644
    a b Player.prototype.GetLockTeams = function()  
    460460
    461461Player.prototype.GetDiplomacy = function()
    462462{
    463     return this.diplomacy;
     463    return this.diplomacy.slice();
    464464};
    465465
    466466Player.prototype.SetDiplomacy = function(dipl)
    467467{
    468     this.diplomacy = dipl;
     468    this.diplomacy = dipl.slice();
    469469
    470470    Engine.BroadcastMessage(MT_DiplomacyChanged, {
    471471        "player": this.playerID,
  • binaries/data/mods/public/simulation/components/tests/test_Player.js

    diff --git a/binaries/data/mods/public/simulation/components/tests/test_Player.js b/binaries/data/mods/public/simulation/components/tests/test_Player.js
    index fbd19e0..f8650c9 100644
    a b var cmpPlayer = ConstructComponent(10, "Player");  
    77
    88TS_ASSERT_EQUALS(cmpPlayer.GetPopulationCount(), 0);
    99TS_ASSERT_EQUALS(cmpPlayer.GetPopulationLimit(), 0);
     10
     11cmpPlayer.SetDiplomacy([-1, 1, 0, 1, -1]);
     12
     13var diplo = cmpPlayer.GetDiplomacy();
     14diplo[0] = 1;
     15TS_ASSERT(cmpPlayer.IsEnemy(0));
     16
     17diplo = [1, 1, 0];
     18cmpPlayer.SetDiplomacy(diplo);
     19diplo[1] = -1;
     20TS_ASSERT(cmpPlayer.IsAlly(1));