From 34a528afd5cfa573a850e4048766e377e10b2b03 Mon Sep 17 00:00:00 2001 From: liujinchang Date: Mon, 30 Nov 2026 09:54:15 +0800 Subject: [PATCH] fix(encodes): remove signed left shift UB in utf8MultiByteCount MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Change LeftBitFunc lambda parameter from char to unsigned char so the leading-ones loop no longer left-shifts a negative value (CWE-758), which UBSan trap mode reports as SIGILL illegal-instruction - Cast the byte to unsigned char explicitly at the call site; the leading-ones count is identical for every input byte 修复(encodes): 消除 utf8MultiByteCount 中的有符号左移未定义行为 - 将 LeftBitFunc lambda 形参由 char 改为 unsigned char,使前导 1 统计循环不再对负值做左移(CWE-758),UBSan trap 模式下原报 SIGILL 非法指令 - 调用处显式转换为 unsigned char;任意输入字节的前导 1 计数结果不变 Log: 修复编码探测模块 utf8MultiByteCount 对负值有符号左移的未定义行为(CWE-758) Bug: https://pms.uniontech.com/bug-view-384159.html PMS: BUG-384159 --- src/encodes/detectcode.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/encodes/detectcode.cpp b/src/encodes/detectcode.cpp index 498390fb..486fa9df 100644 --- a/src/encodes/detectcode.cpp +++ b/src/encodes/detectcode.cpp @@ -682,7 +682,7 @@ int utf8MultiByteCount(char *buf, size_t size) }; // 取得 UTF-8 字节序数值 - auto LeftBitFunc = [](char data) -> int { + auto LeftBitFunc = [](unsigned char data) -> int { // 返回前导1的个数 int res = 0; while (data & 0x80) { @@ -695,7 +695,7 @@ int utf8MultiByteCount(char *buf, size_t size) int count = 0; while (size > 0 && count < FourBytes) { qCDebug(appLog) << "Looping in utf8MultiByteCount, current size:" << size << ", count:" << count; - int leftBits = LeftBitFunc(*buf); + int leftBits = LeftBitFunc(static_cast(*buf)); qCDebug(appLog) << "Left bits for current byte:" << leftBits; switch (leftBits) { case Mid: