From 1d546e0211dcf35aaadaa989c469b22ec26b42a2 Mon Sep 17 00:00:00 2001 From: Joywayer Date: Fri, 28 Aug 2026 00:59:22 +0800 Subject: [PATCH] =?UTF-8?q?fix(ui-migration):=20frameFileName=20=E8=A1=A5?= =?UTF-8?q?=E4=B8=8A=E7=95=8C=E6=A0=A1=E9=AA=8C=EF=BC=88=E9=98=B2=E6=AD=A2?= =?UTF-8?q?=E9=87=8E=E6=8C=87=E9=92=88=E5=BC=95=E7=94=A8=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 问题:frameFileName 只校验下界(frameIndex > 0),未校验上界(frameIndex <= frameCount)。 结果:会安静产出指向不存在文件的名字(如 frameFileName('00014.png', 999, 12) → '00014_999.png'), 一路流进中间描述,直到 Cocos 加载资源才报错。 这是「把读不出来当成没问题」这一形态的第四次出现: - Task 2: readPngSize 返回 null 被当成校验通过 - 前期: ??: 缺顶层键当成空集合 - 前轮: frameIndex 任何 falsy 当成帧 1 - 本轮: frameFileName 超界当成合法产出垃圾名字 修法:加上界检查 `normalized > frameCount`,错误信息对齐 frameRect 格式。 新增 1 条测试用例:frameFileName('00014.png', 13, 12) 应抛错。 顺手补齐 frameFileName 的 undefined/NaN 测试的消息断言(与 frameRect 对齐)。 Co-Authored-By: Claude Opus 5 (1M context) --- cocoscreator_projects/scripts/test/ui-frames.test.mjs | 7 +++++++ cocoscreator_projects/scripts/ui-migration/frames.mjs | 4 ++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/cocoscreator_projects/scripts/test/ui-frames.test.mjs b/cocoscreator_projects/scripts/test/ui-frames.test.mjs index 515862d..fd662d2 100644 --- a/cocoscreator_projects/scripts/test/ui-frames.test.mjs +++ b/cocoscreator_projects/scripts/test/ui-frames.test.mjs @@ -73,10 +73,17 @@ test('frameFileName 单帧图原样返回', () => { test('frameFileName undefined 作为 frameIndex 抛 RangeError', () => { assert.throws(() => frameFileName('00014.png', undefined, 12), RangeError); + assert.throws(() => frameFileName('00014.png', undefined, 12), /00014\.png/); }); test('frameFileName NaN 作为 frameIndex 抛 RangeError', () => { assert.throws(() => frameFileName('00014.png', NaN, 12), RangeError); + assert.throws(() => frameFileName('00014.png', NaN, 12), /00014\.png/); +}); + +test('frameFileName 越界抛 RangeError 且带图名', () => { + assert.throws(() => frameFileName('00014.png', 13, 12), RangeError); + assert.throws(() => frameFileName('00014.png', 13, 12), /00014\.png/); }); test('frameNames 返回全部帧,下标 0 对应帧 1', () => { diff --git a/cocoscreator_projects/scripts/ui-migration/frames.mjs b/cocoscreator_projects/scripts/ui-migration/frames.mjs index acf0a84..ffc4e12 100644 --- a/cocoscreator_projects/scripts/ui-migration/frames.mjs +++ b/cocoscreator_projects/scripts/ui-migration/frames.mjs @@ -29,9 +29,9 @@ export function padWidth(frameCount) { export function frameFileName(bmp, frameIndex, frameCount) { if (frameCount <= 1) return bmp; const normalized = frameIndex === 0 ? 1 : frameIndex; - if (!Number.isInteger(normalized) || normalized <= 0) { + if (!Number.isInteger(normalized) || normalized <= 0 || normalized > frameCount) { throw new RangeError( - `帧号非法: ${bmp} frameIndex=${frameIndex}(有效范围 >= 1,0 视同 1)`, + `帧号非法或越界: ${bmp} frameIndex=${frameIndex}(有效范围 1..${frameCount},0 视同 1)`, ); } const base = bmp.replace(/\.png$/i, '');