Skip to content

fix: fix network check, dpkg lock flow, memory leak and uri term - #948

Open
add-uos wants to merge 2 commits into
linuxdeepin:masterfrom
add-uos:fix/bug-6224
Open

add-uos wants to merge 2 commits into
linuxdeepin:masterfrom
add-uos:fix/bug-6224

Conversation

@add-uos

@add-uos add-uos commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

fix: fix network check, dpkg lock flow, memory leak and uri term

  1. Root cause: DriverInstallerApt::isNetworkOnline used != 0 on packet
    loss rate, treating packet loss > 0 as online (inverted logic)
  2. Fix: change comparison to == 0 so 0% loss means online, matching
    DriverInstaller::isNetworkOnline semantics
  3. Root cause: DriverInstaller/DebInstaller::installPackage missing
    return after emit finished(false) in dpkg lock timeout branch,
    control flow falls through to doOperate() starting real install
  4. Fix: add return statement after emit in both installPackage methods
  5. Root cause: enablePrinter early-return paths (cupsServer null,
    httpConnect2 failed) did not call ippDelete(request), leaking ipp_t
  6. Fix: add ippDelete(request) before each early return false
  7. Root cause: construct_uri loop condition allowed d to reach
    buffer+buflen, NUL termination check failed when buffer exactly full
  8. Fix: reserve 1 byte for NUL by changing loop to d < buffer+buflen-1
    and percent-encoding check to d+3 < buffer+buflen

Log: fix network online detection and dpkg lock timeout handling

Influence:

  1. Verify network online/offline detection with various packet loss rates
  2. Test dpkg lock timeout scenario does not trigger real installation
  3. Test enablePrinter/disablePrinter with CUPS server unavailable
  4. Test printer URI construction with long names and special characters

fix: 修复网络检查逻辑反转、dpkg锁控制流穿透、内存泄漏和URI终止问题

  1. 根因:DriverInstallerApt::isNetworkOnline 对丢包率使用 != 0,丢包率>0
    时判定为在线,逻辑与预期相反
  2. 方案:将比较改为 == 0,丢包率为0时返回在线,与 DriverInstaller 一致
  3. 根因:DriverInstaller/DebInstaller::installPackage 在 dpkg 锁超时分支
    emit finished(false) 后缺少 return,控制流穿透到 doOperate 启动安装
  4. 方案:在两个 installPackage 的 emit 后添加 return 语句
  5. 根因:enablePrinter 早退路径(cupsServer 空/httpConnect2 失败)未调用
    ippDelete(request),每次调用泄漏 ipp_t 结构
  6. 方案:在每条早退 return false 前添加 ippDelete(request)
  7. 根因:construct_uri 循环条件允许 d 到达 buffer+buflen,缓冲区填满时
    NUL 终止检查失败,返回未终止字符串
  8. 方案:循环条件改为 d < buffer+buflen-1 保留 NUL 空间,百分号编码
    检查改为 d+3 < buffer+buflen

Log: 修复网络在线检测和dpkg锁超时处理问题

Influence:

  1. 验证不同丢包率下网络在线/离线检测是否正确
  2. 测试 dpkg 锁超时场景不会触发真实安装
  3. 测试 CUPS 服务器不可用时 enablePrinter/disablePrinter 无泄漏
  4. 测试长打印机名和特殊字符的 URI 构造是否正确终止

Summary by Sourcery

Fix network status detection, package-installation timeout handling, printer resource cleanup, and URI buffer termination.

Bug Fixes:

  • Correct network availability detection so only zero packet loss is considered online.
  • Prevent package installation from continuing after dpkg lock timeouts.
  • Release printer request resources when CUPS setup or connection fails.
  • Ensure constructed printer URIs remain NUL-terminated within the destination buffer.

@sourcery-ai

sourcery-ai Bot commented Oct 26, 2026

Copy link
Copy Markdown

Sorry @add-uos, you've used your own review budget of 250,000 diff characters for the last 7 days.

You can request another review in 1 day and 12 hours by commenting @sourcery-ai review. Upgrade to get a review now.

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: add-uos

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 Oct 26, 2026

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

Reviewer's Guide

This PR fixes four edge cases by aligning packet-loss interpretation with expected online semantics, terminating installer control flow after dpkg-lock timeouts, cleaning up IPP requests on CUPS connection failures, and bounding URI encoding to preserve NUL termination. Review should focus on packet-loss behavior, ensuring timeout failures never invoke the real install operation, validating cleanup across printer failure paths, and testing URI construction at buffer limits with special characters.

Sequence diagram for dpkg lock timeout handling

sequenceDiagram
    participant Caller
    participant Installer as DriverInstaller_or_DebInstaller
    participant Dpkg
    participant InstallOperation

    Caller->>Installer: installPackage(package)
    Installer->>Dpkg: check lock
    alt dpkg lock times out
        Dpkg-->>Installer: timeout
        Installer-->>Caller: installFinished(false)
        Note over Installer,InstallOperation: return prevents doOperate()
    else lock available
        Dpkg-->>Installer: available
        Installer->>InstallOperation: doOperate()
    end
Loading

Sequence diagram for printer enable failure cleanup

sequenceDiagram
    participant Caller
    participant ControlInterface
    participant CUPS
    participant IPP

    Caller->>ControlInterface: enablePrinter(hclass, name)
    ControlInterface->>IPP: ippAddString(request, ...)
    ControlInterface->>CUPS: cupsServer()
    alt CUPS server unavailable
        CUPS-->>ControlInterface: null
        ControlInterface->>IPP: ippDelete(request)
        ControlInterface-->>Caller: false
    else HTTP connection fails
        ControlInterface->>CUPS: httpConnect2(host, port, ...)
        CUPS-->>ControlInterface: null
        ControlInterface->>IPP: ippDelete(request)
        ControlInterface-->>Caller: false
    end
Loading

Flow diagram for network online detection

flowchart TD
    A[Measure packet loss] --> B{received packet loss is 0%?}
    B -->|Yes| C[Return online]
    B -->|No| D[Return offline]
    D --> E[Do not start online-dependent operation]
Loading

File-Level Changes

Change Details Files
Correct network availability detection to treat zero packet loss as online.
  • Invert the packet-loss comparison in the APT installer to match the shared network-check semantics.
  • Continue reporting offline when the network probe cannot produce a valid result.
deepin-devicemanager-server/deepin-devicecontrol/src/drivercontrol/driverinstallerapt.cpp
Stop package installation immediately when the dpkg lock cannot be acquired before timeout.
  • Return after emitting the failure signals in both installer implementations.
  • Prevent the normal installation path from being entered after a lock-timeout failure.
deepin-devicemanager-server/deepin-devicecontrol/src/drivercontrol/debinstaller.cpp
deepin-devicemanager-server/deepin-devicecontrol/src/drivercontrol/driverinstaller.cpp
Release IPP requests on printer-enabling failure paths and guarantee URI buffer termination.
  • Delete the allocated IPP request when the CUPS server is unavailable or the HTTP connection fails.
  • Reserve space for the NUL terminator during URI encoding and require sufficient capacity for percent-encoded bytes.
deepin-devicemanager-server/deepin-devicecontrol/src/controlinterface.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

@deepin-ci-robot

Copy link
Copy Markdown

deepin pr auto review

🤖 AI 代码审查报告

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

Pass


📊 总体评价

项目 结果
审查结论 代码审查通过(良好)
评分详情 本次提交修复了内存泄漏、缓冲区溢出、dpkg锁流程控制流和网络检查逻辑四类问题,大部分修复正确且针对性强的最小化改动。但dpkg锁超时的控制流修复不完整——installPackage已添加return语句,uninstallPackage中存在完全相同的fall-through缺陷未修复。此外construct_uri中buflen为0时存在unsigned underflow的防御性编程隐患。综合评分86分,审查通过。

🔍 详细分析

1. 语法逻辑 ❌

评价: 存在逻辑缺陷 ❌ 不通过

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

建议: ['在debinstaller.cpp的uninstallPackage函数中,emit installFinished(false)之后添加return语句,与installPackage的修复保持一致']


2. 代码质量 ✅

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

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

建议: ['对installPackage和uninstallPackage中相同的dpkg锁超时模式进行统一修复,确保两处都添加return语句']


3. 代码性能 ✅

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

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

建议: []


4. 代码安全 🔒

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

🔐 发现 0 个安全漏洞

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

建议: ['在construct_uri函数开头添加 if (buflen == 0) return; 的前置检查,防止无符号整数下溢']


💡 改进建议代码示例

// 修复1: debinstaller.cpp - uninstallPackage函数添加return语句
void DebInstaller::uninstallPackage(const QString &packagename)
{
    qCDebug(appLog) << "Uninstalling package:" << packagename;
    if (Utils::isFileLocked("/var/lib/dpkg/lock")) {
        if (m_iRuningTestCount < MAX_DPKGRUNING_TEST) {
            QTimer::singleShot(TEST_TIME_INTERVAL, this, [&] {
                uninstallPackage(packagename);
                m_iRuningTestCount++;
            });
            return;
        } else {
            emit errorOccurred("Dpkg locked ,time out");
            emit installFinished(false);
            return;  // 添加return,防止fall-through到doOperate
        }
    }
    doOperate(packagename, false);
}

// 修复2: controlinterface.cpp - construct_uri添加buflen==0防御性检查
void ControlInterface::construct_uri(char *buffer, size_t buflen, const char *base, ...)
{
    if (buflen == 0) return;  // 防止无符号整数下溢
    // ... 原有逻辑
}

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

1. Root cause: DriverInstallerApt::isNetworkOnline used != 0 on packet
   loss rate, treating packet loss > 0 as online (inverted logic)
2. Fix: change comparison to == 0 so 0% loss means online, matching
   DriverInstaller::isNetworkOnline semantics
3. Root cause: DriverInstaller/DebInstaller::installPackage missing
   return after emit finished(false) in dpkg lock timeout branch,
   control flow falls through to doOperate() starting real install
4. Fix: add return statement after emit in both installPackage methods
5. Root cause: enablePrinter early-return paths (cupsServer null,
   httpConnect2 failed) did not call ippDelete(request), leaking ipp_t
6. Fix: add ippDelete(request) before each early return false
7. Root cause: construct_uri loop condition allowed d to reach
   buffer+buflen, NUL termination check failed when buffer exactly full
8. Fix: reserve 1 byte for NUL by changing loop to d < buffer+buflen-1
   and percent-encoding check to d+3 < buffer+buflen

Log: fix network online detection and dpkg lock timeout handling

Influence:
1. Verify network online/offline detection with various packet loss rates
2. Test dpkg lock timeout scenario does not trigger real installation
3. Test enablePrinter/disablePrinter with CUPS server unavailable
4. Test printer URI construction with long names and special characters

fix: 修复网络检查逻辑反转、dpkg锁控制流穿透、内存泄漏和URI终止问题

1. 根因:DriverInstallerApt::isNetworkOnline 对丢包率使用 != 0,丢包率>0
   时判定为在线,逻辑与预期相反
2. 方案:将比较改为 == 0,丢包率为0时返回在线,与 DriverInstaller 一致
3. 根因:DriverInstaller/DebInstaller::installPackage 在 dpkg 锁超时分支
   emit finished(false) 后缺少 return,控制流穿透到 doOperate 启动安装
4. 方案:在两个 installPackage 的 emit 后添加 return 语句
5. 根因:enablePrinter 早退路径(cupsServer 空/httpConnect2 失败)未调用
   ippDelete(request),每次调用泄漏 ipp_t 结构
6. 方案:在每条早退 return false 前添加 ippDelete(request)
7. 根因:construct_uri 循环条件允许 d 到达 buffer+buflen,缓冲区填满时
   NUL 终止检查失败,返回未终止字符串
8. 方案:循环条件改为 d < buffer+buflen-1 保留 NUL 空间,百分号编码
   检查改为 d+3 < buffer+buflen

Log: 修复网络在线检测和dpkg锁超时处理问题

Influence:
1. 验证不同丢包率下网络在线/离线检测是否正确
2. 测试 dpkg 锁超时场景不会触发真实安装
3. 测试 CUPS 服务器不可用时 enablePrinter/disablePrinter 无泄漏
4. 测试长打印机名和特殊字符的 URI 构造是否正确终止
The license-check CI (SPDX Header Validation) requires source files that
were created in earlier years and are still being modified to carry a
copyright year range ending with the current year. Extend the
SPDX-FileCopyrightText ranges of debinstaller.cpp, driverinstaller.cpp and
driverinstallerapt.cpp (2019 ~ 2023 / 2019 ~ 2025) so the end year covers
2026 and the SPDX summary reports 0 failures.
@deepin-bot

deepin-bot Bot commented Oct 28, 2026

Copy link
Copy Markdown
Contributor

TAG Bot

New tag: 6.0.75
DISTRIBUTION: unstable
Suggest: synchronizing this PR through rebase #956

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