diff --git a/crates/scriptcheck/src/analyze.rs b/crates/scriptcheck/src/analyze.rs index ac430a96bf..526b667d74 100644 --- a/crates/scriptcheck/src/analyze.rs +++ b/crates/scriptcheck/src/analyze.rs @@ -27,7 +27,7 @@ use crate::catalog::{self, FeatureId}; use crate::directives; use crate::lex::{Lexer, T, Token, expr_after}; use crate::report::{self, Acc, OutsideId}; -use sig::{COMPOUND, LineBreaks, NEWER, Read, Sig, ends_value, for_in, goes_on_after, goes_on_with, is, member_mark, newer_number, unquote, word}; +use sig::{COMPOUND, LineBreaks, NEWER, Read, Sig, ends_value, exact_unquote, for_in, goes_on_after, goes_on_with, is, member_mark, newer_number, unquote, word}; mod declared; mod features; @@ -717,8 +717,9 @@ struct Analyzer<'s, 'a, 'l> { /// The last token was the `]` that closes an index of the global object. global_index_closed: bool, /// The offset of the last `]` that closes an index of the global object by a string literal - /// alone, with the variable name it reads (`$.global['local']`, `this['local']`). - global_bracket: Option<(usize, String)>, + /// alone, with the variable name it reads (`$.global['local']`, `this['local']`), or `None` when + /// the literal does not decode to one exact name. + global_bracket: Option<(usize, Option)>, /// The offset of the last name read as a property of the global object (`local` in /// `$.global.local`, `globalThis.local` or `this.local`). global_member: Option, diff --git a/crates/scriptcheck/src/analyze/features.rs b/crates/scriptcheck/src/analyze/features.rs index 05833f62df..0a4cff614a 100644 --- a/crates/scriptcheck/src/analyze/features.rs +++ b/crates/scriptcheck/src/analyze/features.rs @@ -1,6 +1,6 @@ //! The platform features, menu commands and later syntax the analysis recognises. -use super::{Analyzer, COMPOUND, FeatureId, NEWER, OutsideId, Pending, Sig, T, catalog, is, member_mark, newer_number, report, unquote, word}; +use super::{Analyzer, COMPOUND, FeatureId, NEWER, OutsideId, Pending, Sig, T, catalog, exact_unquote, 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] = [ @@ -592,7 +592,7 @@ impl<'s, 'a, 'l> Analyzer<'s, 'a, 'l> { && is(h1, "[") && let Some(key) = h0.filter(|s| s.kind == T::Str) { - self.global_bracket = Some((t.start, unquote(key.text, 256))); + self.global_bracket = Some((t.start, exact_unquote(key.text))); } } let changes = t.kind == T::Punct && (t.text == "=" || COMPOUND.contains(&t.text) || matches!(t.text, "++" | "--")); @@ -673,15 +673,20 @@ impl<'s, 'a, 'l> Analyzer<'s, 'a, 'l> { let global = if is(h1, ".") && h0.is_some_and(|m| m.kind == T::Ident) && write_key(h0) { bracketed(h2) } else if is(h0, "]") && is(h2, "[") && h1.is_some_and(|m| m.kind == T::Str) && write_key(h1) { - bracketed(h3).or_else(|| h3.filter(|s| s.kind == T::Ident && self.global_member == Some(s.start)).map(|s| s.text.to_string())) + bracketed(h3).or_else(|| h3.filter(|s| s.kind == T::Ident && self.global_member == Some(s.start)).map(|s| Some(s.text.to_string()))) } else { None }; - if let Some(name) = global { + match global { // The global variable's value may be saved under a body's or a `catch`'s own variable of // that name, so forget every value of the name. - self.forget_writer(&name); - self.share_early_as(&name, true); + Some(Some(name)) => { + self.forget_writer(&name); + self.share_early_as(&name, true); + } + // A key that does not decode to one exact name may name any variable. + Some(None) => self.forget_writers(), + None => {} } } // A name read in a function body that the body does not declare, as a receiver or otherwise. A @@ -701,20 +706,21 @@ impl<'s, 'a, 'l> Analyzer<'s, 'a, 'l> { // be the global object (`this`, `globalThis`, `$.global`, or any object holding it, as // `holder.this`), so the variable of that name may get another value. let rebound = if is(h1, ".") { - h0.filter(|s| s.kind == T::Ident).map(|s| s.text.to_string()) + h0.filter(|s| s.kind == T::Ident).map(|s| Some(s.text.to_string())) } else if is(h0, "]") && is(h2, "[") && h1.is_some_and(|s| s.kind == T::Str) { - h1.filter(|s| s.kind == T::Str).map(|s| unquote(s.text, 256)) + h1.filter(|s| s.kind == T::Str).map(|s| exact_unquote(s.text)) } else { None }; - if let Some(name) = rebound { + if let Some(Some(name)) = rebound { // In a function body: the function may run after a later literal gives the name methods. if !self.scopes.is_empty() { self.reassigned_later(&name); } self.forget_writer(&name); - } else if global_index_closed { - // `$.global[key] = …`: the key may name any of them. + } else if global_index_closed || rebound.is_some() { + // `$.global[key] = …`, or a key that does not decode to one exact name: the key may + // name any of them. self.forget_writers(); } } diff --git a/crates/scriptcheck/src/analyze/sig.rs b/crates/scriptcheck/src/analyze/sig.rs index edcb642cc5..24641a4908 100644 --- a/crates/scriptcheck/src/analyze/sig.rs +++ b/crates/scriptcheck/src/analyze/sig.rs @@ -135,6 +135,34 @@ pub(super) fn newer_number(text: &str) -> bool { text.contains('_') || (b.len() > 1 && b.last() == Some(&b'n')) || matches!((b.first(), b.get(1)), (Some(b'0'), Some(b'b' | b'B' | b'o' | b'O'))) } +/// A string literal's whole value ([`unquote`] without a length limit), or `None` when an escape +/// does not decode to exactly one character: a `\x` without two hex digits, a `\u` without four +/// (`\u{63}` included), or a code unit that is not a character on its own. +pub(super) fn exact_unquote(text: &str) -> Option { + let b = text.as_bytes(); + let mut i = 0; + while i < b.len() { + if b[i] == b'\\' { + let digits = match b.get(i + 1) { + Some(b'x') => 2, + Some(b'u') => 4, + _ => 0, + }; + if digits > 0 { + let hex = b.get(i + 2..i + 2 + digits).filter(|d| d.iter().all(u8::is_ascii_hexdigit))?; + // A surrogate code unit (`\uD800` to `\uDFFF`) is not a character on its own. + if digits == 4 && matches!(hex[0], b'd' | b'D') && matches!(hex[1], b'8'..=b'9' | b'a'..=b'f' | b'A'..=b'F') { + return None; + } + } + i += 2; + } else { + i += 1; + } + } + Some(unquote(text, text.chars().count())) +} + /// A string literal's value, its quotes removed and its escapes undone (`\n`, `\xHH`, `\uHHHH`, /// octal escapes, a backslash before a line break), up to `max` characters. pub(super) fn unquote(text: &str, max: usize) -> String { diff --git a/crates/scriptcheck/src/tests.rs b/crates/scriptcheck/src/tests.rs index da8506d6ff..9d7bee1afd 100644 --- a/crates/scriptcheck/src/tests.rs +++ b/crates/scriptcheck/src/tests.rs @@ -1209,6 +1209,34 @@ fn a_property_assignment_in_a_loop_ends_the_own_save_as() { "for (var i = 0; i < 2; i++) { local.saveAs(f); $.global['lo\\u0063al'] = app.activeDocument; }\n", )); assert_eq!(feature(&r, "file.write").map(|f| f.lines.as_slice()), Some(&[3][..])); + // Hex escapes and a line continuation in the key. + let r = report(concat!( + "var f = new File(p);\n", + "var local = {saveAs: function () {}};\n", + "for (var i = 0; i < 2; i++) { local.saveAs(f); this['lo\\x63\\\n", + "al'] = app.activeDocument; }\n", + )); + assert_eq!(feature(&r, "file.write").map(|f| f.lines.as_slice()), Some(&[3][..])); + // A key longer than 256 characters names the variable of that whole name. + let long = "v".repeat(300); + let r = report(&format!( + "var f = new File(p);\nvar {long} = {{saveAs: function () {{}}}};\nfor (var i = 0; i < 2; i++) {{ {long}.saveAs(f); $.global['{long}'] = app.activeDocument; }}\n" + )); + assert_eq!(feature(&r, "file.write").map(|f| f.lines.as_slice()), Some(&[3][..])); + let r = report(&format!("var f = new File(p);\nvar {long} = {{saveAs: function () {{}}}};\n$.global['{long}'].saveAs = w;\n{long}.saveAs(f);\n")); + assert_eq!(feature(&r, "file.write").map(|f| f.lines.as_slice()), Some(&[4][..])); + // A key whose escape does not decode to one exact name may name any variable. + let r = report(concat!( + "var f = new File(p);\n", + "var local = {saveAs: function () {}};\n", + "local.saveAs(f);\n", + "this['lo\\u{63}al'] = app.activeDocument;\n", + "local.saveAs(f);\n", + "var kept = {saveAs: function () {}};\n", + "$.global['k\\uD800'].saveAs = w;\n", + "kept.saveAs(f);\n", + )); + assert_eq!(feature(&r, "file.write").map(|f| f.lines.as_slice()), Some(&[5, 8][..])); } /// A function that replaces a method through the global object's property (`$.global.local.saveAs`)