Skip to content

Conversation

@loremmoqi
Copy link
Contributor

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

[

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

CLA没有通过,重新提交PR
对于下面的问题添加了详细的代码注释。
当前 RT-Thread 在使用 newlib 的部分版本时,会出现以下问题:
_GLOBAL_REENT 结构中,旧版本使用的 __sdidinit 字段已被移除;

  • 代码中仍然直接访问 _GLOBAL_REENT->__sdidinit,导致在编译 newlib 时出错;
  • 不同版本的 newlib(如 3.3.x / 3.4.x / 4.0.0 / 4.3.0 / 12.x)在 reent 结构中存在兼容性差异。

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

components/libc/posix/io/stdio.c/stdio.c 中增加更合理的版本判断宏:
#if defined(_REENT_SMALL) || !defined(__sdidinit)
// Newlib >= 3.4.0 or small reent version: no __sdidinit field
// Do nothing
#else
// Older newlib versions (typically < 3.4.0) still define __sdidinit
_GLOBAL_REENT->__sdidinit = 1;
#endif

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

  • BSP:bsp/qemu-vexpress-a9
  • .config:无需修改 .config 文件,仅涉及代码逻辑的条件编译适配

]

当前拉取/合并请求的状态 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:

  • 已经仔细查看过代码改动的对比 Already check the difference between PR and old code
  • 代码风格正确,包括缩进空格,命名及其他风格 Style guide is adhered to, including spacing, naming and other styles
  • 没有垃圾代码,代码尽量精简,不包含#if 0代码,不包含已经被注释了的代码 All redundant code is removed and cleaned up
  • 所有变更均有原因及合理的,并且不会影响到其他软件组件代码或BSP All modifications are justified and not affect other components or BSP
  • 对难懂代码均提供对应的注释 I've commented appropriately where code is tricky
  • 代码是高质量的 Code in this PR is of high quality
  • 已经使用formatting 等源码格式化工具确保格式符合RT-Thread代码规范 This PR complies with RT-Thread code specification
  • 如果是新增bsp, 已经添加ci检查到.github/ALL_BSP_COMPILE.json 详细请参考链接BSP自查

@github-actions
Copy link

👋 感谢您对 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:fix-stdio-newlib-v2
  • 设置PR number为 \ Set the PR number to:10789
  1. 等待工作流完成 | Wait for the workflow to complete
    格式化后的代码将自动推送至你的分支。
    The formatted code will be automatically pushed to your branch.

完成后,提交将自动更新至 fix-stdio-newlib-v2 分支,关联的 Pull Request 也会同步更新。
Once completed, commits will be pushed to the fix-stdio-newlib-v2 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
Copy link

📌 Code Review Assignment

🏷️ Tag: components

Reviewers: @Maihuanyi

Changed Files (Click to expand)
  • components/libc/posix/io/stdio/stdio.c

🏷️ Tag: components_libc

Reviewers: @GorrayLi @mysterywolf

Changed Files (Click to expand)
  • components/libc/posix/io/stdio/stdio.c

📊 Current Review Status (Last Updated: 2025-10-11 14:28 CST)


📝 Review Instructions

  1. 维护者可以通过单击此处来刷新审查状态: 🔄 刷新状态
    Maintainers can refresh the review status by clicking here: 🔄 Refresh Status

  2. 确认审核通过后评论 LGTM/lgtm
    Comment LGTM/lgtm after confirming approval

  3. PR合并前需至少一位维护者确认
    PR must be confirmed by at least one maintainer before merging

ℹ️ 刷新CI状态操作需要具备仓库写入权限。
ℹ️ Refresh CI status operation requires repository Write permission.

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
1 out of 2 committers have signed the CLA.

✅ loremmoqi
❌ github-actions[bot]
You have signed the CLA already but the status is still pending? Let us recheck it.

@BernardXiong
Copy link
Member

AI给出的信息

你分析得完全正确:
不同版本的 newlib 对 _GLOBAL_REENT->__sdidinit 的定义不同,3.4.0 及以后部分版本(特别是 small reent)已经移除了该成员。直接访问会导致编译错误或未定义行为,因此需要根据实际 newlib 版本和编译选项做兼容。

更可靠的跨版本兼容方案

目前的写法:

#if defined(_REENT_SMALL) || !defined(__sdidinit)
// do nothing
#else
    _GLOBAL_REENT->__sdidinit = 1;
#endif

这种判断虽然可以解决部分情况,但通常 newlib 官方建议用更严谨的宏判断。实际上,newlib 从 3.4.0 开始逐步移除了 __sdidinit,而且有些定制版本可能行为不同。

推荐做法

  1. 判断 NEWLIB_VERSION_NUM 是否小于 3.4.0(即 30400U),只有在旧版时才设置该字段。
  2. 再加一层保护:仅当 _GLOBAL_REENT 结构体内存在 __sdidinit 成员时才访问,防止自定义 newlib 版本导致的潜在问题。

可选:用结构体成员检测宏(如 offsetof 或手动宏定义)进一步防护。

代码实现与注释(英文)

#include <stdio.h>
#include <reent.h>
#include <fcntl.h>
#include <rtthread.h>

/**
 * Initialize the standard IO streams for RT-Thread when using Newlib.
 * This function sets up the global reentrancy structure's standard streams,
 * and configures legacy fields for compatibility with different Newlib versions.
 *
 * Compatibility notes:
 * - In Newlib versions prior to 3.4.0, the __sdidinit field exists and needs to be set.
 * - In Newlib 3.4.0 and later, or when using the "small reent" configuration, __sdidinit is removed.
 * - Accessing __sdidinit when it does not exist causes build errors.
 * - This code uses version checks and macro guards to ensure safe access.
 */
void rt_posix_stdio_init(void)
{
    FILE *std_console = NULL;
    int std_fd = -1;

    /* (Other initialization code omitted for brevity) */

    _GLOBAL_REENT->_stdin = std_console;
    _GLOBAL_REENT->_stdout = std_console;
    _GLOBAL_REENT->_stderr = std_console;

    /*
     * For Newlib versions < 3.4.0, set the legacy __sdidinit field.
     * This field is obsolete and removed in newer versions or in small reent mode.
     * The macro NEWLIB_VERSION_NUM is provided by Newlib headers.
     */
#if defined(NEWLIB_VERSION_NUM) && (NEWLIB_VERSION_NUM < 30400U)
    /* __sdidinit is present, set to 1 to indicate stdio streams are initialized */
    _GLOBAL_REENT->__sdidinit = 1;
#endif
    /*
     * For custom or unknown Newlib versions, if __sdidinit is defined in _reent,
     * you may add extra macro checks here for higher safety.
     */
}

补充说明

  • 若你的 toolchain 没有定义 NEWLIB_VERSION_NUM,可通过 CMake 或 makefile 手动传递宏定义,或者用 #ifdef 保护。
  • 如果担心极端情况下 NEWLIB_VERSION_NUM 也没有、但 __sdidinit 还在,可以用如下宏补充:
#if (!defined(NEWLIB_VERSION_NUM) && defined(__sdidinit)) || \
     (defined(NEWLIB_VERSION_NUM) && (NEWLIB_VERSION_NUM < 30400U))
    _GLOBAL_REENT->__sdidinit = 1;
#endif

英文注释重点

  • Clearly state which Newlib versions require setting __sdidinit.
  • Explain why the check is necessary (struct member may not exist).
  • Mention that directly accessing __sdidinit in newer Newlib versions will cause build errors.
  • Note that the macro NEWLIB_VERSION_NUM is key for version detection.

Summary:
Use version macros to guard the access of _GLOBAL_REENT->__sdidinit, and clearly annotate the reason for compatibility. This ensures smooth compilation and execution regardless of Newlib version in RT-Thread’s POSIX layer.

@loremmoqi loremmoqi closed this by deleting the head repository Oct 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants