[Bug] [libc] settimeofday 在 time64/long32 配置下窄化校验,错误拒绝有效时间戳
维护者通常 2 天内回复
@Yunlk 已经在做这个了。
开始于 2026年10月24日。
评估
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 新手友好度
- 72/100
- Issue 类型
- 缺陷
- 描述清晰度
- 描述清楚
- 活跃度
- 活跃
- 技术栈
- c
- 领域
- backend, embedded-iot
调研方向
Read components/libc/compilers/common/ctime.c, especially settimeofday() around lines 559–599, and inspect the field types used by its validation and the RTC control calls. Reproduce the narrowing behavior with the stated time64/long32 ARM configuration or a focused test. Done when a valid positive timestamp such as 2208988800 passes entry validation and reaches the RTC driver; hardware support determines whether the driver accepts it.
由索引模型根据 Issue 内容生成。
描述
RT-Thread Version
master, commit a59e72fb5c2f272761a2813fc9bd9fe18eb29d7d.
Affected area
libc / RTC, components/libc/compilers/common/ctime.c, settimeofday().
Hardware / Architecture / Toolchain
32 位 ARM ABI:time_t 为 64 位有符号类型,long 为 32 位。工具链为 GNU Arm Embedded Toolchain 10.3-2021.10(newlib)。已通过隔离的 ARM 编译验证类型大小及窄化转换行为;尚未验证已部署固件,不代表已经完成实机测试。
问题描述 / Describe the bug
settimeofday() 在验证时间戳时先强制转换为 long:
if (tv != RT_NULL && (long)tv->tv_usec >= 0 && (long)tv->tv_sec >= 0)
在 time64/long32 配置下,有效的正时间戳可能因此变成负数,在调用 RTC 驱动之前就返回 -1、设置 errno=EINVAL。
复现 / Reproduction
在上述 ABI 下启用 RT_USING_RTC,并提供能接受该时间的 RTC 设备:
#include <time.h>
#include <sys/time.h>
#include <errno.h>
_Static_assert(sizeof(time_t) == 8, "requires time64");
_Static_assert(sizeof(long) == 4, "requires long32");
struct timeval tv = {0};
tv.tv_sec = (time_t)2208988800LL; /* 2040-01-01 00:00:00 UTC */
tv.tv_usec = 0;
int rc = settimeofday(&tv, NULL);
预期:该有效正时间应通过入口校验,由 RTC 驱动决定是否支持设置。
源码推导结果:测试所用 ARM GCC 的 (long)2208988800LL 为 -2085978496,入口条件为假,因此两个 RTC control 操作均不会被调用,返回 -1/EINVAL。
验证边界:已隔离编译确认类型大小及强转后的判断结果,尚未在目标板上端到端执行上面的调用。超范围有符号转换的结果取决于实现;这里的数值针对已验证的 GCC 行为。并非所有 2038 年之后的时间都会被拒绝:在该行为下首个拒绝区间为秒值 2147483648..4294967295。
Suggested direction
避免为了检查符号而将 tv_sec 窄化为 long,按字段自身的类型进行范围验证。该问题不限于 picolibc,time64/long32 的 newlib 配置同样可能受影响。它与 time32 本身的表示范围限制是两个独立问题。
引入提交和版本 / Regression history
- 引入该窄化检查的提交:92859cc79e,2023-08-07,
[libc][time] silence the incorrect type warning。 - 该提交将原先直接比较字段的表达式改成
(long)强转;首个包含它的正式 release 为 v5.0.2。 - 这是条件性回归:只有字段类型与
long的宽度/表示范围不匹配并输入对应时间戳时才暴露,并非从该版本开始所有平台都不能设置时间。
其他实现参考 / Comparison
- glibc 2.42 settimeofday.c:64 位实现经
__timeval64转为__timespec64后调用时钟设置接口,不为检查符号而将秒窄化为long。 - NuttX lib_settimeofday.c:使用
ts.tv_sec = tv->tv_sec,再调用clock_settime()。
这里只比较有效正时间戳的传递方式,不主张所有 OS 的设置时间权限、负值策略或硬件支持范围相同。
- 主要语言
- C
- 星标
- 12.4k
- 派生
- 5.5k
- 平均合并
- 3 天 7 小时
- 30 天内合并 PR
- 79
环境准备
在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。
- 没有 Dockerfile 或 Docker Compose 文件
- 有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
RT-Thread/rt-thread 的其他 Issue
-
Arch: ARM/AArch64 Doc
难度 1/5 1 小时以内 新手友好度 90/100
RT-Thread/rt-thread#12082 · 1 条评论 ·
维护者通常 2 天内回复
-
bug
难度 2/5 1-3 小时 新手友好度 67/100
RT-Thread/rt-thread#11951 · 2 条评论 ·
维护者通常 2 天内回复
-
[Bug] [libc] timer_gettime 拆秒前将总纳秒截断为 uint32_t,剩余时间查询错误可能重新可做 @Yunlk 于 28 天前认领,目前没有进行中的 PR。 未关闭in progress
难度 2/5 1-3 小时 新手友好度 68/100
RT-Thread/rt-thread#11924 · 7 条评论 · 已指派 1 人 ·
维护者通常 2 天内回复
-
[Bug] dfs_v2 tmpfs: ftruncate(fd, 0) frees the file buffer but reports success — use-after-free read/write and double free from a legal POSIX call可能已有人在做 @remukisama 于 33 天前认领。 未关闭in progress
难度 2/5 1-3 小时 新手友好度 78/100
RT-Thread/rt-thread#11902 · 4 条评论 · 已指派 1 人 ·
维护者通常 2 天内回复
-
[bsp][stm32][bluepill] README「快速上手」缺少重新生成 MDK 工程这一步,按文档操作无法编译通过可能重新可做 @moment-NEW 于 50 天前认领,目前没有进行中的 PR。 未关闭in progress
难度 2/5 1-3 小时 新手友好度 75/100
RT-Thread/rt-thread#11818 · 4 条评论 · 已指派 1 人 ·
维护者通常 2 天内回复
查看 RT-Thread/rt-thread 的全部 Issue
相似的 Issue
-
Builder: a structure opened without calculation data shows the Diffraction (fibre) view by default未关闭bug
难度 2/5 1-3 小时 新手友好度 65/100
FriendsofECCE/ECCE#1245 · 1 条评论 ·
-
难度 2/5 1-3 小时 新手友好度 72/100
apache/nuttx-apps#4108 · 4 条评论 ·
维护者通常 1 天内回复
-
bug
难度 1/5 1 小时以内 新手友好度 84/100
VirtualBox/virtualbox#996 ·
维护者通常 6 天内回复
-
难度 1/5 1 小时以内 新手友好度 82/100
trezor/trezor-firmware#8357 ·
维护者通常 2 天内回复
-
难度 2/5 1-3 小时 新手友好度 76/100
维护者通常 1 天内回复