Skip to content

[libc] fix: validate settimeofday fields by their own type - #11928

Open
Yunlk wants to merge 1 commit into
RT-Thread:masterfrom
Yunlk:fix/settimeofday-time64
Open

Yunlk wants to merge 1 commit into
RT-Thread:masterfrom
Yunlk:fix/settimeofday-time64

Conversation

@Yunlk

@Yunlk Yunlk commented Oct 24, 2026

Copy link
Copy Markdown
Contributor

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

[

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

修复 #11922。

settimeofday() 用强转后的值判断参数是否合法:

if (tv != RT_NULL && (long)tv->tv_usec >= 0 && (long)tv->tv_sec >= 0)

在 ILP32 且 time_t 为 64 位的目标上(time64/long32,例如 32 位 ARM + RT-Thread 默认 libc 配置),这个 (long) 会把 tv_sec 截成 32 位。于是 INT32_MAX + 1 到 UINT32_MAX 区间的每个合法时间戳都变成负数,函数在碰到 RTC 设备之前就以 EINVAL 拒绝。2208988800(2040-01-01)就是这样被拒的。

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

按字段自身类型校验,不做强转:

-    if (tv != RT_NULL && (long)tv->tv_usec >= 0 && (long)tv->tv_sec >= 0)
+    if (tv != RT_NULL && tv->tv_usec >= 0 && tv->tv_sec >= 0)

tv_sec 直接按 time_t 比较,RT-Thread 各配置下 time_t 都是有符号类型,所以 >= 0 的判断依然有效,只是不再窄化。

tv_usec 的强转也一并去掉,只是为了两处写法统一;它在 ILP32 上原本就没问题(suseconds_t 32 位)。如果希望改动更小,只动 tv_sec 也可以。

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

  • BSP: bsp/qemu-vexpress-a9(32 位 ARM,RT_USING_RTC=y,板上有 rtc 设备)
  • .config: BSP 默认配置,未改动
  • action: 未使用 GitHub Actions;本地 QEMU 11.0.0 + arm-none-eabi-gcc 15.2.1,master 1c801efa

运行时探针确认 sizeof(time_t)=8、sizeof(long)=4,正是 time64/long32。

逐点扫描边界,同一份固件改前/改后各跑一次:

tv_sec 对应时间 修改前 修改后
0 1970-01-01 ret=0 ret=0
2147483647 2038-01-19 ret=0 ret=0
2147483648 2038+1 ret=-1, errno=EINVAL ret=0
2208988800 2040-01-01 ret=-1, errno=EINVAL ret=0
4294967295 2106-02-07 ret=-1, errno=EINVAL ret=0

首个被拒值正好是 INT32_MAX + 1,与 issue 描述的区间一致;INT32_MAX 及以下行为完全未变。

两点说明

  1. 未包含的相邻缺陷:当前只校验 tv_usec >= 0,POSIX 还要求 < 1000000。传 tv_usec = 5000000 会被放行并送进 RTC 驱动。这属于另一处校验缺失,为了避免把两种行为变更混在一个补丁里,本次没有改动,是否收紧由维护者判断。

  2. 关于 utest:这个用例要能跑,前提是系统里有 rtc 设备(_control_rtc() 通过 rt_device_find("rtc") 查找,找不到会返回 -RT_ENOSYS,这与强转问题是两条不同的失败路径)。CI 里的 qemu-vexpress-a9 有 rtc,但其他跑 utest 的 BSP 未必有,直接加会因找不到设备而误失败。如果维护者确认放在哪个 BSP/config 合适,我可以补一条 RT_USING_RTC 条件下编译的用例,断言 INT32_MAX + 1 与 2208988800 能通过。

]

当前拉取/合并请求的状态 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
  • 已经使用clang-format 源码格式化工具确保格式符合RT-Thread代码规范 This PR has been formatted with clang-format and complies with RT-Thread code specification
  • 如果是新增bsp, 已经添加ci检查到.github/ALL_BSP_COMPILE.json 详细请参考链接BSP自查

settimeofday() decided whether the caller's arguments were valid by casting
them to long first:

    if (tv != RT_NULL && (long)tv->tv_usec >= 0 && (long)tv->tv_sec >= 0)

On ILP32 targets where time_t is 64 bit (time64/long32, e.g. 32-bit ARM with
the default RT-Thread libc configuration) that cast narrows tv_sec to 32 bits.
Every timestamp from INT32_MAX + 1 to UINT32_MAX becomes negative, so the
function rejects perfectly valid values with EINVAL before it ever reaches the
RTC device. 2208988800 (2040-01-01) is rejected this way.

Validate the fields by their own type instead; tv_sec is compared as time_t,
which is signed in every configuration RT-Thread ships.

Verified on bsp/qemu-vexpress-a9 (32-bit ARM, RT_USING_RTC, on-board rtc
device, arm-none-eabi-gcc 15.2.1, QEMU 11.0.0), where the probe reports
sizeof(time_t) = 8 and sizeof(long) = 4:

  tv_sec        before                                  after
  0             ret=0                                   ret=0
  2147483647    ret=0                                   ret=0
  2147483648    ret=-1, errno=EINVAL                    ret=0
  2208988800    ret=-1, errno=EINVAL                    ret=0
  4294967295    ret=-1, errno=EINVAL                    ret=0

Values at or below INT32_MAX behave exactly as before.

Note: tv_usec is only checked for a negative value here, POSIX also requires
it to be below 1000000. That is a separate gap and is left untouched.

Refs RT-Thread#11922
@Yunlk
Yunlk requested a review from mysterywolf as a code owner October 24, 2026 17:13
@github-actions

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

  • Use workflow from 保持默认分支(通常为 master)
    Keep the default branch (usually master) in Use workflow from
  • 在 branch 输入框填写 PR 分支 fix/settimeofday-time64
    Enter PR branch fix/settimeofday-time64 in the branch field
  • 设置需排除的文件/目录(目录请以"/"结尾)
    Set files/directories to exclude (directories should end with "/")
  1. 等待工作流完成 | Wait for the workflow to complete
    格式化后的代码将作为独立提交推送至你的分支。
    The formatting changes will be pushed to your branch as a separate commit.

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

📌 Code Review Assignment

🏷️ Tag: components

Reviewers: @Maihuanyi

Changed Files (Click to expand)
  • components/libc/compilers/common/ctime.c

🏷️ Tag: components_libc

Reviewers: @GorrayLi @mysterywolf

Changed Files (Click to expand)
  • components/libc/compilers/common/ctime.c

📊 Current Review Status (Last Updated: 2026-10-25 01:14 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.

@github-actions

Copy link
Copy Markdown

This branch has not been deployed

No deployments
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.

1 participant