Script check: a nested function that assigns an outer body's variable keeps the write reported

A function body's assignment was left out of the names other code may
change when any enclosing body had declared the name with var. A nested
function such as swap() { d = app.activeDocument; } called before
d.saveAs(f) then left d trusted as the script's own literal, and the
write went unreported. Only a name the innermost body declared now counts
as that body's own; plain, compound, increment and for-in assignments to
an outer body's variable all keep the write reported.
This commit is contained in:
dotCooCoo committed 2026-11-28 06:10:24 -08:00
1 parent b086db0fbb
commit e46f697d50
2 files changed
+24 -18

No files matched your search

+5 -16
View File
@@ -1,6 +1,6 @@
//! The names declared, read before the analysis.
use std::collections::{HashMap, HashSet};
use std::collections::HashSet;
use super::{
Acc, Brackets, COMPOUND, Lexer, LineBreaks, Read, Sig, T, Token, catalog, directives, for_in, goes_on_after, goes_on_with, is, member_mark,
@@ -98,9 +98,6 @@ struct Declarations<'s, 'a, 'l> {
assign: Option<bool>,
/// The function bodies being read, the innermost last, up to [`crate::Limits::nesting`].
bodies: Vec<Body<'s>>,
/// The names the bodies being read declare so far, each with the number of those bodies that
/// declare it.
declared_around: HashMap<&'s str, usize>,
/// The bracket depth inside the outermost function body past that limit being read. The names
/// declared inside it are not kept.
untracked: Option<usize>,
@@ -120,7 +117,6 @@ pub(crate) fn declarations<'s>(src: &'s str, acc: &mut Acc) -> Declared<'s> {
var_depth: None,
assign: None,
bodies: vec![],
declared_around: HashMap::new(),
untracked: None,
keys: vec![],
out: Declared::default(),
@@ -208,8 +204,10 @@ impl<'s> Declarations<'s, '_, '_> {
} else {
None
};
// A name a body around `t` has declared with `var` before it is that body's own.
let own = |name: &str| self.untracked.is_none() && self.declared_around.contains_key(name);
// A name the innermost body around `t` has declared with `var` before it is that body's own. A
// name only an outer body declared is that body's variable, which a nested function may assign
// before a call in the outer body runs.
let own = |name: &str| self.untracked.is_none() && self.bodies.last().is_some_and(|b| b.names.contains(name));
if let Some(name) = assigned.filter(|n| !own(n)) {
if self.out.body_assigned.contains(name) || self.acc.room(self.out.body_assigned.len()) {
self.out.body_assigned.insert(name);
@@ -316,14 +314,6 @@ impl<'s> Declarations<'s, '_, '_> {
}
while let Some(body) = self.bodies.pop_if(|b| depth < b.depth) {
self.var_depth = body.var_depth;
for name in body.names {
if let Some(n) = self.declared_around.get_mut(name) {
*n = n.saturating_sub(1);
if *n == 0 {
self.declared_around.remove(name);
}
}
}
}
if self.untracked.is_some_and(|d| depth < d) {
self.untracked = None;
@@ -348,7 +338,6 @@ impl<'s> Declarations<'s, '_, '_> {
if let Some(body) = self.bodies.last_mut() {
if (var || body.depth == depth) && !body.names.contains(t.text) && self.acc.room(body.names.len()) {
body.names.insert(t.text);
*self.declared_around.entry(t.text).or_insert(0) += 1;
if let Some((_, names)) = self.out.bodies.get_mut(body.index) {
names.push(t.text);
}
+19 -2
View File
@@ -673,10 +673,11 @@ fn only_a_whole_var_value_makes_save_as_the_scripts_own() {
/// assignment of a property of that name, which may be the global object's, a function body that
/// assigns a `saveAs` or `exportFile` property of any object, code given as text, a `with` statement
/// and an included file each leave the write reported. A function that declares its own variable of the
/// name, or assigns another property of it, leaves it the script's own.
/// name, or assigns another property of it, leaves it the script's own. A nested function that assigns
/// a variable its enclosing body declared leaves the write reported too.
#[test]
fn a_save_as_other_code_may_replace_writes_a_file() {
let cases: [(&str, &[usize]); 12] = [
let cases: [(&str, &[usize]); 16] = [
("var f = new File(p);\nvar d = app.activeDocument;\nif (flag) label:\nvar d = {saveAs: function () {}};\nd.saveAs(f);\n", &[5]),
(
"var f = new File(p);\nvar d = {\n saveAs: function () {},\n useDocument: function () {\n d = null;\n d = app.activeDocument;\n }\n};\nd.useDocument();\nd.saveAs(f);\n",
@@ -698,6 +699,22 @@ fn a_save_as_other_code_may_replace_writes_a_file() {
),
("var f = new File(p);\nfunction note() { d.n = 1; }\nvar d = {saveAs: function () {}};\nnote();\nd.saveAs(f);\n", &[]),
("var f = new File(p);\nvar d = {saveAs: function () {}};\nfunction g() { var d = app.activeDocument; d = 1; }\nd.saveAs(f);\n", &[]),
(
"var f = new File(p);\nfunction run() {\n var d = {saveAs: function () {}};\n swap();\n d.saveAs(f);\n function swap() { d = app.activeDocument; }\n}\nrun();\n",
&[5],
),
(
"var f = new File(p);\nfunction run() {\n var d = {saveAs: function () {}};\n swap();\n d.saveAs(f);\n function swap() { d += 1; }\n}\n",
&[5],
),
(
"var f = new File(p);\nfunction run() {\n var d = {saveAs: function () {}};\n swap();\n d.saveAs(f);\n function swap() { d++; }\n}\n",
&[5],
),
(
"var f = new File(p);\nfunction run() {\n var d = {saveAs: function () {}};\n swap();\n d.saveAs(f);\n function swap() { for (d in o) {} }\n}\n",
&[5],
),
];
for (src, lines) in cases {
let r = report(src);