fix: 畸形 DHT 拒绝 + 渐进式 restart 重同步(M23 r1 P1-1/P1-2)
P1-1(内存安全):mj_huff_build 构造 9 位快表前校验规范码空间 (code + bits[l] > (1<<l) → 表非法),填表时再夹一道 <512 边界;mj_dht 失败 即返回 0 → 后端拒绝 → 回退 stb。此前过订阅 DHT(如「长度 2 的码 100 个」)会把 fast[512] 下标推到 12799,28~220 字节的畸形 JPEG 就栈溢出(ASan 实测 stack-buffer-overflow,非 ASan 直接 SIGSEGV/SIGBUS)。 回归:码长 1..4 × 符号数 3..255 共 1012 组畸形 DHT 全部后端拒绝、无崩溃; 「有效 JPEG + 注入坏 DHT」走拒绝 + 回退(不花屏)。 P1-2(能力):三处扫描循环的区间重同步统一到 mj_boundary,**只在 total == rst (编码器区间长度)时重同步**。原实现一见「over && marker∈D0..D7」就重同步,但熵码 位缓冲有最多 9 位前瞻,over 会在区间真正结束前 1~2 个 MCU 置位——提前重同步会丢掉 bitbuf 里本区间剩余块的有效比特,随即失步(渐进式 DC 扫描每块仅 ~1 bit,前瞻能跨过 整个 MCU)。这正是 Pillow restart_marker_blocks=1/2/3/4/5/8/16/64/200 的渐进式 100% 回退 stb(25.8MiB)的根因。另修 mj_restart 裸扫把 FF 00(数据字节 0xFF 的填充) 里的 00 误当 marker。 回归:9 个渐进式 + restart 夹具全部 BACKEND=1、峰值 1.86MiB(与无 restart 相同)、 与同编码无 restart 文件逐字节一致(mad=0.000);baseline + restart 仍逐字节一致。 附带(评审 P2 两项,改动极小且已验证): - PNG 8bit gray/RGB 的 tRNS 透明接通(8bit 采样值在 tRNS 低字节),与 stb 逐点 alpha 一致; - 拒绝 16bit 量化表(只属 12bit 精度;收下会让 F*qt 在 mj_accim 里 int32 溢出,UB)。 - meow_img_load_file_step 的 JPEG 走真缩解后端(PNG/BMP 暂留 stb:阅读器预算模型仍是 M22「整幅物化」口径,待 M27 同步后放开)。 验证:ASan 构建全 PASS;test_meow_img_stream 111 项全 PASS;test_meow_img_budget / test_meow_img_color 全 PASS;tuku run_tests.sh rc=0(zip 106 / cbz 296,含 membudget)、 run_webtoon_tests.sh rc=0;build_sdk.sh 零警告,MIPS32 rel2 目标码。 峰值不变(修复不增加分配):2000x3000 baseline/progressive 1.86MiB、PNG 1.33MiB。 体积:meow_jpeg.o 17,284B + meow_png_stream.o 8,456B = 25.1KB。
This commit is contained in:
1 parent
e9c0749609
commit
11a4fcd730
6 files changed
+377
-54
No files matched your search
+12
-5
@@ -128,17 +128,19 @@ static void png_get_px(const png_ctx *c, const unsigned char *row, int x, unsign
|
||||
} else {
|
||||
unsigned char g = (unsigned char)(v * 255 / ((1 << d) - 1));
|
||||
o[0] = o[1] = o[2] = g;
|
||||
o[3] = 255;
|
||||
/* tRNS 存的是原始采样值(0..2^d-1),不是展开后的 0..255 */
|
||||
o[3] = (c->color == 0 && c->have_trns && (unsigned)v == c->trns[0]) ? 0 : 255;
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (d == 8) {
|
||||
if (c->color == 0) { /* gray */
|
||||
o[0] = o[1] = o[2] = row[x];
|
||||
o[3] = 255;
|
||||
o[3] = (c->have_trns && row[x] == c->trns[0]) ? 0 : 255;
|
||||
} else if (c->color == 2) { /* RGB */
|
||||
o[0] = row[x * 3]; o[1] = row[x * 3 + 1]; o[2] = row[x * 3 + 2];
|
||||
o[3] = 255;
|
||||
o[3] = (c->have_trns && row[x * 3] == c->trns[0] &&
|
||||
row[x * 3 + 1] == c->trns[1] && row[x * 3 + 2] == c->trns[2]) ? 0 : 255;
|
||||
} else if (c->color == 3) { /* palette */
|
||||
int i = row[x];
|
||||
o[0] = c->pal[i][0]; o[1] = c->pal[i][1]; o[2] = c->pal[i][2];
|
||||
@@ -333,9 +335,14 @@ int meow_png_decode_stream(FILE *f, const unsigned char *mem, size_t len,
|
||||
if (c.color == 3) {
|
||||
for (i = 0; i < (int)clen && i < 256; i++) c.trns_a[i] = d[i];
|
||||
} else if (c.color == 0 && clen >= 2) {
|
||||
c.trns[0] = d[0]; /* 高字节(已降 8bit) */
|
||||
/* 8bit/低位深:采样值存在低字节;16bit:与像素一样取高字节比较 */
|
||||
c.trns[0] = (c.depth == 16) ? d[0] : d[1];
|
||||
} else if (c.color == 2 && clen >= 6) {
|
||||
c.trns[0] = d[0]; c.trns[1] = d[2]; c.trns[2] = d[4];
|
||||
if (c.depth == 16) {
|
||||
c.trns[0] = d[0]; c.trns[1] = d[2]; c.trns[2] = d[4];
|
||||
} else {
|
||||
c.trns[0] = d[1]; c.trns[1] = d[3]; c.trns[2] = d[5];
|
||||
}
|
||||
}
|
||||
} else if (memcmp(type, "IDAT", 4) == 0) {
|
||||
unsigned remain = clen;
|
||||
|
||||
Reference in new issue
Block a user