diff --git a/crates/scriptcheck/src/analyze.rs b/crates/scriptcheck/src/analyze.rs index bed249d6eb..0c9012c6ac 100644 --- a/crates/scriptcheck/src/analyze.rs +++ b/crates/scriptcheck/src/analyze.rs @@ -643,6 +643,9 @@ struct Analyzer<'s, 'a, 'l> { /// most function bodies that were open where it was read; a literal in a body with that many /// open or more is unaffected, since it gives a variable of its own. shared_early: HashMap, + /// [`Analyzer::shared_early`] had no room for a name: no literal gives methods that are the + /// script's own from then on. + shared_full: bool, /// The object literals being assigned to a variable, the innermost last, up to /// [`crate::Limits::nesting`] of them. objects: Vec, @@ -737,6 +740,7 @@ impl<'s, 'a, 'l> Analyzer<'s, 'a, 'l> { maybe_files_full: false, writers: HashMap::new(), shared_early: HashMap::new(), + shared_full: false, objects: vec![], closed: None, index_receiver: 0, @@ -1423,6 +1427,9 @@ impl<'s, 'a, 'l> Analyzer<'s, 'a, 'l> { { let h1 = self.h[1]; let declared = word(h1, "var") || (is(h1, ",") && self.var_depth.is_some()); + if !declared && !member_mark(h1) && !is(h1, "@") { + self.share_early(name.text); + } if declared { if self.in_catch(name.text) { self.declare_in_catch(name.text); @@ -1436,7 +1443,11 @@ impl<'s, 'a, 'l> Analyzer<'s, 'a, 'l> { self.assign = Some(Assign { name: name.text.to_string(), after_new: false }); } } - match self.updated(t) { + let updated = self.updated(t); + if let Some((name, _)) = updated { + self.share_early(name); + } + match updated { // The value on the right is read at this depth, and the name holds nothing the check // follows once that value ends ([`Analyzer::settle_groups`]). Some((name, true)) => self.start_value(name.to_string(), Operand::Other), @@ -1451,6 +1462,7 @@ impl<'s, 'a, 'l> Analyzer<'s, 'a, 'l> { fn settle_literal(&mut self, t: &Sig<'s>) { if let Some((name, bits)) = self.closed.take() && (t.is(";") || t.is(",") || (t.nl && !goes_on_with(t))) + && !self.shared_full && self.shared_early.get(&name).is_none_or(|&open| open <= self.scopes.len()) && (self.writers.contains_key(&name) || self.acc.room(self.writers.len())) { diff --git a/crates/scriptcheck/src/analyze/features.rs b/crates/scriptcheck/src/analyze/features.rs index 839476d420..d77b17a20c 100644 --- a/crates/scriptcheck/src/analyze/features.rs +++ b/crates/scriptcheck/src/analyze/features.rs @@ -2,6 +2,12 @@ use super::{Analyzer, COMPOUND, FeatureId, NEWER, OutsideId, Pending, Sig, T, catalog, is, member_mark, newer_number, report, unquote, word}; +/// Words that start or continue a statement or an expression, never a variable's name. +const RESERVED: [&str; 24] = [ + "var", "function", "if", "else", "for", "while", "do", "return", "new", "typeof", "delete", "in", "instanceof", "this", "with", "switch", "case", + "try", "catch", "finally", "throw", "break", "continue", "void", +]; + /// A global name of the platform the report looks for (`$`, `File`, `Socket`, `alert`, `eval`…). pub(super) fn platform_name(n: &str) -> bool { matches!( @@ -372,6 +378,23 @@ impl<'s, 'a, 'l> Analyzer<'s, 'a, 'l> { self.scopes.last().is_none_or(|s| s.saved.contains_key(name)) } + /// A function body uses `name`, which no literal has given methods and the body does not declare, + /// in a way that may change or share it ([`Analyzer::shared_early`]). With no room left for the + /// name, no literal's methods are the script's own from then on. + pub(super) fn share_early(&mut self, name: &str) { + let Some(body) = self.scopes.last() else { return }; + if body.saved.contains_key(name) || self.writers.contains_key(name) { + return; + } + if self.shared_early.contains_key(name) || self.acc.room(self.shared_early.len()) { + let open = self.scopes.len(); + let seen = self.shared_early.entry(name.to_string()).or_insert(open); + *seen = (*seen).max(open); + } else { + self.shared_full = true; + } + } + fn writer_bits(&self, receiver: Option>, before: Option>) -> u8 { match receiver { Some(r) if r.kind == T::Ident && !member_mark(before) => self.writers.get(r.text).copied().unwrap_or(0), @@ -449,12 +472,12 @@ impl<'s, 'a, 'l> Analyzer<'s, 'a, 'l> { if let Some(name) = escaped { self.writers.remove(name); } - if let Some(name) = early - && (self.shared_early.contains_key(name) || self.acc.room(self.shared_early.len())) - { - let open = self.scopes.len(); - let seen = self.shared_early.entry(name.to_string()).or_insert(open); - *seen = (*seen).max(open); + if let Some(name) = early { + self.share_early(name); + } + // A name read in a function body that the body does not declare, as a receiver or otherwise. + if let Some(r) = h0.filter(|r| r.kind == T::Ident && !member_mark(h1) && !RESERVED.contains(&r.text)) { + self.share_early(r.text); } if changes { let rebound = if is(h1, ".") { diff --git a/crates/scriptcheck/src/tests.rs b/crates/scriptcheck/src/tests.rs index 938981691e..c0e86dc0f1 100644 --- a/crates/scriptcheck/src/tests.rs +++ b/crates/scriptcheck/src/tests.rs @@ -738,7 +738,68 @@ fn a_hoisted_function_that_changes_or_shares_the_variable_ends_the_own_save_as() "function g1() { use(mine); }\n", "function g2() { var mine = {saveAs: function () {}}; mine.saveAs(f); }\n", )); - assert_eq!(feature(&r, "file.write").map(|f| f.lines.as_slice()), Some(&[4, 7, 11][..])); + assert_eq!(feature(&r, "file.write").map(|f| f.lines.as_slice()), Some(&[4, 7, 11, 13][..])); +} + +/// A table of shared names with no room left for one more means a function may share any name: no +/// literal after it gives methods that are the script's own. +#[test] +fn a_full_table_of_shared_names_keeps_reporting_the_write() { + let src = concat!( + "var f = new File(p);\n", + "function a(w) { p1.saveAs = w; p2.saveAs = w; p3.saveAs = w; }\n", + "var local = {saveAs: function () {}};\n", + "local.saveAs(f);\n", + ); + let r = check("test.jsx", src.as_bytes(), None, &Limits { entries: 2, ..Limits::default() }); + assert_eq!(feature(&r, "file.write").map(|f| f.lines.as_slice()), Some(&[4][..])); +} + +/// A function body that assigns a variable it does not declare (`=`, a compound operator, `++` or +/// `--`) may run after the literal and give the variable another value. +#[test] +fn an_assignment_in_a_hoisted_function_ends_the_own_save_as() { + let r = report(concat!( + "var f = new File(p);\n", + "function a(w) { local = w; }\n", + "var local = {saveAs: function () {}};\n", + "local.saveAs(f);\n", + "function b() { other += 1; }\n", + "var other = {saveAs: function () {}};\n", + "other.saveAs(f);\n", + "function c() { third++; }\n", + "var third = {saveAs: function () {}};\n", + "third.saveAs(f);\n", + "function e() { --fourth; }\n", + "var fourth = {saveAs: function () {}};\n", + "fourth.saveAs(f);\n", + "function d() { var mine = 1; mine = 2; }\n", + "var mine = {saveAs: function () {}};\n", + "mine.saveAs(f);\n", + )); + assert_eq!(feature(&r, "file.write").map(|f| f.lines.as_slice()), Some(&[4, 7, 10, 13][..])); +} + +/// A function body that calls a method of, or reads, a variable it does not declare may run before +/// the literal as well, so a later literal's calls keep reporting the write. +#[test] +fn a_member_call_or_read_in_a_hoisted_function_ends_the_own_save_as() { + let r = report(concat!( + "var f = new File(p);\n", + "function a() { local.run(); }\n", + "var local = {saveAs: function () {}};\n", + "local.saveAs(f);\n", + "function b() { other['m'](); }\n", + "var other = {saveAs: function () {}};\n", + "other.saveAs(f);\n", + "function c() { return third.name; }\n", + "var third = {saveAs: function () {}};\n", + "third.saveAs(f);\n", + "function d() { var calm = 1; return calm.name; }\n", + "var calm = {saveAs: function () {}};\n", + "calm.saveAs(f);\n", + )); + assert_eq!(feature(&r, "file.write").map(|f| f.lines.as_slice()), Some(&[4, 7, 10][..])); } /// A function that reads the variable from outside runs after whatever the script gave it since, so