Skip to content

fix: CWE-787 heap buffer overflow in CharaDataManger::loadCharaData - #65

Open
fly602 wants to merge 1 commit into
masterfrom
agent/agent/6e487a86e304
Open

fly602 wants to merge 1 commit into
masterfrom
agent/agent/6e487a86e304

Conversation

@fly602

@fly602 fly602 commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Security Fix

CWE-787: Out-of-bounds Write (charadatamanger.cpp:83)

  • Problem: loadCharaData() allocates memory based on faceCharaSize from JSON, but writes jsonArray.size() elements. If jsonArray.size() > faceCharaSize, heap buffer overflow occurs.
  • Fix: Use max(faceCharaSize, jsonArray.size()) for allocation, add null check and size validation.

CWE-312: Sensitive Data in Plaintext (design-level, deferred)

  • Problem: Face feature templates are serialized as plaintext JSON and passed to Uadp1 service for storage.
  • Note: This is a design-level issue requiring architectural changes (encryption at rest / secure storage migration). Not fixed in this PR.

Summary by Sourcery

Harden character-data loading against invalid sizes, allocation failures, and out-of-bounds writes.

Bug Fixes:

  • Prevent heap buffer overflows when loading face-character data whose JSON array exceeds the declared size.
  • Skip invalid or failed face-data allocations instead of storing unusable entries.

Influence: use max(faceCharaSize, jsonArray.size()) for allocation to prevent out-of-bounds write when JSON array is larger than declared size
@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: fly602

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

@sourcery-ai

sourcery-ai Bot commented Nov 2, 2026

Copy link
Copy Markdown
Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

The fix hardens CharaDataManger::loadCharaData against CWE-787 by allocating enough space for the larger of the declared and serialized face-data sizes, bounding writes, and adding validation and allocation-failure handling. The CWE-312 plaintext-storage concern is explicitly deferred and remains unresolved.

Flow diagram for safe face data loading

flowchart TD
    A[loadCharaData] --> B["Read faceSize and jsonArray.size()"]
    B --> C[Compute allocSize with qMax]
    C --> D{allocSize <= 0}
    D -->|Yes| E[Skip character]
    D -->|No| F[malloc allocSize floats]
    F --> G{faceChara == nullptr}
    G -->|Yes| H[Skip character]
    G -->|No| I[Write bounded jsonArray values]
    I --> J[Store allocSize and faceChara]
Loading

File-Level Changes

Change Details Files
Prevents heap buffer overflow by sizing face-feature storage to accommodate both the declared size and the JSON payload.
  • Computes allocation length as the maximum of the configured face size and JSON array length.
  • Stores the effective allocation length alongside the allocated buffer.
  • Bounds the population loop by both the JSON length and allocation length.
charadatamanger.cpp
Adds defensive handling for invalid input sizes and allocation failures.
  • Skips entries when the effective size is non-positive.
  • Checks malloc results and skips entries when allocation fails.
  • Emits warnings for invalid sizes and failed allocations.
charadatamanger.cpp

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 found 1 issue

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="charadatamanger.cpp" line_range="88" />
<code_context>
             faceChara[i] = jsonArray[i].toString().toFloat();
         }
-        m_charaData[chara] = QPair<int, float *>(faceSize, faceChara);
+        m_charaData[chara] = QPair<int, float *>(allocSize, faceChara);
     }
 }
</code_context>
<issue_to_address>
**Uninitialized features corrupt templates**

When a stored entry has a positive `faceCharaSize` greater than its `faceChara` array length, `loadCharaData()` allocates `faceSize` elements but initializes only the array entries, then records `allocSize` as the template length. `saveCharaData()` serializes the uninitialized tail, and `getCharaData()` copies it for verification, corrupting templates and producing unreliable recognition results.

Initialize every allocated element before recording `allocSize` as the template length.
</issue_to_address>

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

Comment thread charadatamanger.cpp
faceChara[i] = jsonArray[i].toString().toFloat();
}
m_charaData[chara] = QPair<int, float *>(faceSize, faceChara);
m_charaData[chara] = QPair<int, float *>(allocSize, faceChara);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Medium · Uninitialized features corrupt templates

When a stored entry has a positive faceCharaSize greater than its faceChara array length, loadCharaData() allocates faceSize elements but initializes only the array entries, then records allocSize as the template length. saveCharaData() serializes the uninitialized tail, and getCharaData() copies it for verification, corrupting templates and producing unreliable recognition results.

Initialize every allocated element before recording allocSize as the template length.

Prompt for AI agents
In `charadatamanger.cpp` at line 88:

**Uninitialized features corrupt templates**

When a stored entry has a positive `faceCharaSize` greater than its `faceChara` array length, `loadCharaData()` allocates `faceSize` elements but initializes only the array entries, then records `allocSize` as the template length. `saveCharaData()` serializes the uninitialized tail, and `getCharaData()` copies it for verification, corrupting templates and producing unreliable recognition results.

Initialize every allocated element before recording `allocSize` as the template length.

@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

🤖 AI 代码审查报告

总体评分: 98 分 (通过阈值: 70分)

Pass


📊 总体评价

项目 结果
审查结论 代码审查通过
评分详情 本次提交修复了 CharaDataManger::loadCharaData 函数中的 CWE-787 堆缓冲区溢出漏洞,通过使用 qMax 确保分配足够内存、增加 allocSize 有效性检查、malloc 返回值空指针检查以及循环边界双重保护,正确消除了原始漏洞且未引入新漏洞。OCR 发现一处预存的未初始化内存问题(建议使用 calloc 替代 malloc),属于改进建议而非新引入漏洞,扣减代码质量 2 分。

🔍 详细分析

1. 语法逻辑 ✅

评价: 语法正确,逻辑清晰 ✅ 通过

潜在问题:
✅ 未发现明显问题

建议: []


2. 代码质量 ✅

评价: 代码结构清晰,注释完整 ✅ 通过

潜在问题:
✅ 未发现明显问题

建议: ['建议将 malloc 替换为 calloc,或在使用 malloc 后通过 memset 显式清零未写入部分,确保所有分配的内存都被正确初始化']


3. 代码性能 ✅

评价: 性能良好,资源使用合理 ✅ 通过

潜在问题:
✅ 未发现明显问题

建议: []


4. 代码安全 🔒

评价: 存在0个安全漏洞 ✅ 通过

🔐 发现 0 个安全漏洞

安全漏洞详情:
✅ 未发现安全漏洞

建议: ['建议使用 calloc 替代 malloc 以零初始化缓冲区,进一步消除未初始化内存读取的潜在风险']


💡 改进建议代码示例

// 推荐使用 calloc 替代 malloc,确保缓冲区零初始化
int allocSize = qMax(faceSize, jsonArray.size());
if (allocSize <= 0) {
    qWarning() << "Invalid face data size, skipping:" << chara;
    continue;
}
float *faceChara = static_cast<float *>(
                       calloc(static_cast<unsigned long>(allocSize), sizeof(float)));
if (faceChara == nullptr) {
    qWarning() << "Failed to allocate face data, skipping:" << chara;
    continue;
}
for (int i = 0; i < jsonArray.size() && i < allocSize; i++) {
    faceChara[i] = jsonArray[i].toString().toFloat();
}
m_charaData[chara] = QPair<int, float *>(allocSize, faceChara);

本报告由 AI 代码审查工具自动生成

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.

2 participants