[代码质量/鲁棒性] 底层二进制解码过度依赖非空断言 !:缺失防御性越界校验与异常统一 #11
Labels
No Label
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date
No due date set.
Blocks
#7 [Code Review] 整体代码质量、架构缺陷与技术债务全景审查报告
troytt/diablo2-web
Reference: troytt/diablo2-web#11
Loading…
Reference in New Issue
No description provided.
Delete Branch "%!s(<nil>)"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
模块位置
src/formats/bitstream.ts,pcx.ts,cel.ts,cof.ts,dcc.ts,pal.ts,dt1.ts等问题背景与现象
data[i]!压制 TypeScript 空安全检查。造成的影响
TypeError: Cannot read properties of undefined;CelError、MpqError、PcxError领域异常体系,丢失了具体的偏移量(Offset)和损坏位置信息,给故障排查带来困难。解决建议
bitstream.ts与格式解析基础库中提供安全的切片与步进读取函数(如safeByte(data, offset)、requireExtent(offset, length, limit));已修复 — 边界检查读取器与领域错误 (
a6ca337→f52edd8)新增
src/formats/reader.ts:FormatError/TruncatedDataError/InvalidFieldError错误层次 +ByteReader游标 +requireBytes()。错误携带结构化字段(format/field/offset/needed/available),消息形如:dcc: truncated reading header.framesPerDirection at offset 0x1A4: need 4 bytes, only 2 available (buffer length 420)按
pal → bitstream → pcx → dc6 → cel → pl2 → ds1 → dt1 → cof → dcc → crypt → archive → implode的顺序逐个改造,除字节可用性外还校验结构不变量:magic / 版本 / 用作循环上界或分配尺寸的计数(在分配之前拒绝荒谬值,避免一个损坏的 u32 触发 GB 级分配)/ 必须落在缓冲区内的偏移。对合法输入的解码行为逐字节不变,这是加固而非重写。]!的收敛情况:formats/+mpq/中]!总数剩余 29 处全部是编译期可证安全的结构化访问(常量表、已校验长度的内部数组、预建哈希表),例如
dt1.ts的ISO_JUMP[row]!受row < ISO_JUMP.length约束,pcx.ts的palette[index]!中 palette 恒为 768 字节而index上界为 767。这些逐一复核过。验证(冷装):
tsc --noEmit0 错误;全仓grep ": any\|as any" src/干净;npm run verify:formats通过(已接入verify:all)。变异测试(我方独立选定): 将
reader.ts中requireBytes的边界判断置为false→reader.test.ts > requireBytes > validates direct offsets转红并给出精确期望消息;还原后转绿。