Skip to content

tools: harden attachconfig handling#11555

Open
yanhu7150-tech wants to merge 1 commit into
RT-Thread:masterfrom
yanhu7150-tech:bugfix-attachconfig-validation
Open

tools: harden attachconfig handling#11555
yanhu7150-tech wants to merge 1 commit into
RT-Thread:masterfrom
yanhu7150-tech:bugfix-attachconfig-validation

Conversation

@yanhu7150-tech

Copy link
Copy Markdown

拉取/合并请求描述:(PR description)

为什么提交这份PR (why to submit this PR)

本 PR 修复 RT-Thread Python 构建工具和 CI 脚本中 attachconfig 处理流程的若干健壮性问题。

修复前,在一些正常的 attachconfig 异常输入或缺省配置场景下,脚本可能直接抛出 Python 异常,或者在 BSP 目录中残留临时备份文件,影响后续构建流程的稳定性。具体问题包括:

  1. tools/ci/bsp_buildings.py 中直接使用 os.getenv('SRTT_BSP').split(',') 解析环境变量。当 SRTT_BSP 未设置或为空时,脚本会触发 AttributeError,并输出 Python traceback,而不是给出明确的 CI 配置错误提示。

  2. tools/attachconfig.py 在执行 attachconfig 查询模式时,会直接访问 attachconfig[0]。当当前 BSP 没有任何 attachconfig 配置项时,会触发 IndexError: list index out of range

  3. 在 YAML attachconfig 构建流程中,如果某个 project 缺少 kconfig 字段,旧逻辑可能已经创建 .config.origin 备份文件后直接 continue,导致 .config.origin 残留在 BSP 目录中,并跳过正常的 .config 恢复流程。

  4. 一些 YAML 可选命令字段,例如 pre_buildbuild_cmdpost_buildmsh_cmd,旧代码在未检查字段是否存在的情况下直接调用 .splitlines()。当这些可选字段缺失时,可能触发非预期异常。

以上问题属于普通构建工具健壮性缺陷,不涉及安全漏洞。

你的解决方案是什么 (what is your solution)

本 PR 对 attachconfig 处理流程进行了以下改进:

  1. 增加逗号分隔环境变量的安全解析逻辑:

    • 在使用 SRTT_BSP 前先检查环境变量是否存在且非空;
    • SRTT_BSP 缺失或为空时,输出明确错误信息:::error::SRTT_BSP is not set or empty
    • 对逗号分隔后的 BSP 名称执行 strip,并过滤空字符串;
    • 保持 ATTACHCONFIG_RTT_BSP 对 attachconfig BSP 过滤的原有行为。
  2. 修复 attachconfig 查询结果为空时的异常:

    • scons --attach=? 没有找到任何 attachconfig 项时,输出 No AttachConfig found.
    • 正常返回,不再访问空列表;
    • 避免 IndexError
  3. 增强 YAML 可选命令字段处理:

    • 当字段缺失时,不再对 None 调用 .splitlines()
    • 字符串字段仍保持按行拆分的原有行为;
    • 对 list 或 tuple 类型字段,直接转换为命令列表处理。
  4. 改进 .config 备份和恢复流程:

    • 对缺少 kconfig 的 YAML project,在创建 .config.origin 前直接跳过;
    • 对已经创建 .config.origin 的构建流程使用 try/finally 保护;
    • 确保构建流程结束后总是恢复 .config,并删除 .config.origin
    • 避免异常或跳过路径导致 BSP 目录残留临时备份文件。
  5. 新增回归测试,覆盖以下场景:

    • attachconfig 查询结果为空;
    • SRTT_BSP 未设置;
    • YAML project 缺少 kconfig
    • bsp_board_info 缺少可选命令字段。

请提供验证的bsp和config (provide the config and bsp)

  • BSP: N/A

本 PR 只修改 Python 构建/CI 辅助脚本,并新增对应回归测试,不涉及具体 BSP 源码修改。

  • .config: N/A

本 PR 不需要修改任何 BSP .config 选项。

  • action: N/A

本 PR 已在本地完成 Python 语法检查和回归测试。提交 PR 后,可继续以 GitHub Actions 的结果为准。

本地验证命令如下:

python -m py_compile tools\attachconfig.py tools\ci\bsp_buildings.py tools\testcases\test_attachconfig_robustness.py
python tools\testcases\test_attachconfig_robustness.py
python -m unittest discover -s tools\testcases -p "test_attachconfig_robustness.py"
git diff --check

本地测试结果如下:

Ran 4 tests

OK

]

当前拉取/合并请求的状态 Intent for your PR

必须选择一项 Choose one (Mandatory):

  • 本拉取/合并请求是一个草稿版本 This PR is for a code-review and is intended to get feedback
  • 本拉取/合并请求是一个成熟版本 This PR is mature, and ready to be integrated into the repo

代码质量 Code Quality:

我在这个拉取/合并请求中已经考虑了 As part of this pull request, I've considered the following:

@github-actions

github-actions Bot commented Jul 4, 2026

Copy link
Copy Markdown

👋 感谢您对 RT-Thread 的贡献!Thank you for your contribution to RT-Thread!

为确保代码符合 RT-Thread 的编码规范,请在你的仓库中执行以下步骤运行代码格式化工作流(如果格式化CI运行失败)。
To ensure your code complies with RT-Thread's coding style, please run the code formatting workflow by following the steps below (If the formatting of CI fails to run).


🛠 操作步骤 | Steps

  1. 前往 Actions 页面 | Go to the Actions page
    点击进入工作流 → | Click to open workflow →

  2. 点击 Run workflow | Click Run workflow

  • 设置需排除的文件/目录(目录请以"/"结尾)
    Set files/directories to exclude (directories should end with "/")
  • 将目标分支设置为 \ Set the target branch to:bugfix-attachconfig-validation
  • 设置PR number为 \ Set the PR number to:11555
  1. 等待工作流完成 | Wait for the workflow to complete
    格式化后的代码将自动推送至你的分支。
    The formatted code will be automatically pushed to your branch.

完成后,提交将自动更新至 bugfix-attachconfig-validation 分支,关联的 Pull Request 也会同步更新。
Once completed, commits will be pushed to the bugfix-attachconfig-validation branch automatically, and the related Pull Request will be updated.

如有问题欢迎联系我们,再次感谢您的贡献!💐
If you have any questions, feel free to reach out. Thanks again for your contribution!

@github-actions github-actions Bot added action github action yml imporve tools labels Jul 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

action github action yml imporve tools

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant