Skip to content

Commit ca22a8d

Browse files
zhuanshicongCopilot
authored andcommitted
Potential fix for pull request finding
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
1 parent 92e1e62 commit ca22a8d

1 file changed

Lines changed: 17 additions & 3 deletions

File tree

NewLife.RocketMQ/Protocol/MessageExt.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,24 @@ public Boolean Read(ref SpanReader reader)
129129
throw new NotSupportedException($"消息使用 SysFlag 压缩类型 {compressType} (1=LZ4,2=ZSTD),当前实现仅支持 ZLIB/DEFLATE。");
130130

131131
// 兼容两种 DEFLATE 包装:
132-
// 1) RFC1950 ZLIB 格式:以 CMF 字节开头,低 4 位必须为 8 (DEFLATE),典型首字节 0x78
132+
// 1) RFC1950 ZLIB 格式:2 字节头部为 CMF/FLG,需同时满足:
133+
// - CM = 8(DEFLATE)
134+
// - CINFO <= 7(32K 窗口及以下)
135+
// - (CMF << 8 | FLG) % 31 == 0
133136
// 2) RFC1951 RAW DEFLATE:直接是 DEFLATE 块头,没有 2 字节 ZLIB 包裹
134-
// 通过首字节低 4 位是否为 8 判断走哪条路径,兼顾 Java 官方客户端与 NewLife/部分 5.x 客户端
135-
Body = (Body != null && Body.Length >= 2 && (Body[0] & 0x0F) == 8)
137+
// 仅当头部满足 RFC1950 约束时才剥离 2 字节,避免将 RAW DEFLATE 误判为 ZLIB
138+
var hasZlibHeader = false;
139+
if (Body != null && Body.Length >= 2)
140+
{
141+
var cmf = Body[0];
142+
var flg = Body[1];
143+
hasZlibHeader =
144+
(cmf & 0x0F) == 8 &&
145+
(cmf >> 4) <= 7 &&
146+
(((cmf << 8) + flg) % 31) == 0;
147+
}
148+
149+
Body = hasZlibHeader
136150
? Body.ReadBytes(2, -1).Decompress()
137151
: Body.Decompress();
138152
}

0 commit comments

Comments
 (0)