Skip to content

fix(encodes): remove signed left shift UB in utf8MultiByteCount - #754

Open
pppanghu77 wants to merge 1 commit into
linuxdeepin:masterfrom
pppanghu77:fix/384159-utf8-negshift-ub-master
Open

pppanghu77 wants to merge 1 commit into
linuxdeepin:masterfrom
pppanghu77:fix/384159-utf8-negshift-ub-master

Conversation

@pppanghu77

@pppanghu77 pppanghu77 commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

修复编码探测模块 utf8MultiByteCount 的有符号左移未定义行为(CWE-758)

WingFuzz 模糊测试(ASan+UBSan 插桩)发现:utf8MultiByteCount 内 LeftBitFunc lambda 形参为有符号 char,输入字节 ≥ 0x80 时(如 0x80 → -128)data <<= 1 对负值左移属未定义行为(C++11/14 [expr.shift]),UBSan trap 模式插入 ud2 陷阱导致进程 SIGILL(detectcode.cpp 内左移语句,illegal-instruction)。普通构建下该 UB "看起来正常"(0x80 左移截断为 0,返回 1),故长期潜伏。

修复方式与提单建议一致:

  • LeftBitFunc 形参 char → unsigned char,消除负值左移 UB
  • 调用处显式 static_cast<unsigned char>(*buf)
  • 任意输入字节的前导 1 计数结果不变(0x80 仍返回 1)

本 PR 为 master(V25)分支修复,与 release/eagle 的 #753 同源同内容。

验证:

  • 等价复现程序:修复前 UBSan 报 left shift of negative value -128、trap 模式 SIGILL 退出码 132;修复后无诊断、退出码 0、返回值不变
  • V25 构建机(x86_64)编译通过,DetectCode 相关单测 73/73 PASSED(含直接覆盖 utf8MultiByteCount 的参数化用例),编码探测/转换无功能回退

fix(encodes): remove signed left shift UB in utf8MultiByteCount

Log: 修复编码探测模块 utf8MultiByteCount 对负值有符号左移的未定义行为(CWE-758)
Bug: https://pms.uniontech.com/bug-view-384159.html
PMS: BUG-384159

Summary by Sourcery

Bug Fixes:

  • Eliminate undefined behavior in UTF-8 byte counting for input bytes with the high bit set, preventing sanitizer-triggered crashes while preserving detection results.

@sourcery-ai

sourcery-ai Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

Implementation

Use an unsigned byte in the leading-one counter and cast the input at the call site to avoid shifting a negative value.

  • src/encodes/detectcode.cpp L685, L698

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨

@deepin-bot

deepin-bot Bot commented Nov 30, 2026

Copy link
Copy Markdown
Contributor

TAG Bot

New tag: 6.6.11
DISTRIBUTION: unstable
Suggest: synchronizing this PR through rebase #757

- 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
@pppanghu77
pppanghu77 force-pushed the fix/384159-utf8-negshift-ub-master branch from 54eb9cc to 34a528a Compare November 30, 2026 07:57
@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: lzwind, pppanghu77

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@pppanghu77

Copy link
Copy Markdown
Contributor Author

/merge

@deepin-bot

deepin-bot Bot commented Nov 30, 2026

Copy link
Copy Markdown
Contributor

TAG Bot

New tag: 6.6.12
DISTRIBUTION: unstable
Suggest: synchronizing this PR through rebase #758

@deepin-bot

deepin-bot Bot commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

TAG Bot

New tag: 6.6.13
DISTRIBUTION: unstable
Suggest: synchronizing this PR through rebase #764

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants