Repository navigation
fix: resolve signed shift UB in escape parsing - #596
Conversation
Reviewer's GuideImplementationMake the UTF-8 byte classifier shift an unsigned value to avoid undefined behavior on high-bit bytes.
Accumulate hexadecimal and octal escape values as unsigned integers and guard shifts to prevent overflow.
Extract the UTF-8 byte-count helper from detectcode.cpp into its own implementation and include its declaration for the existing caller.
ConfigurationAdd the extracted UTF-8 byte-count implementation to the terminal widget build and test source lists.
TestsAdd focused tests for UTF-8 byte classification, including high-bit bytes and boundary inputs.
Cover common, oversized, invalid, and mixed escape parsing behavior in SettingIO.
Other changesFiles not grouped.
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've found 1 issue
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="src/common/settingio.cpp" line_range="461-463" />
<code_context>
}
if ((ch >= '0' && ch <= '9') || (ch >= 'A' && ch <= 'F')) {
// qDebug() << "Branch: valid hex digit in sequence";
+ if (escapeVal > 0x0FFFFFFFu) {
+ stringResult += QChar(escapeVal);
+ goto normal;
+ }
escapeVal <<= 4;
</code_context>
<issue_to_address>
**Oversized escapes split into text**
When a hex or octal escape has enough significant digits to exceed 32 bits, the guard appends only the accumulated prefix and jumps to `normal`, where the first excess digit is processed as ordinary text; a long escape such as `\x100000000` therefore becomes a code unit plus a literal suffix in the configuration value.
Define a consistent policy for oversized escapes, such as consuming the full sequence while safely bounding the value or rejecting it, rather than emitting a prefix and treating the rest as literal text.
Also at `src/common/settingio.cpp:485-488`.
</issue_to_address>| if (escapeVal > 0x0FFFFFFFu) { | ||
| stringResult += QChar(escapeVal); | ||
| goto normal; |
There was a problem hiding this comment.
🟡 Medium · Oversized escapes split into text
When a hex or octal escape has enough significant digits to exceed 32 bits, the guard appends only the accumulated prefix and jumps to normal, where the first excess digit is processed as ordinary text; a long escape such as \x100000000 therefore becomes a code unit plus a literal suffix in the configuration value.
Define a consistent policy for oversized escapes, such as consuming the full sequence while safely bounding the value or rejecting it, rather than emitting a prefix and treating the rest as literal text.
Also at src/common/settingio.cpp:485-488.
Prompt for AI agents
In `src/common/settingio.cpp` at lines 461-463:
**Oversized escapes split into text**
When a hex or octal escape has enough significant digits to exceed 32 bits, the guard appends only the accumulated prefix and jumps to `normal`, where the first excess digit is processed as ordinary text; a long escape such as `\x100000000` therefore becomes a code unit plus a literal suffix in the configuration value.
Define a consistent policy for oversized escapes, such as consuming the full sequence while safely bounding the value or rejecting it, rather than emitting a prefix and treating the rest as literal text.
Also at `src/common/settingio.cpp:485-488`.3d2de9f to
d89750b
Compare
d89750b to
7fef9a1
Compare
1. Root cause: SettingIO::unescapedString() used signed int for escapeVal, left-shift operations triggered signed integer overflow UB (CWE-190/CWE-758) when accumulating hex/octal values 2. Root cause: utf8MultiByteCount() LeftBitFunc lambda took char parameter, signed char implicit conversion caused UB on high bytes (>=0x80) during bit testing 3. Fix: change escapeVal to unsigned int and add range checks before shift operations; change LeftBitFunc parameter to unsigned char 4. Impact: eliminates undefined behavior, no behavior change for valid inputs, no API/ABI change Influence: 1. Test terminal config read/write with common escape sequences 2. Test UTF-8 multi-byte character detection with high bytes 3. Verify no regression on existing escape parsing behavior fix: 修复转义解析中的有符号移位未定义行为 1. 根因:SettingIO::unescapedString() 使用有符号 int 存储 escapeVal, 左移操作在累积十六进制/八进制值时触发有符号整数溢出 UB (CWE-190/CWE-758) 2. 根因:utf8MultiByteCount() 的 LeftBitFunc lambda 参数为 char, 有符号 char 在高字节(>=0x80)位测试时隐式转换导致 UB 3. 方案:escapeVal 改为 unsigned int 并在移位前增加范围检查; LeftBitFunc 参数改为 unsigned char 4. 影响:消除未定义行为,合法输入路径无行为变化,无 API/ABI 变化 Influence: 1. 测试终端配置文件含常见转义字符的读写 2. 测试含高字节 UTF-8 字符的编码检测 3. 验证现有转义解析行为无回归 Bug: #383495 PMS: BUG-383495
bb57079 to
b051470
Compare
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: liyigang1, max-lvs The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
|
/forcemerge |
|
This pr force merged! (status: unstable) |
cea82f2
into
linuxdeepin:release/eagle
Root Cause Analysis
SettingIO::unescapedString()used signedintforescapeVal, and the hex/octal escape parsing loop performedescapeVal <<= 4/<<= 3without iteration limits, causing signed integer overflow UB (CWE-190/CWE-758) on long escape sequences.utf8MultiByteCount()'sLeftBitFunclambda tookcharparameter — on x86_64charis signed, so UTF-8 leading bytes (≥0x80) became negative, anddata <<= 1on a negative value is UB. Both confirmed via UBSan independent verifiers.Fix
Changed
escapeValfrominttounsigned intand added range checks (> 0x0FFFFFFFfor hex,> 0x1FFFFFFFfor octal) before shift operations to prevent overflow. ChangedLeftBitFuncparameter fromchartounsigned charto eliminate negative left-shift UB.Change Safety Assessment
Code Safety
unescapedString()only called by config I/O;utf8MultiByteCount()only called within Gb18030→UTF-8 transcoding pathBusiness Impact Scope
Verification Suggestion
Test terminal config file read/write with common escape sequences; test UTF-8 encoding detection with high bytes; verify no regression in existing escape parsing.
根因分析
SettingIO::unescapedString()使用有符号int存储escapeVal,十六进制/八进制转义解析循环escapeVal <<= 4/<<= 3无迭代上限,超长转义序列触发有符号整数溢出 UB(CWE-190/CWE-758)。utf8MultiByteCount()的LeftBitFunclambda 参数为有符号char,UTF-8 首字节 ≥ 0x80 时为负值,data <<= 1对负值左移为 UB。两处 UB 均通过 UBSan 独立验证器复现确认。修复方案
将
escapeVal从int改为unsigned int,在 hex/octal 左移前增加范围检查(> 0x0FFFFFFF/> 0x1FFFFFFF)防止溢出。将LeftBitFunc参数从char改为unsigned char,消除负值左移 UB。改动安全评估
代码安全评估
unescapedString()仅被配置 I/O 调用;utf8MultiByteCount()仅被 Gb18030→UTF-8 转码路径调用业务影响范围
验证建议
测试终端配置文件含常见转义字符的读写功能;测试含高字节 UTF-8 字符的编码检测功能;验证现有转义解析行为无回归。
PMS: BUG-383495
Summary by Sourcery
Prevent undefined behavior in escape parsing and UTF-8 detection while preserving valid-input behavior.
Bug Fixes:
Enhancements:
Build:
Tests: