From 183902ced323bd873a0c5264b47f6a37ca6b6c78 Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Fri, 11 Oct 2019 18:32:36 +0200 Subject: [PATCH] Port fix incorrect property checks in researchSync Fix naive checks for properties being set of the form obj[key] into the proper form Object.hasOwnProperty.call(obj, key). The reason for this is that if the key has a value of a built-in property like 'constructor' then a check of the form obj[key] will always return true even if that property hasn't been set on the object. This is a port of 6a6274e. --- CHANGELOG.md | 7 +++ sharedPlugins/researchSync/index.js | 69 ++++++++++++++++------------- 2 files changed, 45 insertions(+), 31 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 824e2746..1ac4de35 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -28,6 +28,13 @@ Version 2.0.0 .ejs. Breaks playerManager. +Version 1.2.2 +------------- + +- Fixed possible crash with modded technologies named the same as a built-in + Object prototype property in researchSync. + + Version 1.2.1 ------------- diff --git a/sharedPlugins/researchSync/index.js b/sharedPlugins/researchSync/index.js index 918d63e9..1aeef8b0 100644 --- a/sharedPlugins/researchSync/index.js +++ b/sharedPlugins/researchSync/index.js @@ -145,10 +145,11 @@ class ResearchSync { } clear_contribution_to_researched_techs() { - // if between updates tech was researched - if (!this.prev_research) - return for (let [name, research] of Object.entries(this.research)) { + if (!Object.hasOwnProperty.call(this.prev_research, name)) { + continue; + } + let researched if (research.infinite) researched = this.prev_research[name].level < research.level @@ -168,7 +169,7 @@ class ResearchSync { if (isNaN(node_tech.researched) || isNaN(node_tech.level) || isNaN(node_tech.infinite)) continue - if (cluster_techs[name]) { + if (Object.hasOwnProperty.call(cluster_techs, name)) { if (cluster_techs[name].infinite === 1 && cluster_techs[name].level < node_tech.level) { cluster_techs[name].level = node_tech.level } else if (node_tech.researched > cluster_techs[name].researched) { @@ -188,8 +189,9 @@ class ResearchSync { for (let slave_data of slaves_data) { for (let [name, research] of Object.entries(slave_data.meta.research)) { - if (!cluster_researches[name]) - continue + if (!Object.hasOwnProperty.call(cluster_researches, name)) { + continue; + } if (isNaN(cluster_researches[name].progress) || isNaN(research.contribution) || isNaN(research.level) @@ -213,35 +215,37 @@ class ResearchSync { } filter_researched_techs(cluster_researches) { - let local_researches = this.research let result = {}; - for (let key in local_researches) { - if (!cluster_researches[key]) - continue - if (isNaN(cluster_researches[key].researched) || isNaN(cluster_researches[key].level)) + for (let [name, tech] of Object.entries(this.research)) { + if (!Object.hasOwnProperty.call(cluster_researches, name)) { + continue; + } + + if (isNaN(cluster_researches[name].researched) || isNaN(cluster_researches[name].level)) continue let researched - if (local_researches[key].infinite) - researched = local_researches[key].level < cluster_researches[key].level - else - researched = local_researches[key].researched < cluster_researches[key].researched + if (tech.infinite) { + researched = tech.level < cluster_researches[name].level; + } else { + researched = tech.researched < cluster_researches[name].researched; + } if (researched) - result[key] = cluster_researches[key] + result[name] = cluster_researches[name] } return result; } filter_updated_techs(cluster_techs, to_research) { - let local_techs = this.research let result = {} - for (let name in local_techs) { - if (name in to_research || !cluster_techs[name]) - continue + for (let [name, tech] of Object.entries(this.research)) { + if (Object.hasOwnProperty.call(to_research, name) || !Object.hasOwnProperty.call(cluster_techs, name)) { + continue; + } if (isNaN(cluster_techs[name].progress)) continue - if (local_techs[name].progress < cluster_techs[name].progress) + if (tech.progress < cluster_techs[name].progress) result[name] = cluster_techs[name] } return result @@ -249,8 +253,9 @@ class ResearchSync { research_technologies(to_research) { for (let name of Object.keys(to_research)) - if (!this.research[name]) - delete to_research[name] + if (!Object.hasOwnProperty.call(this.research, name)) { + delete to_research[name]; + } const notify = Object.keys(to_research).length === 1 for (let [name, tech] of Object.entries(to_research)) { @@ -274,8 +279,9 @@ class ResearchSync { update_technologies_progress(to_update) { for (let [name, tech] of Object.entries(to_update)) { - if (!this.research[name]) - continue + if (!Object.hasOwnProperty.call(this.research, name)) { + continue; + } let progress = this.research[name].progress if (progress === null) progress = 'nil' @@ -292,11 +298,10 @@ class ResearchSync { } print_own_contribution() { - if (!this.prev_research) - return for (let [name, tech] of Object.entries(this.research)) { - if (!this.prev_research[name]) - continue + if (!Object.hasOwnProperty.call(this.prev_research, name)) { + continue; + } let diff = this.research[name].contribution - this.prev_research[name].contribution if (Math.abs(diff) > Number.EPSILON * 1000) this.log(`Own research ${name}: ${this.research[name].progress} += ${diff}`) @@ -321,8 +326,10 @@ class ResearchSync { if (isNaN(level) || isNaN(researched)) return - this.prev_research[name] = this.research[name] - if (!this.prev_research[name]) { + + if (Object.hasOwnProperty.call(this.research, name)) { + this.prev_research[name] = this.research[name] + } else { this.prev_research[name] = { researched: null, level: null,